Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 通过指定的分隔符拆分wstring_C++_String_Split - Fatal编程技术网

C++ 通过指定的分隔符拆分wstring

C++ 通过指定的分隔符拆分wstring,c++,string,split,C++,String,Split,我有一个std::wstring变量,它包含一个文本,我需要用分隔符将其拆分。我怎么能这样做?我不会使用boost生成一些警告。多谢各位 编辑1 这是一个示例文本: 嗨,你好吗 这是代码: typedef boost::tokenizer<boost::char_separator<wchar_t>, std::wstring::const_iterator, std::wstring> Tok; boost::char_separator<wchar_t>

我有一个std::wstring变量,它包含一个文本,我需要用分隔符将其拆分。我怎么能这样做?我不会使用boost生成一些警告。多谢各位

编辑1 这是一个示例文本:

嗨,你好吗

这是代码:

typedef boost::tokenizer<boost::char_separator<wchar_t>, std::wstring::const_iterator, std::wstring> Tok;

boost::char_separator<wchar_t> sep;

Tok tok(this->m_inputText, sep);

for(Tok::iterator tok_iter = tok.begin(); tok_iter != tok.end(); ++tok_iter)
{
    cout << *tok_iter;
}
typedef boost::令牌化器Tok;
boost::字符分离器sep;
Tok Tok(此->m_输入文本,sep);
for(Tok::iterator Tok_iter=Tok.begin();Tok_iter!=Tok.end();++Tok_iter)
{

cout您好,您可以使用函数

您说过您不想要boost,所以

<>这可能是一个在C++中使用的WiRD方法,但我在MUD中使用了一个,在C.

中需要大量的标记化。 将此内存块分配给char*chars:

char chars[]=“我喜欢摆弄记忆”

如果需要对空格字符进行标记化:

create array of char* called splitvalues big enough to store all tokens
while not increment pointer chars and compare value to '\0'
  if not already set set address of splitvalues[counter] to current memory address - 1
     if value is ' ' write 0 there
       increment counter
完成后,原始字符串将被销毁,因此不要使用它,而是使用指向令牌的字符串数组。令牌计数是计数器变量(数组的上限)

方法是:

  • 迭代字符串,并在第一次出现时更新令牌开始指针
  • 将需要拆分的字符转换为零,这在C中表示字符串终止
  • 数一数你做了多少次

PS.不确定在unicode环境中是否可以使用类似的方法。

您默认构建
boost::char\u separator
。表示:

函数std::isspace()用于标识删除的分隔符,std::ispunct()用于标识保留的分隔符。此外,还会删除空标记


由于
std::ispunct(L'?')
为true,因此它被视为“保留”分隔符,并作为单独的标记报告。

在代码中,问号出现在单独的一行,因为默认情况下boost::tokenizer就是这样工作的

如果您想要的输出是四个标记(“hi”、“how”、“are”和“you?”),那么您可以

a) 更改要使用的字符分隔符

boost::char_separator<wchar_t> sep(L" ", L"");
测试运行:

可能重复的,有和没有boost的方法都有
#include <string>
#include <iostream>
#include <vector>
#include <boost/algorithm/string.hpp>

int main()
{

        std::wstring m_inputText = L"hi how are you?";

        std::vector<std::wstring> tok;
        split(tok, m_inputText, boost::is_any_of(L" "));

        for(std::vector<std::wstring>::iterator tok_iter = tok.begin();
                        tok_iter != tok.end(); ++tok_iter)
        {
                std::wcout << *tok_iter << '\n';
        }

}