练习3.14:编写一段程序,用cin输入一组整数并把它们存入一个vector对象。
练习3.15:改写上题的程序,不过这次读入的是字符串。
答案:见云盘程序
练习3.14
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int v1;
vector<int> vec1;
while (cin >> v1){
vec1.push_back(v1);
}
return 0;
}
练习3.15
#include <iostream>
#include <vector>
using namespace std;
int main()
{
string word;
vector<string> text;
while (cin >> word){
text.push_back(word);
}
for (auto &i : text)
cout << i << " " ;
cout << endl;
return 0;
}
本文介绍了如何使用C++编程语言通过cin输入整数和字符串,并将这些数据存储到vector容器中。对于整数,使用int类型;对于字符串,则使用string类型。两个示例展示了如何动态地将输入的数据追加到vector末尾。
592

被折叠的 条评论
为什么被折叠?



