STL vector
生活随笔
收集整理的這篇文章主要介紹了
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 }
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 }
編輯器加載中...
轉載于:https://www.cnblogs.com/uniquews/archive/2012/03/27/2420388.html
總結
以上是生活随笔為你收集整理的STL vector的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 访客IP API接口
- 下一篇: C语言宏替换