91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

C++ 中"emplace_back" 與 "push_back" 的區別

發布時間:2020-09-25 14:11:33 來源:腳本之家 閱讀:116 作者:lqh 欄目:編程語言

 C++ 中"emplace_back" 與 "push_back" 的區別

emplace_backpush_back都是向容器內添加數據.

對于在容器中添加類的對象時, 相比于push_back,emplace_back可以避免額外類的復制和移動操作.

"emplace_back avoids the extra copy or move operation required when using push_back."

參見: http://en.cppreference.com/w/cpp/container/vector/emplace_back

注意下面代碼中的emplace_back和push_back的添加方式(VS2012下編譯通過):

#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); //沒有類的創建 
 
  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"; 
  } 
} 

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

新建县| 台南县| 凤城市| 大方县| 平塘县| 溧水县| 白水县| 扶余县| 容城县| 林州市| 石柱| 阿巴嘎旗| 金华市| 浠水县| 陇南市| 玛沁县| 巴林左旗| 凤阳县| 灵宝市| 米易县| 广宗县| 维西| 班玛县| 琼中| 邵阳市| 安丘市| 铅山县| 邯郸市| 黄龙县| 镇江市| 巴彦淖尔市| 长沙市| 怀仁县| 北碚区| 巴马| 隆安县| 杭锦旗| 内江市| 吐鲁番市| 伊川县| 册亨县|