[英]Why a c++ string tokenizer isn't working
我试图用C ++写一个简单的std :: string标记器,但我无法使其正常工作。 我发现了一个网上这不工作,我理解为什么它的作品....但我仍然无法弄清楚,为什么我原来的一个不工作。 我假设它缺少了一些愚蠢的小东西。 谢谢!
输入(带有“ \\ n”,“ \\ t”的随机字符和符号):
"This is a test string;23248h> w chars, aNn, 8132; ai3v2< 8&G,\nnewline7iuf32\t2f,f3rgb, 43q\nefhfh\nu2hef, wew; wg"
标记生成器:
size_t loc, prevLoc = 0;
while( (int)(loc = theStr.find_first_of("\n", prevLoc) ) > 0) {
string subStr = theStr.substr(prevLoc, loc-1); // -1 to skip the \n
cout << "SUBSTR: '" << subStr << "'" << endl << endl;
tokenizedStr->push_back( subStr );
prevLoc = loc+1;
} // while
输出:
SUBSTR: 'This is a test string;23248h> w chars, aNn, 8132; ai3v2< 8&G'
SUBSTR: 'newline7iuf32 2f,f3rgb, 43q
efhfh
u2hef, wew; wg'
SUBSTR: 'efhfh
u2hef, wew; wg'
请注意,第二个“ SUBSTR”(显然)仍包含换行符(“ \\ n”)
可编译的代码:
#include <vector.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
using namespace std;
int main(int argc, char *argv[]) {
string testStr = "This is a test string;23248h> w chars, aNn, 8132; ai3v2< 8&G,\nnewline7iuf32\t2f,f3rgb, 43q\nefhfh\nu2hef, wew; wg";
vector<string> tokenizedStr;
size_t loc, prevLoc = 0;
while( (int)(loc = testStr.find_first_of("\n", prevLoc) ) > 0) {
string subStr = testStr.substr(prevLoc, loc-1); // -1 to skip the \n
cout << "SUBSTR: '" << subStr << "'" << endl << endl;
tokenizedStr.push_back( subStr );
prevLoc = loc+1;
} // while
return 0;
}
substr
的第二个参数是大小,而不是位置。 而不是这样称呼它:
testStr.substr(prevLoc, loc-1);
尝试这个:
testStr.substr(prevLoc, loc-prevLoc);
一旦你解决了这个问题,你将遇到的下一个问题就是你没有打印最后一个子字符串,因为一旦你找不到换行符就停止了。 因此,从最后一个换行符到字符串末尾的位置都不会存储。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.