boost::trim 每个字符串在 std::vector<std::string>;

2021-12-26 00:00:00 foreach c++ stl boost

我目前无法找到修剪 std::vector 中每个字符串的正确语法.

我试过了

std::vectorv;std::for_each(v.begin(), v.end(), &boost::trim);

在 MSVC7.1 中给了我以下错误消息.

<块引用>

错误 C2784:'_Fn1 std::for_each(_InIt,_InIt,_Fn1)':无法从带有 [_Ty=std:: 的'std::vector<_Ty>::iterator'推导出'T1'的模板参数string] : 见'std::for_each'的声明

错误 C2896:'_Fn1 std::for_each(_InIt,_InIt,_Fn1)':不能使用函数模板 'void boost::algorithm::trim(SequenceT &,const std::locale &)' 作为函数参数:参见'boost::algorithm::trim'的声明

如果我明确给模板参数trims,编译器找不到第二个参数,尽管它是默认设置的.

std::for_each(v.begin(), v.end(), &boost::trim<std::string>);

<块引用>

错误 C2198:'void (__cdecl *)(std::string &,const std::locale &)':通过函数指针调用的参数太少

我想知道为 v 中的每个元素调用 trim 的正确语法是什么样的.

解决方案

你还需要绑定trim的第二个参数(locale):

std::vectorv;std::for_each(v.begin(), v.end(),boost::bind(&boost::trim<std::string>,_1, std::locale() ));

I'm currently stuck finding the correct syntax for trimming each string in a std::vector.

I tried

std::vector<std::string> v;
std::for_each(v.begin(), v.end(), &boost::trim);

which gave me the following error messages in MSVC7.1.

error C2784: '_Fn1 std::for_each(_InIt,_InIt,_Fn1)' : could not deduce template argument for 'T1' from 'std::vector<_Ty>::iterator' with [_Ty=std::string] : see declaration of 'std::for_each'

error C2896: '_Fn1 std::for_each(_InIt,_InIt,_Fn1)' : cannot use function template 'void boost::algorithm::trim(SequenceT &,const std::locale &)' as a function argument : see declaration of 'boost::algorithm::trim'

If I explicitly give the template parameter trims second parameter can not be found by the compiler, though its set by default.

std::for_each(v.begin(), v.end(), &boost::trim<std::string>);

error C2198: 'void (__cdecl *)(std::string &,const std::locale &)' : too few arguments for call through pointer-to-function

I was wondering how the correct syntax to call trim for each element in v would look like.

解决方案

You need to bind as well the second parameter of trim (the locale):

std::vector<std::string> v;
std::for_each(v.begin(), v.end(), 
              boost::bind(&boost::trim<std::string>,
                          _1, std::locale() ));

相关文章