3.16
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include <iostream> 2 #include <vector> 3 #include <string> 4 using namespace std; 5 using std::string; 6 int main() 7 { 8 vector<int> v1; 9 /* 10 for (int i = 0; i <= v1.size() - 1; ++i) { 11 cout << "v1的第" << i << "个元素:" << v1[i] << endl; 12 }*/ 13 vector<int> v2(10); 14 for (int i = 0; i <= v2.size()-1; ++i) { 15 cout << "v2的第" << i << "个元素:" << v2[i] << endl; 16 } 17 //cout << endl; 18 vector<int> v3(10, 42); 19 for (int i = 0; i <= v3.size()-1; ++i) { 20 cout << "v3的第" << i << "个元素:" << v3[i] << endl; 21 } 22 //cout << endl; 23 vector<int> v4{10}; 24 for (int i = 0; i <= v4.size()-1; ++i) { 25 cout << "v4的第" << i << "个元素:" << v4[i] << endl; 26 } 27 //cout << endl; 28 vector<int> v5{10, 42}; 29 for (int i = 0; i <= v5.size()-1; ++i) { 30 cout << "v5的第" << i << "个元素:" << v5[i] << endl; 31 } 32 //cout << endl; 33 vector<string> v6{10}; 34 for (int i = 0; i <= v6.size()-1; ++i) { 35 cout << "v6的第" << i << "个元素:" << v6[i] << endl; 36 } 37 //cout << endl; 38 vector<string> v7{10,"hi"}; 39 for (int i = 0; i <= v7.size()-1; ++i) { 40 cout << "v7的第" << i << "个元素:" << v7[i] << endl; 41 } 42 //cout << endl; 43 cout << "v7的长度: " << v7.size() << endl; 44 system("pause"); 45 return 0; 46 }
3.17
全部占一行:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include <iostream> 2 #include <vector> 3 #include <string> 4 using namespace std; 5 int main() 6 { 7 vector<string> str; 8 string temp; 9 getline(cin ,temp); 10 str.push_back(temp); 11 12 for (int i = 0; i < str.size(); ++i) { 13 for (int j = 0; j < str[i].size(); ++j) { 14 str[i][j] = toupper(str[i][j]); 15 } 16 } 17 for (int i = 0; i < str.size(); ++i) { 18 cout << str[i] << endl; 19 } 20 system("pause"); 21 return 0; 22 }
每个词占一行:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include <iostream> 2 #include <vector> 3 #include <string> 4 using namespace std; 5 int main() 6 { 7 vector<string> str; 8 string temp; 9 while (cin >> temp) { 10 str.push_back(temp); 11 } 12 13 for (int i = 0; i < str.size(); ++i) { 14 for (int j = 0; j < str[i].size(); ++j) { 15 str[i][j] = toupper(str[i][j]); 16 } 17 } 18 19 for (int i = 0; i < str.size(); ++i) { 20 cout << str[i] << endl; 21 } 22 23 system("pause"); 24 return 0; 25 }
3.18
不合法,ivec是空vector对象
改为:
vector<int> ivec(10); ivec[0] = 42;
3.19
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include <iostream> 2 #include <vector> 3 #include <string> 4 using namespace std; 5 int main() 6 { 7 vector<int> ivec1(10,42); 8 vector<int> ivec2{ 42,42,42,42,42,42,42,42,42,42 }; 9 vector<int> ivec3(ivec2); 10 for (int i = 0; i < ivec3.size(); ++i) { 11 cout << ivec3[i] << " "; 12 cout << endl; 13 } 14 system("pause"); 15 return 0; 16 }
3.20
输出每对相邻整数的和:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include <iostream> 2 #include <vector> 3 #include <string> 4 using namespace std; 5 int main() { 6 vector<int> v; 7 int s; 8 while (cin >> s) { 9 v.push_back(s); 10 } 11 for (int i = 1; i < v.size(); ++i) { 12 cout << v[i-1]+v[i] << " "; 13 cout << endl; 14 } 15 system("pause"); 16 return 0; 17 }
输出第一个和最后一个元素的和,第二个和倒数第二个元素的和。。。。:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include <iostream> 2 #include <vector> 3 #include <string> 4 using namespace std; 5 int main() 6 { 7 vector<int> v; 8 int s; 9 while (cin >> s) { 10 v.push_back(s); 11 } 12 for (vector<int>::size_type i = 1; i < v.size(); ++i) { 13 cout << v[i - 1] + v[v.size()-i] << " "; 14 cout << endl; 15 } 16 system("pause"); 17 return 0; 18 }
小结:下标运算符指的是形如:ivec[0]