1
私はC++を使い慣れていないので、何かばかげた場合はでもが厳しくありません。区切り文字でsubstrが停止しないのはなぜですか?
文字列を2つの部分に分割しようとしています。私はsubstrを使って最初の部分を適切に分けることができますが、何らかの理由で2番目の部分を分離しようとすると、デリミタの後ろにすべてが必要になります。私はどこでそれを止めようとしていたのか(pos1)それが正しい位置であるかどうかを確認しましたが、その後もすべてを取ります。
for(u_int i = 0; i < eachPerson.size(); i++)
{
string temp, first, last;
u_int pos, pos1;
temp = eachPerson[i];
pos = temp.find_first_of(' ');
pos1 = temp.find_first_of(':');
cout << pos1 << endl;
first = temp.substr(0, pos);
last = temp.substr(pos+1, pos1);
cout << "First: " << first << endl
<< "Last: " << last << endl;
}
出力:
John Doe: 20 30 40 <- How each line looks before it's separated
Jane Doe: 60 70 80
8 <- Location of delimiter
First: John <- first
Last: Doe: 20 <- last
8
First: Jane
Last: Doe: 60
ああ...私は参照を2度読んでいたはずです...ありがとうございます。今、私は分かる。 –