gpt4 book ai didi

c++ - Boost字符串算法错误

转载 作者:行者123 更新时间:2023-11-28 02:40:47 25 4
gpt4 key购买 nike

我对 boost 字符串算法库有疑问。我尝试使用 split 和 tokenize 来拆分/标记 wstring,但我总是遇到以下错误

代码

std::vector<std::wstring> tokenize(const std::wstring& input) {
std::vector<std::wstring> output;
boost::char_separator<wchar_t> sep(L";");
boost::tokenizer<boost::char_separator<wchar_t>> tokens(input, sep);
std::for_each(tokens.begin(), tokens.end(),
[&output] (std::wstring ws) {
output.push_back(ws);
}
);
return output;
}

错误信息

 error C2664: 'std::_String_const_iterator<std::_String_val<std::_Simple_types<char>>>::_String_const_iterator
(const std::_String_const_iterator<std::_String_val<std::_Simple_types<char>>> &)' :
cannot convert argument 1
from 'std::_String_const_iterator<std::_String_val<std::_Simple_types<wchar_t>>>'
to 'const std::_String_const_iterator<std::_String_val<std::_Simple_types<char>>> &'

我尝试过其他方法,例如 boost::split 或将 wstring 更改为 string,但它不起作用。

这里有什么问题吗?

最佳答案

查看tokenizer.hpp的源码,tokenizer是这样定义的:

  template <
typename TokenizerFunc = char_delimiters_separator<char>,
typename Iterator = std::string::const_iterator,
typename Type = std::string
>
class tokenizer {
...

您只为类模板指定了 TokenizerFunc,但忘记指定 IteratorType。结果,您收到了错误:cannot convert argument 1 ...wchar_t... to ...char...

为了让你的代码运行起来,你应该指定 boost::tokenizer 的所有参数,例如:

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

代码:

#include <stdlib.h>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <boost/tokenizer.hpp>

std::vector<std::wstring> tokenize(const std::wstring& input) {
std::vector<std::wstring> output;

typedef boost::tokenizer<boost::char_separator<wchar_t>, std::wstring::const_iterator, std::wstring > tokenizer;
boost::char_separator<wchar_t> sep(L";");
tokenizer tokens(input, sep);

std::for_each(tokens.begin(), tokens.end(),
[&output] (std::wstring ws) {
output.push_back(ws);
}
);

return output;
}

int main(int argc, char* argv[]) {

auto v = tokenize(L"one;two;three");
std::copy(v.begin(), v.end(), std::ostream_iterator<std::wstring, wchar_t>(std::wcout, L" "));

return EXIT_SUCCESS;
}

输出:

one two three 

关于c++ - Boost字符串算法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26075935/

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