亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

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

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

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

兴化市| 丽江市| 临沂市| 阿拉尔市| 尼勒克县| 留坝县| 尼玛县| 甘孜县| 泸西县| 平塘县| 湖南省| 兴化市| 汝城县| 灌南县| 肃南| 昂仁县| 苍南县| 高邑县| 宾阳县| 屏山县| 文水县| 罗山县| 丹棱县| 乌兰县| 景谷| 桦川县| 砀山县| 桑植县| 昭觉县| 京山县| 鄂州市| 怀远县| 锡林浩特市| 陕西省| 桐乡市| 阿克| 永州市| 长乐市| 南华县| 磴口县| 巴青县|