16.swap交换算法

2021-05-15  本文已影响0人  lxr_
#include<iostream>
using namespace std;

#include<vector>
#include<algorithm>

//swap(container c1, container c2);互换两个容器中的元素

void MyPrintFunc(int val)
{
    cout << val << " ";
}
void test1601()
{
    vector<int> v1, v2;
    for (int i = 0; i < 10; i++)
    {
        v1.push_back(i);

        v2.push_back(9 - i);
    }
    cout << "交换前:" << endl;

    cout << "v1:" << endl;
    for_each(v1.begin(), v1.end(), MyPrintFunc);
    cout << endl;

    cout << "v2:" << endl;
    for_each(v2.begin(), v2.end(), MyPrintFunc);
    cout << endl;

    swap(v1, v2);

    cout << "交换后:" << endl;

    cout << "v1:" << endl;
    for_each(v1.begin(), v1.end(), MyPrintFunc);
    cout << endl;

    cout << "v2:" << endl;
    for_each(v2.begin(), v2.end(), MyPrintFunc);
    cout << endl;
}

int main()
{
    test1601();

    system("pause");
    return 0;
}
上一篇 下一篇

猜你喜欢

热点阅读