C++ //常用算术生成算法 //#include<numeric> accumulate //fill //向容器中填充指定的元素
1 //常用算术生成算法 //#include<numeric> accumulate 2 //fill //向容器中填充指定的元素 3 #include<iostream> 4 #include<vector> 5 #include<numeric> 6 #include<algorithm> 7 8 using namespace std; 9 10 11 void test01() 12 { 13 vector<int>v; 14 for (int i = 0; i <= 100; i++) 15 { 16 v.push_back(i); 17 } 18 19 //参数三 其实累加值 20 int total = accumulate(v.begin(), v.end(), 0); 21 22 cout << "total = " <<total<< endl; 23 24 } 25 26 //fill 27 void Myprint(int v) 28 { 29 cout << v << " "; 30 } 31 32 33 void test02() 34 { 35 vector<int>v2; 36 v2.resize(10); 37 for (vector<int>::iterator it = v2.begin(); it != v2.end(); it++) 38 { 39 cout << *it << " "; 40 } 41 cout << endl; 42 //后期重新填充 43 fill(v2.begin(), v2.end(), 99); 44 for_each(v2.begin(), v2.end(), Myprint); 45 cout << endl; 46 47 } 48 int main() 49 { 50 51 test01(); 52 test02(); 53 system("pause"); 54 return 0; 55 }
本文来自博客园,作者:Bytezero!,转载请注明原文链接:https://www.cnblogs.com/Bytezero/p/15158511.html