c++的提高篇

一、常用的排序算法

#include <iostream>
#include <vector>
#include <time.h>
#include <algorithm>


struct MyPrint01
{
    void operator()(int val){
        std::cout << val<<" ";
    }
};

struct MyCompare01
{
    bool operator()(int va11, int val2)
    {
        return va11 > val2;
    }
};

void Test01()
{
    std::vector<int> v1;
    std::vector<int> v2;

    srand((unsigned int)time(NULL));
    for (int i = 0; i < 9;i++)
    {
        v1.push_back(rand()%10);
    }
    for (int i = 0; i < 10;i++)
    {
        v2.push_back(rand() % 10);
    }
    std::cout << "排序前" << std::endl;
    for_each(v1.begin(), v1.end(), MyPrint01()); std::cout << std::endl;
    for_each(v2.begin(), v2.end(), MyPrint01()); std::cout << std::endl;

    sort(v1.begin(), v1.end());
    sort(v2.begin(), v2.end());
    std::cout << "排序后" << std::endl;
    std::cout << "合并算法" << std::endl;
    std::vector<int> v3;
    v3.resize(v1.size()+v2.size());
    merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
    for_each(v1.begin(), v1.end(), MyPrint01()); std::cout << std::endl;
    for_each(v2.begin(), v2.end(), MyPrint01()); std::cout << std::endl;
    for_each(v3.begin(), v3.end(), MyPrint01()); std::cout << std::endl;
    
    std::cout << "降序排序" << std::endl;
    std::cout << "合并算法" << std::endl;
    sort(v1.begin(), v1.end(),MyCompare01());
    sort(v2.begin(), v2.end(),MyCompare01());
    merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin(),MyCompare01());
    for_each(v1.begin(), v1.end(), MyPrint01()); std::cout << std::endl;
    for_each(v2.begin(), v2.end(), MyPrint01()); std::cout << std::endl;
    for_each(v3.begin(), v3.end(), MyPrint01()); std::cout << std::endl; 

    std::cout << "打乱算法" << std::endl;
    random_shuffle(v1.begin(), v1.end());
    for_each(v1.begin(), v1.end(), MyPrint01()); std::cout << std::endl;

    std::cout << "反转算法" << std::endl;
    reverse(v1.begin(), v1.end());
    for_each(v1.begin(), v1.end(), MyPrint01()); std::cout << std::endl;
}

int main()
{
    Test01();
    system("pause");
    return 0;
}

二、常用的拷贝和替换算法

#include <iostream>
#include <vector>
#include <time.h>
#include <algorithm>

struct MyPrint01
{
    void operator()(int v1){
        std::cout << v1<<" ";
    }
};

struct MyCompare01
{
    bool operator()(int val){
        return val > 5;
    }
};

void Test01(){

    std::vector<int> v1;
    srand((unsigned int)time(NULL));
    for (int i = 0; i < 10;i++)
    {
        v1.push_back(rand() % 10);
    }
    for_each(v1.begin(), v1.end(), MyPrint01()); std::cout << std::endl;

    std::vector<int> v2;
    v2.resize(v1.size());
    copy(v1.begin(), v1.end(), v2.begin());
    std::cout << "copy算法" << std::endl;
    for_each(v2.begin(), v2.end(), MyPrint01()); std::cout << std::endl;

    std::vector<int> v3;
    for (int i = 0; i < 10;i++)
    {
        v3.push_back(i);
    }
    std::cout << "v3容器值" << std::endl;
    for_each(v3.begin(), v3.end(), MyPrint01()); std::cout << std::endl;
    swap(v3, v2);

    std::cout << "swap算法" << std::endl;
    std::cout << "v2容器值" << std::endl;
    for_each(v2.begin(), v2.end(), MyPrint01()); std::cout << std::endl;
    std::cout << "v3容器值" << std::endl;
    for_each(v3.begin(), v3.end(), MyPrint01()); std::cout << std::endl;

    std::cout << "replace算法" << std::endl;
    replace(v2.begin(), v2.end(), 5, 100);
    for_each(v2.begin(), v2.end(), MyPrint01()); std::cout << std::endl;


    std::vector<int> v4;
    for (int i = 0; i < 10; i++)
    {
        v4.push_back(i);
    }
    std::cout << "v4容器值" << std::endl;
    for_each(v4.begin(), v4.end(), MyPrint01()); std::cout << std::endl;
    std::cout << "replace_if算法" << std::endl;
    replace_if(v4.begin(), v4.end(), MyCompare01(), 44);
    for_each(v4.begin(), v4.end(), MyPrint01()); std::cout << std::endl;
}
int main(){

    Test01();

    system("pause");
    return 0;
}

三、常用的生成数算法

#include <iostream>
#include <numeric>
#include <vector>
#include <algorithm>

struct MyPrint01
{
    void operator()(int val){
        std::cout << val << " ";
    }
};

void Test01(){

    std::vector<int> v1;

    for (int i = 0; i < 10;i++)
    {
        v1.push_back(i);
    }
    //accumulate算法
    int ret = accumulate(v1.begin(), v1.end(), 0);

    std::cout << "ret:" << ret<<std::endl;
    ret = accumulate(v1.begin(), v1.end(), 10);

    std::cout << "ret:" << ret << std::endl;

    std::vector<int> v2;
    v2.resize(20);
    //fill算法
    fill(v2.begin(), v2.end(), 55);
    for_each(v2.begin(), v2.end(), MyPrint01());
    std::cout << std::endl;

    
    std::vector<int> stdv1;
    std::vector<int> stdv2;

    for (int i = 0;i<10;i++)
    {
        stdv1.push_back(i);
    }
    for (int i = 5; i < 15;i++)
    {
        stdv2.push_back(i);
    }
    //交集
    std::vector<int> stdv3;
    std::cout << "交集" << std::endl;
    stdv3.resize(std::min(stdv1.size(), stdv2.size()));
    std::vector<int>::iterator it = set_intersection(stdv1.begin(), stdv1.end(), stdv2.begin(), stdv2.end(), stdv3.begin());
    for_each(stdv3.begin(), it, MyPrint01()); std::cout << std::endl;

    //并集
    std::vector<int> stdv4;
    std::cout << "并集" << std::endl;
    stdv4.resize(stdv1.size()+ stdv2.size());
    it = set_union(stdv1.begin(), stdv1.end(), stdv2.begin(), stdv2.end(), stdv4.begin());
    for_each(stdv4.begin(), it, MyPrint01()); std::cout << std::endl;

    //差集
    std::vector<int> stdv5;
    std::cout << "差集:stdv1差stdv2" << std::endl;
    stdv5.resize(std::max(stdv1.size(), stdv2.size()));
    it = set_difference(stdv1.begin(), stdv1.end(), stdv2.begin(), stdv2.end(), stdv5.begin());
    for_each(stdv5.begin(), it, MyPrint01()); std::cout << std::endl;

    //差集
    std::vector<int> stdv6;
    std::cout << "差集:stdv2差stdv1" << std::endl;
    stdv6.resize(std::max(stdv1.size(), stdv2.size()));
    it = set_difference(stdv2.begin(), stdv2.end(), stdv1.begin(), stdv1.end(), stdv6.begin());
    for_each(stdv6.begin(), it, MyPrint01()); std::cout << std::endl;
}
int main(){

    Test01();
    system("pause");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_42815643/article/details/129511341
今日推荐