對不起,如果這是一個重複的問題,但我已經試圖尋找答案並空手而來。所以基本上我只想將字符串(單個單詞)添加到矢量的後面,然後將存儲的字符串顯示爲單個字符串。我是新秀。在C++中顯示一個向量字符串
#include <iostream>
#include <vector>
#include <string>
#include <cctype>
using namespace std;
int main(int a, char* b [])
{
vector<string> userString;
string word;
string sentence = "";
for (decltype(userString.size()) i = 0; i <= userString.size() - 1; i++)
{
cin >> word;
userString.push_back(word);
sentence += userString[i] + " ";
}
cout << sentence;
system("PAUSE");
return 0;
}
爲什麼不能正常工作?
編輯
int main(int a, char* b [])
{
cout << "Enter a sequence of words. Enter '.' \n";
vector<string> userString;
string word;
string sentence = ""; /
int wordCount = 0;
while (getline(cin, word))
{
if (word == ".")
{
break;
}
userString.push_back(word);
}
for (decltype(userString.size()) i = 0; i <= userString.size() - 1; i++)
{
sentence += userString[i] + " ";
wordCount += 1;
if (wordCount == 8)
{
sentence = sentence + "\n";
wordCount = 0;
}
}
cout << sentence << endl;
system("PAUSE");
return 0;
}
所以我的新節目工作。它只是將值放在矢量的後面,並將它們打印出8個單詞。我知道有更簡單的方法,但我只是學習矢量,而且我正在採取嬰兒步驟。謝謝你們的幫助。
「謝謝你們的幫助」,那你爲什麼不標記「答案」呢? – JackBauer
那麼這裏接受的答案是什麼? – Nogurenn