STL-泛型算法

使用泛型算法需包含相关的头文件 algorithm头文件 numeric头文件 iterator头文件
    //find操作
    vector<int> ivec = {1, 15, 3, 16, 92, 29, 37, 31};
    int search_value = 26;
    vector<int>::const_iterator iter0 = find(ivec.begin(), ivec.end(), search_value);
    if (iter0 != ivec.end()) {
        cout<<"find the suitable element "<<"*iter0 = "<<*iter0<<endl;
    }
    else cout<<"no element is suitable."<<endl;
    
    //accumulate
    int ivec_count = accumulate(ivec.begin(), ivec.end(), 0);
    cout<<"sum of ivec = "<<ivec_count<<endl;
    ivec_count = accumulate(ivec.begin(), ivec.end(), 30);
    cout<<"sum of ivec add 30 = "<<ivec_count<<endl;
    
    vector<string> svec = {"abc", ".", "png"};
    string _str = accumulate(svec.begin(), svec.end(), string("*")); //传递字符串字面值将会导致编译错误,因为const char* 与string类型不 一致
    //这里加了一个"*",从最后的打印看出"*"是加在最前面的
    cout<<_str<<endl;
    
    //find_first_of 在第一段迭代器所指范围内查找与第二段范围任意元素匹配的元素 找到返回其迭代器,否则返回第一段范围的末端的下一个迭代器
    int cnt = 0;
    list<string> rester1 = {"mike", "rose", "Joe", "Ann"};
    list<string> rester2 = {"mike", "rose", "Amy"};
    list<string>::iterator it = rester1.begin();
    while ((it = find_first_of(it, rester1.end(), rester2.begin(), rester2.end())) != rester1.end()) {
        cnt++;
        ++it;
    }
    cout<<"find "<<cnt<<" element in rester2 is same of rester1"<<endl;
    
    //写容器元素 fill
    vector<int> ivec2 = {1, 2, 3, 4 ,5 ,6 ,7};
    fill(ivec2.begin(), ivec2.begin() + ivec2.size()/2, 0);
    vector<int>::iterator iter2 = ivec2.begin();
    cout<<"ivec2 = { ";
    while (iter2 != ivec2.end()) {
        cout<<*iter2<<" ";
        iter2++;
    }
    cout<<"}"<<endl;
    
    //fill_n 与fill类似,但是fill_n不检查边界。 fill(iter_beg, n, value)

    //back_inserter插入迭代器
    vector<int> ivec3 = {1, 2, 3, 4 ,5 ,6 ,7};
    fill_n(back_inserter(ivec3), 5, 0);
    vector<int>::iterator iter3 = ivec3.begin();
    cout<<"ivec3 = { ";
    while (iter3 != ivec3.end()) {
        cout<<*iter3<<" ";
        iter3++;
    }
    cout<<"}"<<endl;
    
    //写入到目标迭代器
    vector<int> ivec4 = {1, 2, 3, 4 ,5 ,6 ,7};
    copy(ivec3.begin(), ivec3.begin() + ivec3.size()/2, back_inserter(ivec4));  //把ivec3的部分元素复制到ivec4
    vector<int>::iterator iter4 = ivec4.begin();
    cout<<"ivec4 = { ";
    while (iter4 != ivec4.end()) {
        cout<<*iter4<<" ";
        iter4++;
    }
    cout<<"}"<<endl;
    
    //算法的_copy版本 对输入序列做处理 但不改变原来的元素,而使创建一个副本 如:
    vector<int> ivec5 = {3, 5, 3, 7, 3, 3, 6};
    replace(ivec5.begin(), ivec5.end(), 3, 30); //将ivec5中所有值为3的元素替换为30
    replace_copy(ivec5.begin(), ivec5.end(), back_inserter(ivec5), 30, 31); //第三个参数是迭代器,指定保存调整后序列的目标位置, 这里我还是把它放到了原ivec5的后面
    //...此处省略了打印容器内容的代码


打印结果

no element is suitable.
sum of ivec = 224
sum of ivec add 30 = 254
*abc.png
find 2 element in rester2 is same of rester1
ivec2 = { 0 0 0 4 5 6 7 }		 
ivec3 = { 1 2 3 4 5 6 7 0 0 0 0 0 }
ivec4 = { 1 2 3 4 5 6 7 1 2 3 4 5 6 }
ivec5 = { 30, 5, 30, 7, 30, 30, 6 }	
ivec5 = { 30, 5, 30, 7, 30, 30, 6, 31, 5, 31, 7, 31, 31,6 }
Program ended with exit code: 0






你可能感兴趣的:(C++,STL,泛型算法)