无法在循环中的if statement结束时增加

Unable to increment at the end of an if-statement in the for-loop

本文关键字:statement 结束 增加 if 循环      更新时间:2023-10-16
#include <iostream>
#include <vector>
#include <algorithm>
#include <stdio.h>  
#include <string.h> 
using namespace std;
int main ()
{
  //Declaring variables:
  int numberoftestcase=0;
  string words;
  vector<string> holder;
  vector<char> character;
  int counter=0;
  //Allow user inputs a number to declare the size of the vector holder
  cin >> numberoftestcase;
  //Allow user to input words;
  for(int i=0;i<numberoftestcase;i++)
  {
    cin >> words;
    holder.push_back(words);
  }

for(int position=0;position<holder[counter].length();position++)
{
    if(position<holder[counter].length())
    {
        character.push_back(holder[counter].at(position)); 
        cout << character[position] << endl;  
    }
    else
    {
        counter++;
    }
  }
}

我想提出一个能够

的程序
  1. 输入一个要声明测试用例的数字。就像输入4一样,这意味着有4个测试用例可以输入(完成)

  2. 它可以将字符串分为字符并存储在向量中。(部分完成)

目标2存在问题(请参阅第二循环)。我发现我的程序无法增加计数器。也就是说,一旦持有人[0]完成,它就会离开for循环和结束程序。它不会交给持有人[1],持有人[2]等。

为什么会这样解决问题?

谢谢

您需要两个嵌套的for循环。一个通过持有者中的单词迭代,一个单词中的字符迭代。我会写为:

    for (const auto& word: holder)
    {
        for (const auto ch: word)
        {
            character.push_back(ch);
            cout << character.back();
        }
    }