【发布时间】:2011-10-13 09:55:49
【问题描述】:
我正在尝试做一些东西,它会接受用户输入的行,将它们分成向量中的字符串,然后一次打印一个(每行 8 个)。 到目前为止,这就是我所拥有的:
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
int main(void)
{
using namespace std;
vector<string> svec1;
string temp;
while(getline(cin, temp)) //stores lines of text in temp
{
if(temp.empty()) //checks if temp is empty, exits loop if so.
break;
stringstream ss(temp);
string word;
while(ss >> word) //takes each word and stores it in a slot on the vector svec1
{
svec1.push_back(word);
}
}
}
我一直坚持让它一次打印 8 个,我尝试过的解决方案不断出现下标超出范围的错误。
【问题讨论】:
-
并确保添加您的打印解决方案,以便我们帮助您解决下标错误。
-
你在哪里一次打印 8 个?
-
如此短的 sn-ps 应在此处内联,以便即使在 cast pastebin 关闭时您的问题仍然有效(注意:已经为您做过)
标签: c++ loops vector for-loop while-loop