実戦c++のvectorシリーズ--知っているemplace_backはなぜpushより優れているのかbackですか?


前のブログでvectorにstructを入れると言いましたが、structオブジェクトを構築してからpush_back.
そのコードでemplace_が使えないのはbackは、私たちが定義したstructに表示されていない構造関数のためです.
emplace和解?列挙の意味.
今回はstructをvectorの要素としないで、classをvectorの要素として、コードを書きます.
#include 
#include 
#include
using namespace std;
class CText {
private:
    string str;
public:
    text(string s) :str(s) {
    }
    void show()const {
        cout << str << endl;
    }

};
int main()
{
    vector vi;
    vi.emplace_back("hey");
    vi.front().show();
    vi.push_back("girl");//  
    vi.back().show();
    return 0;
}

ここでvi.push_back(“girl”);この文は間違っています.VS 2015は次のように間違っています.
error C2664: “void std::vectorstd::allocator<_ty>>::push_back(const text &)”:       1const 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 
#include 
#include 

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.
"
; } President(President&& other) : name(std::move(other.name)), country(std::move(other.country)), year(other.year) { std::cout << "I am being moved.
"
; } President& operator=(const President& other) = default; }; int main() { std::vector elections; std::cout << "emplace_back:
"
; elections.emplace_back("Nelson Mandela", "South Africa", 1994); std::vector reElections; std::cout << "
push_back:
"
; reElections.push_back(President("Franklin Delano Roosevelt", "the USA", 1936)); std::cout << "
Contents:
"
; for (President const& president : elections) { std::cout << president.name << " was elected president of " << president.country << " in " << president.year << ".
"
; } for (President const& president : reElections) { std::cout << president.name << " was re-elected president of " << president.country << " in " << president.year << ".
"
; } } // : 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.