zoukankan      html  css  js  c++  java
  • algorithm头文件中的函数:remove()与remove_if() 函数,……

    • remove(first, last, value) 函数移除[first, last) 范围的 value,返回新值范围的尾后迭代器

    在头文件 <algorithm>中,声明如下

    template< class ForwardIt, class T >
    ForwardIt remove( ForwardIt first, ForwardIt last, const T& value );//first,last都是迭代器,value是值或对象,返回新值范围的尾后迭代器
    

      使用方法如下

        vector<int> a = {11, 0, 2, 3, 10, 0, 0, 8, 0};
        cout << "Original size : " << a.size() << endl;
        auto itend = remove(a.begin(), a.end(), 0);
        cout << "after REMOVE, size : " << a.size() << endl;
        for (auto it = a.begin(); it != a.end(); ++it)
        {
            cout << *it << " ";
        }
        cout << endl;
        //结果:11 2 3 10 8 0 0 8 0
        //可以这么理解,把所以非value的值都向前移动,数组的大小不变,尾部剩下的部分值不变。
        //remove并没有真正删除元素value,而是返回新值范围的尾后迭代器。
        //可以用erase配合删除
        a.erase(itend, a.end());// 若想删除value值可以直接 a.erase(remove(a.begin(), a.end(), value), a.end());
        for (int& k : a )
        {
            cout << k << " ";
        }
        cout << endl;
       // 结果:11 2 3 10 8 
    
    • remove_if() 函数
    template< class ForwardIt, class UnaryPredicate >
    ForwardIt remove_if( ForwardIt first, ForwardIt last, UnaryPredicate p );
    //从范围 [first, last) 移除所有满足特定判别标准的元素,并返回新值范围的尾后迭代器。
    

      使用如下

        vector<int> a = {11, 0, 2, 3, 10, 0, 0, 8, 0};
        cout << "Original size : " << a.size() << endl;
    //    auto itend = remove_if(a.begin(), a.end(), bind2nd(greater<int>(),9));//移除大于9的数
        auto itend = remove_if(a.begin(), a.end(), [](int i){return i>9;});//也可以用 lambda 表达式,或者写一个bool类型的函数,将函数名做第三个参数。
        cout << "after REMOVE, size : " << a.size() << endl;
        for (auto it = a.begin(); it != a.end(); ++it)
        {
            cout << *it << " ";
        }
        cout << endl;
        //结果:0 2 3 0 0 8 0 8 0
        //同样的把不满足条件的数向前移动,数组大小不变,尾部剩下的值不变。返回新值范围的尾迭代器。
        a.erase(itend, a.end());
        for (int& k : a )
        {
            cout << k << " ";
        }
        cout << endl;
    

      另外,在<algorithm>头文件中,还有count() 函数与 count_if() 函数用法和上面的相似。find() 与 find_if()

    • count(first, last, value)

    其中,first和last 是迭代器或指针,表示范围 [first, last),value是一个值或对象。返回[first, last)中等于value的元素数。

    • count_if(first, last, p)

    其中,first和last 是迭代器或指针,表示范围[first, last),p是一元谓词,可以是函数对象,返回bool类型的函数名(即函数指针),lambda 表达式。返回满足p条件的元素数

    • find(first, last, value)

    返回 [first, last) 范围内第一个等于value的元素的迭代器。若没有找到,则返回 last。

    • find_if(first, last, p) 

    返回 [first, last) 范围内第一个满足p的条件的元素的迭代器。若没有找到,则返回 last。

    p是一元谓词,可以是函数对象,返回bool类型的函数名(即函数指针),lambda 表达式。

    • replace() 与 replace_if()
    template< class ForwardIt, class T >
    void replace( ForwardIt first, ForwardIt last,
                  const T& old_value, const T& new_value );  //在[first,last)范围,把所有的old_value替换为new_value。返回void
     
    template< class ForwardIt, class UnaryPredicate, class T >
    void replace_if( ForwardIt first, ForwardIt last,
                     UnaryPredicate p, const T& new_value ); //在[first,last)范围,把所有的满足p条件的元素替换为new_value。返回void
    
    • reverse(begin,end)

     Reverses the order of the elements in the range [first,last).颠倒顺序

     std::vector<int> myvector;
     for (int i=1; i<10; ++i) myvector.push_back(i);   // 1 2 3 4 5 6 7 8 9
     std::reverse(myvector.begin(),myvector.end());    // 9 8 7 6 5 4 3 2 1
    

      

    常记溪亭日暮,沉醉不知归路。兴尽晚回舟,误入藕花深处。争渡,争渡,惊起一滩鸥鹭。

    昨夜雨疏风骤,浓睡不消残酒。试问卷帘人,却道海棠依旧。知否?知否?应是绿肥红瘦。
  • 相关阅读:
    this和$(this)的关系
    单色边表格
    php概率算法
    jQuery Ajax 参数解析
    拍拍CPS入门使用
    飞鸽端口被占
    浏览器调试工具技巧分享
    事件click,bind,click
    jQuery旋转插件—rotate
    利用谷歌API生成二维码
  • 原文地址:https://www.cnblogs.com/htj10/p/9296597.html
Copyright © 2011-2022 走看看