您的位置:首页 > 编程语言 > C语言/C++

C++顺序容器:头文件,定义和初始化

2009-07-08 16:46 381 查看
头文件:

#include <vector>
#include <list>
#include <deque>

定义:

在大多数的程序中,使用默认构造函数能达到最佳运行时性能,并且使容器更容易使用。

vector<string> svec; // empty vector that can hold strings
list<int> ilist; // empty list that can hold ints
deque<Sales_item> items; // empty deque that holds Sales_items

初始化注意事项:

定义容器的容器,如

vector< vector<string> > lines;

的时候,必须保证<与<,>与>之间有空格,否则会被解释为<<和>>操作符。

使用指针初始化容器:

char *words[] = {"stately", "plump", "buck", "mulligan"};
size_t words_size = sizeof(words)/sizeof(char *);
list<string> words2(words, words + words_size);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: