STL 排序算法 复制 交换 求和 填充

void ShowEmlmt(int& obj)
{
    cout << obj << " ";
}

void main2()
{
    vector<int> v1;
    v1.push_back(1);
    v1.push_back(3);
    v1.push_back(5);

    vector<int> v2;
    v2.push_back(2);
    v2.push_back(4);
    v2.push_back(6);
    vector<int> v3(10);
    merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());//连个容器组合链接到新的容器中
    for_each(v3.begin(), v3.end(), ShowEmlmt);
    cout << endl;
    sort(v3.begin(), v3.end(), greater<int>());//排序
    for_each(v3.begin(), v3.end(), ShowEmlmt);
    cout << endl;
    random_shuffle(v3.begin(), v3.end());//随机
    for_each(v3.begin(), v3.end(), ShowEmlmt);
    cout << endl;
    reverse(v3.begin(), v3.end());//倒序
    for_each(v3.begin(), v3.end(), ShowEmlmt);
    cout << endl;
}
void printV(vector<int> v1)
{
    for (vector<int>::iterator it = v1.begin(); it != v1.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}

void main3()
{

    vector<int> v1;
    v1.push_back(1);
    v1.push_back(2);
    v1.push_back(3);
    vector<int> v2(v1.size());
    copy(v1.begin(), v1.end(), v2.begin());
    cout << "拷贝后" << endl;
    cout << "v1:";
    printV(v1);
    cout << "v2:";
    printV(v2);
    replace(v2.begin(), v2.end(), 1, 10);
    cout << "替换后" << endl;
    cout << "v1:";
    printV(v1);
    cout << "v2:";
    printV(v2);
    swap(v1, v2);
    cout << "交换后" << endl;
    cout << "v1:";
    printV(v1);
    cout << "v2:";
    printV(v2);

}

void main4()
{
    vector<int> v1;
    v1.push_back(1);
    v1.push_back(3);
    v1.push_back(5);

    int sum = accumulate(v1.begin(), v1.end(), 0);//求和算法 #include 
    cout << "sum=" << sum << endl;
    fill(v1.begin(), v1.end(), 0); //填充算法 可以初始化数组
    printV(v1);
}

你可能感兴趣的:(c++,排序算法)