描述:
交换两个容器的元素。
示例:
#include <iostream>
#include <iterator>
#include <vector>
#include <algorithm>
int main(int argc, char **argv)
{
std::vector<int> vecInt1 = { 1,2,3 };
std::vector<int> vecInt2 = { 3,4 };
std::swap(vecInt1, vecInt2);
auto print = [](int nA) {std::cout << nA << " "; };
std::for_each(vecInt1.begin(), vecInt1.end(), print);//3 4
std::cout << std::endl;
std::for_each(vecInt2.begin(), vecInt2.end(), print);//1 2 3
//也可以交换两个变量的值
int nA = 1, nB = 2;
std::swap(nA, nB);
std::cout << nA << std::endl;//2
std::cout << nB << std::endl;//1
return 0;
}