简体   繁体   English

C ++ strtok函数拆分词

[英]C++ strtok function split words

i am to write a function called splitLine() in c++. 我要在C ++中编写一个名为splitLine()的函数。 can someone please help? 有人可以帮忙吗? im really confused 我真的很困惑

splitLine () {

    string temp = aLine;
    string *tempLine =  strtok(temp, " ");
    free(temp)
    countNum = sizeOf(tempLine);

   }

You're misunderstanding the instructions. 您误解了说明。

The strtok function operates on nul terminated char arrays (aka C strings) not C++ strings. strtok函数对nul终止的char数组(也称为C字符串)而不是C ++字符串进行操作。 So create a temporary 'string' actually means this 因此,创建一个临时的“字符串”实际上意味着

// create temporary string which is a copy of aLine
char* temp = new char[aLine.size() + 1];
strcpy(temp, aLine.c_str());

// extract words from temp
...

// free temporary string
delete[] temp;

Breaking the temporary string into words with strtok means writing a loop . strtok将临时字符串分解成单词意味着写一个循环 strtok will extract one word at a time. strtok提取一个单词。 I'm sure you can find examples of this on the internet. 我相信您可以在互联网上找到有关此示例。 So I'll leave that to you. 所以我留给你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM