numeric中的accumulated的基本用法:
来自:https://blog.csdn.net/u011499425/article/details/52756242
1 #include <vector> 2 #include<numeric> 3 #include<iostream> 4 #include <string> 5 using namespace std; 6 struct Grade 7 { 8 string name; 9 int grade; 10 }; 11 int cmp(int a, Grade b) 12 { 13 return a + b.grade; 14 } 15 int main() 16 { 17 Grade subject[3] = { 18 { "English", 80 }, 19 { "Biology", 70 }, 20 { "History", 90 } 21 }; 22 23 //int sum = accumulate(subject, subject + 3, 0, [](int a, Grade b) {return a + b.grade; }); 24 int sum = accumulate(subject, subject + 3, 0, cmp); 25 cout << sum << endl; 26 27 system("pause"); 28 return 0; 29 }
algorithm的count:利用等于操作符,把标志范围内的元素与输入值比较,返回相等元素个数。
#include<iostream> #include<algorithm> using namespace std; int main() { int a[] = { 1, 2, 2, 6 }; int k = count(a, a + 4,2); cout << k << endl; return 0; }
unique:去重;