boost::is_any_of
과 boost::replace_all_copy
으로 간단한 코드 작업을하려고합니다. 스 니펫은 아래에 있습니다.boost :: is_any_of를 boost :: replace_all_copy와 함께 사용하는 방법
그러나 여기에 붙여 넣기에는 너무 긴 컴파일러 오류가 발생합니다. 이 두 가지 기능에 대한 경험이있는 사람이 저의 실수를 지적 할 수 있습니까?
boost::is_any_of
과 boost::replace_all_copy
으로 간단한 코드 작업을하려고합니다. 스 니펫은 아래에 있습니다.boost :: is_any_of를 boost :: replace_all_copy와 함께 사용하는 방법
그러나 여기에 붙여 넣기에는 너무 긴 컴파일러 오류가 발생합니다. 이 두 가지 기능에 대한 경험이있는 사람이 저의 실수를 지적 할 수 있습니까?
나는 그 특정 방법에 지나치게 익숙하지 않지만 replace_all_copy
은 is_any_of
의 결과가 아니라 대체 문자열을 원한다는 것을 알 수 있습니다.
#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;
}
출력 :
abc.def-ghi
abc def ghi
이 부스트 정규식 lib 디렉토리에 링크가 필요 않습니다 나는 또한 일하는 것이 정규식 버전이 나타났습니다 string algorithms의 다른 옵션을 반짝이기는
. 난 당신이 할 수없는 생각하지 않는다
g++ -L/usr/local/Cellar/boost/1.52.0/lib -lboost_regex-mt main.cpp
: 내 경우에는, 내가 함께 만들었습니다. The three parameter version of boost::replace_all_copy
은 입력 문자열, 대체 문자열 및 검색 할 문자열을 사용합니다. boost::is_any_of
이 반환하는 것은 술어 펑터입니다.
은 당신이 아마 원하는 것은 boost::replace_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;
}
희망을 : 당신이 그것을 유지해야하는 경우가 boost::replace_copy_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;
}
이것은, 원본을 수정합니다.
나는 또한 사본 부분이 원래 질문의 얼마나 중요한지 잘 모르겠다. 문서를 기반으로 someString이 제자리에서 수정되고 참조가 반환 된 것처럼 보입니다. 복사가 중요하지 않다면 정규 표현식 사용을 피하기 때문에 솔루션을 더 좋아합니다. –
@JesseVogt 좋은 지적, 나는 대답을 업데이트했다. – jrok
멋지다 - 문서에서'replace_copy_if'를 어떻게보고 싶었는지 모르겠습니다. 좋은 해결책. –