avr39ripe

cppSTLInsertEmplacePush_backDifference

Sep 30th, 2021
658
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. class Item
  6. {
  7. public:
  8.     Item() { std::cout << "Item default constructed " << this << '\n'; }
  9.     Item(const Item& it) { std::cout << "Item copy constructed " << this << " from " << &it << '\n'; }
  10.     Item(Item&& it) { std::cout << "Item move constructed " << this << " from " << &it << '\n'; }
  11.     Item& operator=(const Item& it) { std::cout << "Item copy assigned " << this << " from " << &it << '\n'; return *this; }
  12.     Item& operator=(Item&& it) { std::cout << "Item move assigned " << this << " from " << &it << '\n'; return *this; }
  13.     ~Item() { std::cout << "Item destructed " << this << '\n'; }
  14.     friend std::ostream& operator<<(std::ostream& out, const Item& item)
  15.     {
  16.         return out << "Item # " << &item;
  17.     }
  18. };
  19.  
  20.  
  21.  
  22. int main()
  23. {
  24.     Item it;
  25.  
  26.     std::vector<Item> items;
  27.     std::cout << "Pre reserve\n";
  28.     items.reserve(3);
  29.     std::cout << "Post reserve\n";
  30.     //items.emplace_back(Item{});
  31.     items.emplace(items.end());
  32.     items.emplace(items.end());
  33.     //items.insert(items.begin(), it);
  34.     //items.insert(items.begin(), Item{});
  35.     //items.emplace(items.begin());
  36.    
  37.     std::copy(items.begin(), items.end(), std::ostream_iterator<Item>(std::cout, "\n"));
  38. }
  39.  
RAW Paste Data