zoukankan      html  css  js  c++  java
  • cb26a_c++_STL_算法_元素计数

    cb26a_c++_STL_算法_元素计数
    所有容器都可以使用
    count
    count_if
    关联容器的等效成员函数,容器自己的成员函数速度较快
    1.set.count
    2.multiset.count
    3.map.count
    4.multimap.count

    error C2275: “std::greater<int>”: 将此类型用作表达式非法
    少了括号:std::greater<int>()
    error C3861: “bind2nd”: 找不到标识符
    需要包含:#include <functional>//greater包含在里面,预定义的函数对象

    预定义的函数对象配合预定义的函数适配器一起使用
    bind2nd(greater<int>(), 3),
    bind2nd--预定义的函数适配器
    greater<int>()--预定义的函数对象

    modulus<type>() 取模运算
    not1(op),一个参数,返回结果真,假
    not2(op),二个参数
    */

     1 /*cb26a_c++_STL_算法_元素计数
     2 所有容器都可以使用
     3 count
     4 count_if
     5 关联容器的等效成员函数,容器自己的成员函数速度较快
     6 1.set.count
     7 2.multiset.count
     8 3.map.count
     9 4.multimap.count
    10 
    11 error C2275: “std::greater<int>”: 将此类型用作表达式非法
    12 少了括号:std::greater<int>()
    13 error C3861: “bind2nd”: 找不到标识符
    14 需要包含:#include <functional>//greater包含在里面,预定义的函数对象
    15 
    16 预定义的函数对象配合预定义的函数适配器一起使用
    17 bind2nd(greater<int>(), 3),
    18 bind2nd--预定义的函数适配器
    19 greater<int>()--预定义的函数对象
    20 
    21 modulus<type>() 取模运算
    22 not1(op),一个参数,返回结果真,假
    23 not2(op),二个参数
    24 */
    25 #include <iostream>
    26 #include <algorithm>
    27 #include <vector>
    28 #include <set>
    29 #include <functional>//greater包含在里面
    30 
    31 using namespace std;
    32 
    33 //函数返回是bool,称为谓词
    34 bool isEven(int elem)
    35 {
    36     return elem % 2 == 0;
    37 }
    38 
    39 
    40 int main()
    41 {
    42     vector<int> ivec;
    43     for (int i = 1; i <= 11; ++i)
    44         ivec.push_back(i);
    45     for (vector<int>::iterator iter = ivec.begin(); iter != ivec.end(); ++iter)
    46         cout << *iter << endl;
    47 
    48     int num;
    49     num = count(ivec.begin(), ivec.end(), 8);
    50     cout << "" << num << "个8" << endl;
    51 
    52     int num2;
    53     num2 = count_if(ivec.begin(), ivec.end(), isEven);//调用iven函数,自己写的函数
    54     cout << "统计偶数的个数: " << num2 << endl;
    55 
    56     num2 = count_if(ivec.begin(), ivec.end(), bind2nd(modulus<int>(),2));//使用modulus取模运算,奇数时返回1,
    57     cout << "modulus方法-奇数的个数: " << num2 << endl;
    58 
    59     num2 = count_if(ivec.begin(), ivec.end(), not1(bind2nd(modulus<int>(), 2)));//使用modulus取模运算,not1,取反,返回偶数,
    60     cout << "modulus方法-偶数的个数: " << num2 << endl;
    61                                            //函数适配器 函数对象
    62                                                          //param1>param2
    63     num = count_if(ivec.begin(), ivec.end(), bind2nd(greater<int>(), 3));
    64     //预定义的函数对象:https://blog.csdn.net/txwtech/article/details/104382505
    65     cout << "大于3的个数:" << num << endl;
    66 
    67     cout << "multiset例子" << endl;
    68     multiset<int>mset;
    69     for (int i = 1; i <= 9; ++i)
    70         //mset.insert(i);
    71         mset.insert(mset.end(), i);
    72     mset.insert(2);
    73     mset.insert(7);
    74     for (multiset<int>::iterator iter = mset.begin(); iter != mset.end(); ++iter)
    75         cout << *iter << ' ';
    76         cout << endl;
    77 
    78         num = count(mset.begin(), mset.end(), 7);
    79         cout << "统计7的个数:" <<num<< endl;
    80 
    81         num = mset.count(7);//高效查找,二叉树,自动排序了的。
    82         cout << "mulset自己的count成员函数:个数:" << num << endl;
    83 
    84 
    85 
    86     return 0;
    87 }
    欢迎讨论,相互学习。 txwtech@163.com
  • 相关阅读:
    [MTG][介绍]企业消息处理平台
    [MYSQL][TIP]入门级命令
    [JWF][API] 显示当前所有用户信息
    五一去了五里河公园
    [UML][Feel]活动图的建立
    [JWF][DOC] COM Object Library Reference
    计算机网络操作系统历年试题
    embed标签的使用
    Android初体验D2
    ScrollJquery列表无间隙滚动
  • 原文地址:https://www.cnblogs.com/txwtech/p/12330413.html
Copyright © 2011-2022 走看看