• std::remove_if


    【1】std::remove_if

    今天突然有同事问下面这段代码应如何理解。

    源码如下:

    void GStiffenerBlockRepoDefaultImpl::destory(int id) noexcept
    {
        m_stiffeners.erase(std::remove_if(std::begin(m_stiffeners), std::end(m_stiffeners), [&](auto s)->bool {
            return s.id == id; 
        }), std::end(m_stiffeners));
    }

    【2】应用示例

    用代码说话,比讲解更容易理解。请看如下示例与运行结果:

    (1)示例程序

     1 #include <iostream>
     2 #include <string>
     3 #include <vector>
     4 #include <algorithm>
     5 using namespace std;
     6 
     7 int main()
     8 {
     9     vector<string> srcStr = { "apple", "banana", "key", "cat", "dog", "orange", "banana", "pig", "dog", "monkey" };
    10 
    11     auto printSrc = [&]() {
    12         int index = 0;
    13         for (auto& item : srcStr)
    14         {
    15             cout << (index++) << "|" << item << " ";
    16         }
    17     };
    18 
    19     cout << "*************************************************" << endl;
    20     cout << "the original element of vector size :: " << srcStr.size() << endl;
    21     cout << "the original element of vector :: " << endl;
    22     printSrc();
    23     cout << endl << "*************************************************" << endl;
    24 
    25     cout << endl << "+++++++++++++++++++++++++++++++++++++++++++++++++" << endl;
    26     auto find_str = "banana";
    27     auto sd = remove_if(srcStr.begin(), srcStr.end(), [&](string element) { return element == find_str; });
    28     cout << "after remove banana size :: " << srcStr.size() << endl;
    29     cout << "after remove banana :: " << endl;
    30     printSrc();
    31 
    32     int index = 0;
    33     vector<string>::iterator iter = sd;
    34     cout << endl << "before erase banana, prepare to erase data :: " << endl;
    35     for (; iter != srcStr.end(); ++iter)
    36     {
    37         cout << (index++) << "|" << *iter << " ";
    38     }
    39 
    40     srcStr.erase(sd, srcStr.end());
    41     cout << endl << "after erase banana size :: " << srcStr.size() << endl;
    42     cout << "after erase banana data :: " << endl;
    43     printSrc();
    44     cout << endl << "+++++++++++++++++++++++++++++++++++++++++++++++++" << endl;
    45 
    46     cout << endl << "=================================================" << endl;
    47     find_str = "dog";
    48     srcStr.erase(remove_if(srcStr.begin(), srcStr.end(),
    49         [&](string element) { return element == find_str; }),
    50         srcStr.end());
    51     
    52     cout << "after erase dog size :: " << srcStr.size() << endl;
    53     cout << "after erase dog data :: " << endl;
    54     printSrc();
    55     cout << endl << "=================================================" << endl;
    56 
    57     return 0;
    58 }

    (2)运行结果

    【3】总结

    (1)

    (2)

     

  • 相关阅读:
    rsync
    typora 页内跳转
    shell脚本搭建redis集群
    Html
    python json模块
    Appium
    selenium+python
    jmeter+ant+jenkins接口自动化测试框架
    测试小结
    面向对向
  • 原文地址:https://www.cnblogs.com/Braveliu/p/14082928.html
Copyright © 2020-2023  润新知