#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++;
}
}
}
我要拿出一個方案,它能夠C++ - 無法在if語句到底遞增for循環
輸入一個數字來聲明測試案例。如果輸入4,那麼就意味着這樣的有4個測試案例要輸入(完成)
它可以將一個字符串分解成一個矢量字符和商店。(部分完成)
有是目標2的問題(請參閱第二個for-loop)。我發現我的程序無法增加計數器。也就是說,一旦持有者[0]完成,它就會離開for循環和結束程序。它不會去持有人[1],持有人[2]等。
爲什麼它是如此以及如何解決問題?
感謝
調試,檢查值,你通常會發現它正在做你剛纔告訴它做的事情。如果它很混亂,可以將for循環轉換爲等效循環,因此在特定位置設置斷點更容易。也許你在數組中的位置與數組中的值混淆? –
你需要第二個循環計數器= 0到numberoftestcase – willll
@KennyOstrom是的,我有點混亂。因爲我不明白爲什麼當前的方法是錯誤的。我認爲我的邏輯是正確的「如果位置等於持有者[i]的長度,那麼計數器++」。這應該有效,但事實並非如此。我不明白爲什麼 – Ricky