Advertisement
avr39ripe

cppSTLEmplaceVsInsert

Sep 30th, 2021
1,100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.00 KB | None | 0 0
  1. #include <iostream>
  2. #include <memory>
  3. #include <array>
  4. #include <vector>
  5. #include <forward_list>
  6. #include <list>
  7. #include <algorithm>
  8. #include <functional>
  9.  
  10. namespace itstep
  11. {
  12.     template <typename InIter, typename Func>
  13.     void for_each(InIter begin, InIter end, Func func)
  14.     {
  15.         for (; begin != end; ++begin)
  16.         {
  17.             func(*begin);
  18.         }
  19.     }
  20.  
  21.     template <typename T>
  22.     void print(const T& elem, char delimeter = '\0')
  23.     {
  24.         std::cout << elem;
  25.         if (delimeter)
  26.         {
  27.             std::cout << delimeter;
  28.         }
  29.     }
  30.  
  31.     template <typename T>
  32.     void mul2(T& elem)
  33.     {
  34.         elem *= 2;
  35.     }
  36.  
  37. }
  38.  
  39. class Item
  40. {
  41. public:
  42.     Item() { std::cout << "Item default constructed " << this << '\n'; }
  43.     Item(const Item& it) { std::cout << "Item copy constructed " << this << " from " << &it << '\n'; }
  44.     Item(Item&& it) { std::cout << "Item move constructed " << this << " from " << &it << '\n'; }
  45.     Item& operator=(const Item& it) { std::cout << "Item copy assigned " << this << " from " << &it << '\n'; return *this; }
  46.     Item& operator=(Item&& it) { std::cout << "Item move assigned " << this << " from " << &it << '\n'; return *this; }
  47.     ~Item() { std::cout << "Item destructed " << this << '\n'; }
  48.     friend std::ostream& operator<<(std::ostream& out, const Item& item)
  49.     {
  50.         return out << "Item # " << &item;
  51.     }
  52. };
  53.  
  54.  
  55.  
  56. int main()
  57. {
  58.     std::vector<int> arr{1,2,3,4,5,6,7,8,9,10};
  59.     //arr.reserve(150);
  60.     //std::cout << "Original size = " << arr.size() << " capacity = " << arr.capacity() << '\n';
  61.  
  62.     //for (int i{ 0 }; i < 64; ++i)
  63.     //{
  64.     //  arr.push_back(i);
  65.     //  std::cout << "size = " << arr.size() << " capacity = " << arr.capacity() << '\n';
  66.     //}
  67.     //
  68.     ////arr.shrink_to_fit();
  69.     //arr.resize(32);
  70.     //std::cout << "Shrinked size = " << arr.size() << " capacity = " << arr.capacity() << '\n';
  71.     //arr.reserve(16);
  72.     //std::cout << "Post reserve 16 size = " << arr.size() << " capacity = " << arr.capacity() << '\n';
  73.  
  74.     //std::copy(arr.begin(), arr.end(), std::ostream_iterator<int>(std::cout, " "));
  75.     //std::cout << '\n';
  76.  
  77.     //arr.push_back(11);
  78.  
  79.     //std::copy(arr.begin(), arr.end(), std::ostream_iterator<int>(std::cout, " "));
  80.     //std::cout << '\n';
  81.  
  82.     //arr.pop_back();
  83.     //std::copy(arr.begin(), arr.end(), std::ostream_iterator<int>(std::cout, " "));
  84.     //std::cout << '\n';
  85.  
  86.     //std::list<int> newVal{ 42,43,44,45 };
  87.  
  88.     //arr.insert(arr.begin(), newVal.begin(), newVal.end());
  89.     //std::copy(arr.begin(), arr.end(), std::ostream_iterator<int>(std::cout, " "));
  90.     //std::cout << '\n';
  91.  
  92.     ////arr.erase(arr.begin(), arr.end());
  93.     ////arr.clear();
  94.     //*arr.begin() = 444;
  95.  
  96.     //std::copy(arr.begin(), arr.end(), std::ostream_iterator<int>(std::cout, " "));
  97.     //std::cout << '\n';
  98.  
  99.     Item it;
  100.     std::cout << it << '\n';
  101.  
  102.     std::vector<Item> items;
  103.     //items.reserve(4);
  104.     items.resize(4);
  105.     std::cout << "Original size = " << items.size() << " capacity = " << items.capacity() << '\n';
  106.  
  107.     items.insert(items.begin(), it);
  108.     std::copy(items.begin(), items.end(), std::ostream_iterator<Item>(std::cout, " "));
  109.     std::cout << '\n';
  110.  
  111.     return 0;
  112. }
  113.  
  114.  
  115.  
  116.  
  117.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement