// swap (vector overload)
#include <iostream>
#include <vector>
main ()
{
unsignedint i;
std::vector<int> foo (3,100); // three ints with a value of 100
std::vector<int> bar (5,200); // five ints with a value of 200
foo.swap(bar);
std::cout << "foo contains:";
for (std::vector<int>::iterator it = foo.begin(); it!=foo.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
std::cout << "bar contains:";
for (std::vector<int>::iterator it = bar.begin(); it!=bar.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
In line 11, is there is a mistake, instead of writing std :: swap (foo, bar), then write foo.swap (bar)?
Both work, but since the page that you link to is for the free (non-member function) version it would probably have been better to show it in the example instead of the swap member function (http://www.cplusplus.com/reference/vector/vector/swap/).