C++ C++;从字符串中删除等号的单词
在为一个家庭作业问题实现逻辑方面存在一些问题。我目前使用的平台是Visual Studio 2013,我是初学者。我们使用应用程序内置的终端(命令提示符)来获取输入和输出。我们目前正在使用“CIN”和“COUT”。问题如下: “编写一个程序,要求用户输入一个句子,然后去掉每个偶数单词。例如:“所有总统的人”将变成“所有总统”。使用输出参数将修改过的句子返回main()函数,然后显示原始和修改过的句子。” 我一直在尝试将其应用于逻辑,将每个单词放入一个数组/向量中,并删除索引为偶数的每个单词。我还没有成功地完成这项工作,我正在寻求你们专家的帮助C++ C++;从字符串中删除等号的单词,c++,arrays,string,vector,C++,Arrays,String,Vector,在为一个家庭作业问题实现逻辑方面存在一些问题。我目前使用的平台是Visual Studio 2013,我是初学者。我们使用应用程序内置的终端(命令提示符)来获取输入和输出。我们目前正在使用“CIN”和“COUT”。问题如下: “编写一个程序,要求用户输入一个句子,然后去掉每个偶数单词。例如:“所有总统的人”将变成“所有总统”。使用输出参数将修改过的句子返回main()函数,然后显示原始和修改过的句子。” 我一直在尝试将其应用于逻辑,将每个单词放入一个数组/向量中,并删除索引为偶数的每个单词。我还
非常感谢。你可以这样写
int count = -1;
for (auto it =input.begin();it!=input.end();){
if(*it==' '){
count++;it++;
if (count%2==0){
while (it != input.end()){
if (*it==' ')break;
it=input.erase (it);
}
}else it++;
}else it++;
}`
std::字符串行;
//从cin流获取输入
if(std::getline(cin,line))//检查是否成功
{
向量词;
字符串字;
//使用“”分隔符拆分行的最简单方法是使用istreamstring+getline
std::istringstream;
str(line);
//将行拆分为单词,并将它们插入到向量“单词”中
while(std::getline(流,字,'))
单词。推回(单词);
if(words.size()%2!=0)//如果字数不均匀,则打印错误。
std::请更具体地说明您尝试了什么以及问题是什么(例如,向我们展示您的代码)。
std::string line;
// get input from cin stream
if (std::getline(cin, line)) // check for success
{
std::vector<std::string> words;
std::string word;
// The simplest way to split our line with a ' ' delimiter is using istreamstring + getline
std::istringstream stream;
stream.str(line);
// Split line into words and insert them into our vector "words"
while (std::getline(stream, word, ' '))
words.push_back(word);
if (words.size() % 2 != 0) // if word count is not even, print error.
std::cout << "Word count not even " << words.size() << " for string: " << line;
else
{
//Remove the last word from the vector to make it odd
words.pop_back();
std::cout << "Original: " << line << endl;
std::cout << "New:";
for (std::string& w : words)
cout << " " << w;
}
}