swap vector c 2b 2b

Solutions on MaxInterview for swap vector c 2b 2b by the best coders in the world

showing results for - "swap vector c 2b 2b"
Anton
27 Oct 2016
1std::iter_swap(arr.begin()+pos1,arr.begin()+pos2);
Kaiden
19 Feb 2017
1vector<T>().swap(x);   // clear x reallocating 
Maria
08 Feb 2016
1vector1.swap<vector2>;
2//time complexity: O(1)
Manuel
12 May 2016
1#include <iostream>
2#include <vector>
3 
4template<class Os, class Co> Os& operator<<(Os& os, const Co& co) {
5    os << "{";
6    for (auto const& i : co) { os << ' ' << i; }
7    return os << " } ";
8}
9 
10int main()
11{
12    std::vector<int> a1{1, 2, 3}, a2{4, 5};
13 
14    auto it1 = std::next(a1.begin());
15    auto it2 = std::next(a2.begin());
16 
17    int& ref1 = a1.front();
18    int& ref2 = a2.front();
19 
20    std::cout << a1 << a2 << *it1 << ' ' << *it2 << ' ' << ref1 << ' ' << ref2 << '\n';
21    a1.swap(a2);
22    std::cout << a1 << a2 << *it1 << ' ' << *it2 << ' ' << ref1 << ' ' << ref2 << '\n';
23 
24    // Note that after swap the iterators and references stay associated with their
25    // original elements, e.g. it1 that pointed to an element in 'a1' with value 2
26    // still points to the same element, though this element was moved into 'a2'.
27}
Ben
24 Jan 2016
1// clearing vectors
2#include <iostream>
3#include <vector>
4
5int main ()
6{
7  std::vector<int> myvector;
8  myvector.push_back (100);
9  myvector.push_back (200);
10  myvector.push_back (300);
11
12  std::cout << "myvector contains:";
13  for (unsigned i=0; i<myvector.size(); i++)
14    std::cout << ' ' << myvector[i];
15  std::cout << '\n';
16
17  myvector.clear();
18  myvector.push_back (1101);
19  myvector.push_back (2202);
20
21  std::cout << "myvector contains:";
22  for (unsigned i=0; i<myvector.size(); i++)
23    std::cout << ' ' << myvector[i];
24  std::cout << '\n';
25
26  return 0;
27}
similar questions
queries leading to this page
swap vector c 2b 2b