STL


Vector
1 用vector向量容器装入10个整数,然后,使用迭代器iterator和accumulate算法统计
出这10个元素的和
    
    
    
    
  1. #include "stdafx.h"
  2. #include "vector"
  3. #include "iostream"
  4. #include "numeric" //accumulate need it
  5. using namespace std;
  6. int _tmain(int argc, _TCHAR* argv[])
  7. {
  8. vector<int> vint;
  9. //初始化vector
  10. for (int i=0; i<10; i++)
  11. {
  12. vint.push_back(i);
  13. }
  14. //使用迭代器
  15. vector<int>::iterator iter;
  16. for (iter = vint.begin(); iter != vint.end(); iter++)
  17. {
  18. cout << *iter << " "; //need * before iter
  19. }
  20. cout << endl;
  21. int sum = accumulate(vint.begin(), vint.end(), 0);
  22. cout << "sum=" << sum << endl;
  23. return 0;
  24. }






来自为知笔记(Wiz)


你可能感兴趣的:(STL)