我只是C++的新手,我一直在研究这个简短的程序。我似乎找不到它出了什么问题。它可以很好地输出first_name字符串,但是当我尝试输出称为w的 vector 的内容时,什么也没发生。我将不胜感激。毫无疑问,这里有一个简单的解释,例如我错过了一些语法。谢谢!
int main()
{
cout<< "Please enter the first name of the person you are writing to\n";
string first_name = "??";
cin >> first_name;
vector<string>w;
cout << "Enter your message to "<<first_name<<"?\n";
for(string word; cin>>word;)
w.push_back(word);
for(int i=0; i<w.size();++i)
cout<<w[i]<<'\n';
}
最佳答案
那是因为你没有阻止你循环
int main()
{
cout<< "Please enter the first name of the person you are writing to\n";
string first_name = "??";
cin >> first_name;
vector<string>w;
cout << "Enter your message to "<<first_name<<"?\n";
for(string word; cin>>word && word != "q";)
w.push_back(word);
for(int i=0; i<w.size();++i)
cout<<w[i]<<'\n';
}
那对我有用。这将是一个无限循环,但如果传递给q,它将停止
关于c++ - 我无法从此 vector 检索输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62247922/