C++のベクトルを使用して個々の単語で文字列を分割しようとしています。だから私は文字列内の単語の間に複数のスペースを置く場合、ベクトルのスペースを無視する方法を知りたいと思います。ベクトルC++の空白を無視する
どうすればいいですか?
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
cout<<"Sentence: ";
string sentence;
getline(cin,sentence);
vector<string> my;
int start=0;
unsigned int end=sentence.size();
unsigned int temp=0;
while(temp<end){
int te=sentence.find(" ",start);
temp=te;
my.push_back(sentence.substr(start, temp-start));
start=temp+1;
}
unsigned int i;
for(i=0 ; i<my.size() ; i++){
cout<<my[i]<<endl;
}
return 0;
}
多分 'のstd :: regex'またはさらに良い'のstd :: stringstream'を使用しています。 – Ron
なぜ単純に 'cin >> string'を使って繰り返しませんか? –
私はコードを適切にインデントして、助けようとしている人のために明確にします。 – s5s