C++ 中"emplace_back" 與 "push_back" 的區(qū)別
C++ 中"emplace_back" 與 "push_back" 的區(qū)別
emplace_back和push_back都是向容器內(nèi)添加數(shù)據(jù).
對(duì)于在容器中添加類(lèi)的對(duì)象時(shí), 相比于push_back,emplace_back可以避免額外類(lèi)的復(fù)制和移動(dòng)操作.
"emplace_back avoids the extra copy or move operation required when using push_back."
參見(jiàn): http://en.cppreference.com/w/cpp/container/vector/emplace_back
注意下面代碼中的emplace_back和push_back的添加方式(VS2012下編譯通過(guò)):
#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);
};
int main()
{
std::vector<President> elections;
std::cout << "emplace_back:\n";
elections.emplace_back("Nelson Mandela", "South Africa", 1994); //沒(méi)有類(lèi)的創(chuàng)建
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";
}
}
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
C語(yǔ)言實(shí)現(xiàn)電話簿管理系統(tǒng)課程設(shè)計(jì)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)電話簿管理系統(tǒng)課程設(shè)計(jì),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11
基于c++的中國(guó)象棋游戲設(shè)計(jì)與實(shí)現(xiàn)
這篇文章主要介紹了基于c++的中國(guó)象棋游戲設(shè)計(jì)與實(shí)現(xiàn),主要操作是possibleMove(int?x,?int?y),通過(guò)整個(gè)棋盤(pán)每個(gè)位置上的信息、中國(guó)象棋的規(guī)則來(lái)獲得位置(x,?y)這個(gè)棋子可以移動(dòng)到的位置,需要的朋友可以參考一下2022-02-02
C語(yǔ)言模擬實(shí)現(xiàn)庫(kù)函數(shù)詳解
C語(yǔ)言庫(kù)函數(shù)是把自定義函數(shù)放到庫(kù)里,是別人把一些常用到的函數(shù)編完放到一個(gè)文件里,供程序員使用,下面讓我們一起來(lái)詳細(xì)了解它2022-07-07
利用C++實(shí)現(xiàn)計(jì)算機(jī)輔助教學(xué)系統(tǒng)
我們都知道計(jì)算機(jī)在教育中起的作用越來(lái)越大。這篇文章主要為大家詳細(xì)介紹了如何利用C++編寫(xiě)一個(gè)計(jì)算機(jī)輔助教學(xué)系統(tǒng),感興趣的可以了解一下2023-05-05
Qt?http編程之nlohmann?json庫(kù)使用詳解
nlohmann是一個(gè)C++的JSON庫(kù),它提供了方便的方式來(lái)解析、生成和操作JSON數(shù)據(jù),這篇文章主要為大家介紹了nlohmann?json庫(kù)的簡(jiǎn)單使用,希望對(duì)大家有所幫助2024-04-04
C++模擬實(shí)現(xiàn)string類(lèi)的實(shí)例代碼
這篇文章主要給大家介紹了C++如何模擬實(shí)現(xiàn)string類(lèi),文章通過(guò)代碼示例講解的非常詳細(xì),有完整的實(shí)現(xiàn)過(guò)程,具有一定的參考價(jià)值,需要的朋友可以參考下2023-08-08
C++日期類(lèi)(Date)實(shí)現(xiàn)的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用C++語(yǔ)言實(shí)現(xiàn)日期類(lèi)(Date),可以實(shí)現(xiàn)確定某年某月有多少天、打印日期等功能,感興趣的可以了解一下2022-07-07
C語(yǔ)言實(shí)現(xiàn)學(xué)生選課系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)學(xué)生選課系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02

