10.1 Overview(概述)
1. auto result = find(vec.cbegin(), vec.cend(), val);
find返回一个指向与第一个元素的相同值的变量,如果find没有找到,它将会返回第二个参数,来指向失败。
2.int n = count(vec.cbegin(), vec.cend(), val);
count类似find,返回值为val出现的次数。
#include <iostream> #include <algorithm> #include <vector> #include <list> #include <string> using namespace std; int main() { list<string> li = { "hello", "ni", "hello", "si" }; string str; cin >> str; int n = count(li.cbegin(), li.cend(), str); cout << str << "出现" << n << "次" << endl; system("PAUSE"); return 0; }

浙公网安备 33010602011771号