2017-10-13 13 views
-3
std::list<std::string> lWords; //filled with strings! 
for (int i = 0; i < lWords.size(); i++){ 
    std::list<std::string>::iterator it = lWords.begin(); 
    std::advance(it, i); 

는 지금은표준 : : 목록 <std::string> :: 반복자 성병 : : 문자열

std::string * str = NULL; 

    str = new std::string((it)->c_str()); //version 1 
    *str = (it)->c_str(); //version 2 
    str = *it; //version 3 


    cout << str << endl; 
} 

str을 문자열이어야합니다 (이 3 개 버전이 작동하지 않습니다) 새로운 문자열이 반복자가되고 싶어요 * 작동하지만 도움이 필요하지 않습니다!

+3

를 사용하는 포인터

나는 싶은 것은이 같은 생각? –

+1

귀하의 게시물에서 달성하고자하는 것이 분명하지 않습니다. 컴파일러 오류를 해결하는 것은 실제로 유용하지 않을 것입니다. –

+0

"새로운 문자열을 반복자로 사용하고 싶습니다"라는 의미는 무엇입니까? "나는 새로운 사과를 비행기로 만들고 싶다." –

답변

0

현대 C++에서는 값 또는 참조로 데이터를 참조하는 것이 좋습니다. 구현 세부 사항으로 필요하지 않으면 포인터로는 이상적이지 않습니다.

#include <list> 
#include <string> 
#include <iostream> 
#include <iomanip> 

int main() 
{ 
    std::list<std::string> strings { 
     "the", 
     "cat", 
     "sat", 
     "on", 
     "the", 
     "mat" 
    }; 

    auto current = strings.begin(); 
    auto last = strings.end(); 

    while (current != last) 
    { 
     const std::string& ref = *current; // take a reference 
     std::string copy = *current; // take a copy 
     copy += " - modified"; // modify the copy 

     // prove that modifying the copy does not change the string 
     // in the list 
     std::cout << std::quoted(ref) << " - " << std::quoted(copy) << std::endl; 

     // move the iterator to the next in the list 
     current = std::next(current, 1); 
     // or simply ++current; 
    } 

    return 0; 
} 

예상 출력 : 왜 당신이

"the" - "the - modified" 
"cat" - "cat - modified" 
"sat" - "sat - modified" 
"on" - "on - modified" 
"the" - "the - modified" 
"mat" - "mat - modified"