2017-12-06 1 views
-1

아래 코드는 regex_token_iterator에서 std :: vector로 값을 복사하지 못합니다. Visual Studio 2015에서는 매개 변수가있는 'std :: copy'가 안전하지 않을 수 있다고보고합니다.std :: regex_token_iterator를 std :: vector에 복사하는 방법은 무엇입니까?

누구나 수정 방법을 알고 계십니까?

#include <vector> 
#include <iostream> 
#include <algorithm> 
#include <iterator> 
#include <regex> 

int main() 
{ 
    // String to split in words 
    std::string line = "dir1\\dir2\\dir3\\dir4"; 

    // Split the string in words 
    std::vector<std::string> to_vector; 
    const std::regex ws_re("\\\\"); 
    std::copy(std::sregex_token_iterator(line.begin(), line.end(), ws_re, -1), 
       std::sregex_token_iterator(), 
       std::back_insert_iterator<std::vector<std::string>>(to_vector)); 

    // Display the words 
    std::cout << "Words: "; 
    std::copy(begin(to_vector), end(to_vector), std::ostream_iterator<std::string>(std::cout, "\n")); 
} 
+1

가 관련이 있는지 내가 확실히 모르겠어요,하지만 당신은 필요한 함수 벡터를 제공하고 있습니다 반복자. – chris

+0

고마워, 나는 벡터에서 삽입 반복기를 전달하는 방법을 탐색 할 것이다. –

+1

벡터가 비어 있습니다. back_inserter를 사용해야합니다. –

답변

0
다음

벡터에 regex_token_iterator에서 추출 된 값을 저장하기 위해 내 솔루션 :

#include <vector> 
#include <iostream> 
#include <algorithm> 
#include <iterator> 
#include <regex> 

int main() 
{ 
    std::string s("dir1\\dir2\\dir3\\dir4"); 

    // Split the line in words 
    const std::regex reg_exp("\\\\"); 
    const std::regex_token_iterator<std::string::iterator> end_tokens; 
    std::regex_token_iterator<std::string::iterator> it(s.begin(), s.end(), reg_exp, -1); 
    std::vector<std::string> to_vector; 
    while (it != end_tokens) 
    { 
     to_vector.emplace_back(*it++); 
    } 

    // Display the content of the vector 
    std::copy(begin(to_vector), 
       end(to_vector), 
       std::ostream_iterator<std::string>(std::cout, "\n")); 

    return 0; 
} 
관련 문제