C++ boost::trim_right_if和null字符

C++ boost::trim_right_if和null字符,c++,boost,C++,Boost,我有以下代码: #include <boost/algorithm/string/classification.hpp> #include <boost/algorithm/string/trim.hpp> #include <boost/assign/list_of.hpp> #include <string> #include <vector> int main() { std::vector<char> som

我有以下代码:

#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/trim.hpp>
#include <boost/assign/list_of.hpp>

#include <string>
#include <vector>

int main()
{
  std::vector<char> some_vec = boost::assign::list_of('1')('2')('3')('4')('5')('\0')('\0');
  std::string str(some_vec.begin(), some_vec.end());
  boost::trim_right_if(str, boost::is_any_of("\0"));
}
#包括
#包括
#包括
#包括
#包括
int main()
{
std::vector some_vec=boost::assign::list_of('1')('2')('3')('4')('5')('0')('0'));
std::string str(一些_vec.begin(),一些_vec.end());
boost::trim_right_if(str,boost::is_any_of(“\0”));
}

我认为在str中应该是“12345”,但是有“12345\0\0”。为什么以及如何解决它?

我不知道boost::是什么函数,但它的参数是字符串文字,它似乎将“\0”视为一组空字符(en empty string literal)。因此,该算法不修剪任何内容

这只是我的假设。

此代码有效

boost::trim_right_if(str, boost::is_any_of(boost::as_array("\0") );

诀窍是使用
boost::as_array

您的假设是正确的。要修复,请使用:
boost::is_any_of(std::string('\0',1))
@Benjamin Lindley我认为应该是boost::is_any of(std::string('\0',1)),也就是说应该有普通引号。@vladfrommosco.
字符串
构造函数有一个重载,它需要一个
字符常量*
和计数,因此Benjamin是正确的。OP还可以为谓词使用lambda表达式
[](char c){return c=='\0';}
。否。这是一个不同的构造函数,但参数是相反的。i、 e.你可以使用
std::string(1,'\0')
@Benjamin Lindley在我写下评论后,我看到自己是std::basic_string的构造函数。事实上,您展示的记录可以使用。:)现在我认为在这种情况下,什么构造函数更好,我认为最好使用construector with charT,而不是const charT*:)