2012-08-31 2 views
0

저는이 문제에 어려움을 겪고 있습니다. 어떤 진전을 이루지 못했고 도움을 요청할 시점입니다. 부스트 라이브러리에 대한 나의 친숙 함은 피상적 인 것보다 약간 낫다. 다소 큰 문자열을 통해 점진적 검사를 수행하려고합니다. 사실 std :: string 객체로 읽힌 파일의 전체 내용입니다 (파일이 커지지는 않을 것입니다, 명령 행 프로그램의 출력입니다).boost :: regex_search가 내 인수를 거부합니다.

이 프로그램의 출력 pnputil은 반복적입니다. 내가 원하는 "oemNNN.inf"파일을 찾기 위해 특정 패턴을 찾고 있습니다. 본질적으로, 내 알고리즘은 첫 번째 "oemNNN.inf"를 찾아 해당 파일의 특성을 식별하는 것입니다. 그것이 내가 원하는 사람이 아니라면, 다음으로 이동하십시오.

std::string filesContents; 
std::string::size_type index(filesContents.find_first_of("oem")); 
std::string::iterator start(filesContents.begin() + index); 
boost::match_results<std::string::const_iterator> matches; 
while(!found) { 
    if(boost::regex_search(start, filesContents.end(), matches, re)) 
    { 
     // do important stuff with the matches 
     found = true; // found is used outside of loop too 
     break; 
    } 

    index = filesContents.find_first_of("oem", index + 1); 
    if(std::string::npos == index) break; 
    start = filesContents.being() + index; 
} 

내가 사용 this example 부스트 라이브러리 설명서에서 1.47 (내가 사용 버전)에 대한 : 코드에서

, 그것은 같은입니다. 누군가 제 사용법이이 예제가 가지고있는 것과 다른 점을 제게 설명해주십시오. (사실 저는 맵에 물건을 저장하지 않습니다.)

내가 알 수있는 바로는, 예제에서 사용하는 반복기와 동일한 유형을 사용하고 있습니다. 그러나 코드를 컴파일 할 때 Microsoft 컴파일러는 다음과 같이 알려줍니다. overloaded function boost :: regex_search 인스턴스가 인수 목록과 일치하지 않습니다. 그러나 인텔리 센스는 필자가 사용하고있는 인자로이 함수를 보여 주지만 이터레이터는 BidiIterator라는 이름으로되어있다. 나는 이것의 중요성을 알지 못한다. 그러나 예를 들어, 나는 BidiIterator가 무엇이든 상관없이, 그것은 건설을위한 std :: string :: iterator를 취할 것이라고 가정한다. (아마도 나쁜 가정이지만, 예). 이 예제는 다섯 번째 인수 인 match_flags를 보여 주지만이 인수의 기본값은 boost :: match_default입니다. 따라서 불필요합니다. 그러나, 단지 킥과 웃음을 위해, 나는 다섯 번째 주장을 추가했지만 여전히 효과가 없습니다. 논쟁을 어떻게 잘못 사용합니까? 특히, 예를 고려할 때.

다음은 루핑 알고리즘없이 문제를 보여주는 간단한 프로그램입니다.

#include <iostream> 
#include <string> 

#include <boost/regex.hpp> 

int main() { 
std::string haystack("This is a string which contains stuff I want to find"); 
boost::regex needle("stuff"); 

boost::match_results<std::string::const_iterator> what; 
if(boost::regex_search(haystack.begin(), haystack.end(), what, needle, boost::match_default)) { 
    std::cout << "Found some matches" << std::endl; 
    std::cout << what[0].first << std::endl; 
} 

return 0; 
} 

당신이 컴파일하기로 결정했다면 나는 부스트 라이브러리의 1.47에 대해 컴파일하고 링크하고있다. 내가 작업하고있는 프로젝트는 광범위하게이 버전을 사용하며 업데이트는 내가 결정할 수있는 것이 아닙니다.

도움 주셔서 감사합니다. 이것은 가장 실망 스럽습니다.

앤디

답변

2

일반적으로 반복기의 유형이 다릅니다. begin()end()에서

std::string haystack("This is a string which contains stuff I want to find"); 

리턴 값 std::string::iterator 것이다. 하지만 검색 유형은

boost::match_results<std::string::const_iterator> what; 

std::string::iteratorstd::string::const_iterator 다른 종류입니다. 그래서 몇 가지 변종

에게 CONST로
  1. 선언 문자열 (즉 const std::string haystack;)
  2. 가 const_iterators로 반복자 (즉 std::string::const_iterator begin = haystack.begin(), end = haystack.end();)를 선언하고 regex_search에게 패스가있다.
  3. 사용 boost::match_results<std::string::iterator> what;
  4. 당신이 경우 C++ 11이 haystack.cbegin()haystack.cend()

example of work

+0

당신을 감사드립니다 사용할 수 있습니다! 나는 그 문제가 단순한 것이어야한다는 것을 알았다. 나는 그저 너무 많은 시간을 낭비했고 두 번째 눈이 필요했습니다. 필자는 함수의 매개 변수에서 "const std :: string ..."을 사용하는 예제를 완전히 간과하고있었습니다. 한가지 : 문자열 객체가 const 인 경우'myString.begin() '을 호출하면 const 반복자가 반환 될 것으로 기대합니다. 그러나, 실제로 작동하도록하려면'std :: string :: const_iterator begin (myString.begin());'을 사용해야했습니다. 왜 그런가요? –

+0

@AndrewFalanga 보시다시피 http://liveworkspace.org/code/57db9afe6464f944f2fecc4d8561ab91'const std :: string.begin()'은'const_iterator'를 반환합니다. – ForEveR

관련 문제