<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                ##實戰c++中的vector系列--知道emplace_back為何優于push_back嗎? 上一篇博客說道vector中放入struct,我們先構造一個struct對象,再push_back。 那段代碼中,之所以不能使用emplace_back,就是因為我們定義的struct沒有顯示的構造函數。 emplace和解?? 放列的意思。 這次我們不把struct當做vector的元素了,我們把一個class當做vector的元素,寫下代碼: ~~~ #include <iostream> #include <vector> #include<string> using namespace std; class CText { private: string str; public: text(string s) :str(s) { } void show()const { cout << str << endl; } }; int main() { vector<CText > vi; vi.emplace_back("hey"); vi.front().show(); vi.push_back("girl");//錯誤 vi.back().show(); return 0; } ~~~ 其中vi.push_back(“girl”);這條語句錯誤,VS2015報錯為: ~~~ error C2664: “void std::vector<text,std::allocator<_Ty>>::push_back(const text &)”: 無法將參數 1 從“const char [5]”轉換為“text &&” ~~~ 但此時我們稍作修改:? 把 vi.push_back(“girl”) 改為? vi.push_back(CText(“girl”));? 問題就解決了。。 簡而言之,就是empace_back與push_back相比,替我們省去了調用CText進行構造。 **emplace_back**? 添加一個新元素到結束的容器。該元件是構成在就地,即沒有復制或移動操作進行。 inserts a new element at the end of the vector, right after its current last element. This new element is constructed in place using args as the arguments for its constructor. This effectively increases the container size by one, which causes an automatic reallocation of the allocated storage space if -and only if- the new vector size surpasses the current vector capacity. The element is constructed in-place by calling allocator_traits::construct with args forwarded. A similar member function exists, push_back, which either copies or moves an existing object into the container. 寫到這里,你應該明白emplace_back如何是也了吧。 最后再來一段代碼,涉及到使用右值引用和std::move的: ~~~ #include <vector> #include <string> #include <iostream> struct President { std::string name; std::string country; int year; President(std::string && p_name, std::string && p_country, int p_year) : name(std::move(p_name)), country(std::move(p_country)), year(p_year) { std::cout << "I am being constructed.\n"; } President(President&& other) : name(std::move(other.name)), country(std::move(other.country)), year(other.year) { std::cout << "I am being moved.\n"; } President& operator=(const President& other) = default; }; int main() { std::vector<President> elections; std::cout << "emplace_back:\n"; elections.emplace_back("Nelson Mandela", "South Africa", 1994); std::vector<President> reElections; std::cout << "\npush_back:\n"; reElections.push_back(President("Franklin Delano Roosevelt", "the USA", 1936)); std::cout << "\nContents:\n"; for (President const& president : elections) { std::cout << president.name << " was elected president of " << president.country << " in " << president.year << ".\n"; } for (President const& president : reElections) { std::cout << president.name << " was re-elected president of " << president.country << " in " << president.year << ".\n"; } } //輸出: emplace_back: I am being constructed. push_back: I am being constructed. I am being moved. Contents: Nelson Mandela was elected president of South Africa in 1994. Franklin Delano Roosevelt was re-elected president of the USA in 1936. ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看