STL container

作者:jicanmeng

时间:2017年06月21日


Sequence Containers的两个例子:

vector的用法:

  1. #include <iostream>
  2. #include <vector>
  3. int main()
  4. {
  5. using namespace std;
  6. vector<int> vect;
  7. for (int nCount=0; nCount < 6; nCount++)
  8. vect.push_back(10 - nCount); // insert at end of array
  9. for (int nIndex=0; nIndex < vect.size(); nIndex++)
  10. cout << vect[nIndex] << " ";
  11. cout << endl;
  12. }

以下是运行结果:

[jicanmeng@andy tmp]$ ./a.out
                10 9 8 7 6 5
			[jicanmeng@andy tmp]$

deque的用法:

  1. #include <iostream>
  2. #include <deque>
  3. int main()
  4. {
  5. using namespace std;
  6. deque<int> deq;
  7. for (int nCount=0; nCount < 3; nCount++) {
  8. deq.push_back(nCount); // insert at end of array
  9. deq.push_front(10 - nCount); // insert at front of array
  10. }
  11. for (int nIndex=0; nIndex < deq.size(); nIndex++)
  12. cout << deq[nIndex] << " ";
  13. cout << endl;
  14. }

以下是运行结果:

[jicanmeng@andy tmp]$ ./a.out
                8 9 10 0 1 2
			[jicanmeng@andy tmp]$

参考资料

  1. <<C++实用教程>> 电子工业出版社 郑阿奇 主编 丁有和 编著 P280
  2. The C++ Tutorial:
    http://www.learncpp.com/cpp-tutorial/16-1-the-standard-template-library-stl/
  3. misc:
    http://www.cnblogs.com/shiyangxt/archive/2008/09/11/1289493.html