STL vector

View Code
 1 #include<iostream>
2 #include<vector>
3 #include<string>
4
5 using namespace std;
6
7
8 int main()
9
10 {
11
12 vector<int> ivec;//空!大小为零
13 cout << ivec.size() <<endl;
14
15 cout<<"请输入5个整数"<<endl;
16 int k;
17 for(vector<int>::size_type ix =0; ix!=5;++ix)
18 { // cin << ivec[ix]; 这种写法是错的,如果要使用下标,必须要已经由下标才行,空表则不可以!想用下标先给表赋值或……
19 cin >> k;
20 ivec.push_back(k);
21
22 }
23 cout<<"显示vector里的数据"<<endl;
24
25 for(vector<int>::size_type m=0; m!=ivec.size() ;++m)
26 {
27
28 cout<< ivec[m]<<endl;
29
30 }
31
32
33 cout<<"下面请输入一些字符串"<<endl;
34
35 string word;
36 vector<string> text;
37 while(cin >> word)
38 text.push_back(word);
39
40
41 cout<<"你输入的字符串是:"<<endl;
42
43 for(vector<string>::size_type n=0 ;n!=text.size(); ++n)
44 cout << text[n]<<endl;
45
46
47 return 0;
48 }


编辑器加载中...

posted @ 2012-03-27 23:15  uniquews  阅读(166)  评论(0编辑  收藏  举报