Boost: :Trim_Right_If and Null Characters

I have the following code:

#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"));
}

I think that in str should be "12345", but there's "12345\0\0". Why and how can I solve it?

2 Answers

This code works

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

The trick is to use boost::as_array

I do not know this functions boost::is_any_of but the fact that its argument is a string literal it seems it considers "\0" as an empty set of characters (en empty string literal). So the algorithm trims nothing.

It is only my supposition.

6

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

David Miller

David Miller

Executive Financial & Market Analyst

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.

Share this article
Twitter Facebook Pinterest