gpt4 book ai didi

c++ - 使用 std::regex_token_iterator(不一定)查找单词以查找字符串中的字符位置

转载 作者:太空宇宙 更新时间:2023-11-04 13:42:49 25 4
gpt4 key购买 nike

string zodis;
sregex_token_iterator it(eil.begin(), eil.end(), std::regex("[A-Za-z]+"));
sregex_token_iterator reg_end;

for (; it != reg_end; ++it) {
zodis = it -> str();
/* ... */
}

基本上我使用上面的代码得到了这个词,但我想找出字符串的 zodis 在什么位置字符串中的第一个字符 eil是(字符串 zodis 取自 eil )

我尝试指向从 begin() 返回的迭代器,但这只是给了我字符

有什么想法吗?

最佳答案

好吧,所以我想出了一个干净利落的方法。

    int basepoint=0;
while(whatever)
{
int c1=zodis.end()-zodis.begin();//the length of the word
int c2=eil.find(zodis,basepoint);//the starting position of the word
basepoint+=c1+c2;//start point for the find function for the next loop
}

这将找到所有单词的起点(即使整行都是同一个单词)

如果你有

的文本文件
word word word word word

使用这个

cout << c1 << " " << c2;

其中c1是从字符串中取出的单词的长度,c2是起始位置,会给你

4 0
4 5
4 10
4 15
4 20

关于c++ - 使用 std::regex_token_iterator(不一定)查找单词以查找字符串中的字符位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27088427/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com