如果是声明的时候就初始化,好像只有使用每个容器的带参数构造函数来初始化
除此之外,就是声明以后的初始化,
对于list, deque,vector, 使用 assign
对于下面的,
set, multiset, map, multimap 使用 insert
stack, queue, priority_queue 使用 push
不过对于这些实际上已经不算是是真正的初始化了。
list:
// list::assign
#include
#include
using namespace std;
int main ()
{
list
list
first.assign (7,100); // 7 ints with value 100
second.assign (first.begin(),first.end()); // a copy of first
int myints[]={1776,7,4};
first.assign (myints,myints+3); // assigning from array
cout << "Size of first: " << int (first.size()) << endl;
cout << "Size of second: " << int (second.size()) << endl;
return 0;
}
====================
output:
Size of first: 3
Size of second: 7
====================
vector:
// vector assign
#include
#include
using namespace std;
int main ()
{
vector
vector
vector
first.assign (7,100); // a repetition 7 times of value 100
vector
it=first.begin()+1;
second.assign (it,first.end()-1); // the 5 central values of first
int myints[] = {1776,7,4};
third.assign (myints,myints+3); // assigning from array.
cout << "Size of first: " << int (first.size()) << endl;
cout << "Size of second: " << int (second.size()) << endl;
cout << "Size of third: " << int (third.size()) << endl;
return 0;
}
====================
output:
Size of first: 7
Size of second: 5
Size of third: 3
====================
deque:
// deque::assign
#include
#include
using namespace std;
int main ()
{
deque
deque
deque
first.assign (7,100); // a repetition 7 times of value 100
deque
it=first.begin()+1;
second.assign (it,first.end()-1); // the 5 central values of first
int myints[] = {1776,7,4};
third.assign (myints,myints+3); // assigning from array.
cout << "Size of first: " << int (first.size()) << endl;
cout << "Size of second: " << int (second.size()) << endl;
cout << "Size of third: " << int (third.size()) << endl;
return 0;
}
====================
output:
Size of first: 7
Size of second: 5
Size of third: 3
====================
set:
// set::insert
#include
#include
using namespace std;
int main ()
{
set
set
pair
// set some initial values:
for (int i=1; i<=5; i++) myset.insert(i*10); // set: 10 20 30 40 50
ret = myset.insert(20); // no new element inserted
if (ret.second==false) it=ret.first; // "it" now points to element 20
myset.insert (it,25); // max efficiency inserting
myset.insert (it,24); // max efficiency inserting
myset.insert (it,26); // no max efficiency inserting
int myints[]= {5,10,15}; // 10 already in set, not inserted
myset.insert (myints,myints+3);
cout << "myset contains:";
for (it=myset.begin(); it!=myset.end(); it++)
cout << " " << *it;
cout << endl;
return 0;
}
====================
output:
myset contains: 5 10 15 20 24 25 26 30 40 50
====================
只要在声明对象的时候,增加初始化列表,使对象创建时调用对应的构造函数,即可完成同时初始化的操作。
具体调用方式,依赖于对象类型,及支持的构造函数。
以stl中的string类为例,如定义
string a;
即无参构造,将a初始化为空字符串。
如
string a("for test");
就是把string类型的对象a初始化为"for test"值。
具体对象支持的构造函数列表,可以查询STL的使用手册获取,根据程序需要灵活选择构造的参数个数和值。
STL 一般来说没有 vector
而是建议采用先初始化元素,然后再添加元素到容器中去的方法,这也是STL中容器的高效访问的来由。
http://blog.chinahr.com/blog/kenlinc/post/98781
这是出自 C++ Primer中文版(第4版) 3.3 标准库vector类型
http://www.cppreference.com/
随时可以查看