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

    技术2022-05-11  15

    头文件:

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

     

    定义:

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

    vector<string>    svec;       // empty vector that can hold stringslist<int>         ilist;      // empty list that can hold intsdeque<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);


    最新回复(0)