Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我在 C 中使用 strtok() 函数。它返回指向它拆分字符串的位置的指针。
int main() { char s[100]="A B C D"; char *p; p=strtok(s," "); while(p!=NULL){ p=strtok(NULL,"."); //Do something } return 0; }
如何找出发生拆分的索引(最好在恒定时间内)?
要查找索引:
ptrdiff_t index = p - s;
旁白:显示您的真实代码 -s = "A B C D"不会编译。
s = "A B C D"
它很简单,如上一篇文章中所述。Strtok 返回地址。用数组的初始地址减去返回的地址..
p - 秒;