简体   繁体   中英

How to use boost::is_any_of with boost::replace_all_copy

I am trying to make a simple piece of code work with boost::is_any_of and boost::replace_all_copy . The snippet is below:

std::string someString = "abc.def-ghi";
std::string toReplace = ".-";
std::string processedString = boost::replace_all_copy(someString, boost::is_any_of(toReplace), " ");

However, I get a compiler error that is too long to paste here. Could someone that has experience with these 2 functions please point out my error?

I don't think you can't. The three parameter version of boost::replace_all_copy takes the input string, a substitute string and the string to search for. What boost::is_any_of returns is a predicate functor.

What you probably want is boost::replace_if :

#include <boost/algorithm/string.hpp>            // for is_any_of
#include <boost/range/algorithm/replace_if.hpp>  // for replace_if
#include <string>
#include <iostream>

std::string someString = "abc.def-ghi";
std::string toReplace = ".-";
std::string processedString =
   boost::replace_if(someString, boost::is_any_of(toReplace), ' ');

int main()
{
    std::cout << processedString;
}

This modifies the original, so if you need to keep it, you can use boost::replace_copy_if :

#include <boost/algorithm/string.hpp>
#include <boost/range/algorithm/replace_copy_if.hpp>
#include <string>
#include <iostream>
#include <iterator>    // for back_inserter

std::string someString = "abc.def-ghi";
std::string toReplace = ".-";

int main()
{
    std::string processedString;
    boost::replace_copy_if(someString,
        std::back_inserter(processedString), boost::is_any_of(toReplace), ' ');
    std::cout << processedString;
}

Hope that helps.

I am not overly familiar with that particular method but it appears that replace_all_copy wants just a replacement string rather than the result of is_any_of .

Glancing through the other options for string algorithms I noticed that there is a regex version that would also work:

#include <iostream>                                                                                                                                                                  
#include <boost/algorithm/string.hpp>                                                                                                                                                
#include <boost/algorithm/string/regex.hpp>                                                                                                                                          

int main(int argc, char** argv) {                                                                                                                                                    
    std::string someString = "abc.def-ghi";                                                                                                                                          
    std::cout << someString << std::endl;                                                                                                                                            
    std::string toReplace = "[.-]"; // character class that matches . and -                                                                                                          
    std::string replacement = " ";                                                                                                                                                   
    std::string processedString =                                                                                                                                                    
        boost::replace_all_regex_copy(someString, boost::regex(toReplace), replacement);                                                                                             
    std::cout << processedString << std::endl;                                                                                                                                       
    return 0;                                                                                                                                                                        
} 

Output:

abc.def-ghi
abc def ghi

This does require linking against the boost regex lib. In my case, I built with:

g++ -L/usr/local/Cellar/boost/1.52.0/lib -lboost_regex-mt main.cpp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM