我是编程新手。我一直试图用C++函数在给定参数爆炸字符串的内容转换为字符串数组,例如:如何将一个字符串通过分隔符分割成一个数组?
string str = "___this_ is__ th_e str__ing we__ will use__";
应返回字符串数组:
cout << stringArray[0]; // 'this'
cout << stringArray[1]; // ' is'
cout << stringArray[2]; // ' th'
cout << stringArray[3]; // 'e str'
cout << stringArray[4]; // 'ing we'
cout << stringArray[5]; // ' will use'
我可以令牌化该字符串很好,但对我来说最难的部分是我如何指定stringArray中的元素数量,然后将其分配给当前字符串toke以及如何从函数返回stringArray。
有人会告诉我如何编写函数?
编辑1:我不一定需要结果在字符串数组中,只是任何容器,我可以作为一种常规变量与某种索引调用。
功课,或许?没问题,当然,但我从回答作业问题的人群不同... – dmckee 2009-05-20 20:59:53
重复:http://stackoverflow.com/questions/53849/how-do-i-tokenize-a-string-in-c – lothar 2009-05-20 21:08:43
@Iharhar这里的答案似乎更有效率。 – Arnthor 2011-10-16 14:49:44