2014-04-21 6 views
0

아무도 반복기 문제로 나를 도와 줄 수 있습니까? 내가, 내가 구조체의 외부 (정의)지도 반복자를 만드는 문제에 봉착 언급 한 방법으로C++ typedef, map, iterator

class SomeClass{ 

public: 
    //Constructor assigns to m_RefPtr a new t_Records 
    //Destructor deletes m_RefPtr or decreases m_RefCnt 
    //Copy Constructor assigns m_RefPtr to new obj, increases m_RefCnt 
    bool Search(const string &); 

private: 
    //Some private variables 

    struct t_Records{ //For reference counting 

     int m_RefCnt; //Reference counter 

     typedef vector<int> m_Vec; 
     typedef map<string, m_Vec> m_Map; 
     m_Map m_RecMap; 

     t_Records(void){ 
      m_RefCnt = 1; 
     } 

    }; 

    t_Records * m_RefPtr; 

}; 

//Searchs through the map of m_RefPtr, returns true if found 
bool SomeClass::Search(const string & keyword){ 

    //How to create and use an iterator of m_Map??? 

    return true; 

} 

:이 같은 뭔가가 있어요. 지도는 초기화되어 있으며 일부 레코드가 포함되어 있습니다. 답장을 보내 주셔서 감사합니다. 이처럼

+0

을 더욱 쉽게처럼 반복 할 수 있습니다! –

+0

'auto'를 사용할 수 있습니까? – Deduplicator

+0

해당 디자인에서 잠재적 인 문제점을 제기 할 여러 출처가 있습니다. 이 시점에서 당신에 관해 무엇이 관련이 있습니까? 유형? 반복기를 반환하는 방법? 'm_RefPtr'의 평생 확장? * 내부 모든 요소에 대한 반복자 (예 : * 모든 * std :: vector ?) --BTW, 이것은 shared_ptr을 다시 구현하는 것처럼 보입니다. 표준 스마트 포인터를 사용하는 것이 좋습니다. 이. –

답변

3

: 그런데

// assuming m_RefPtr is properly initialized: 
t_Records::m_Map::iterator it = m_RefPtr->m_RecMap.begin(); 
++it; // etc. 

, m_Map은 유형에 대한 나쁜 이름입니다. 일반적인 규칙에 따라 접두사가 붙은 이름은 m_이며 데이터 멤버로 사용됩니다.

+0

소리 치기 : solution.cpp : 463 : 2 : 오류 :'m_Map'이 선언되지 않았습니다. m_Map :: const_iterator itMap; – marthin23

+0

@ marthin23 힌트 : 'typedef'는 어디에 있나요? – juanchopanza

+0

@ marthin23 죄송합니다. 중첩 된 구조체 정의를 알지 못했습니다. 결정된. – jrok

1

for (m_Map::iterator it = m_RecMap.begin(); it != m_RecMap.end(); ++it) 
{ 
    // do stuff with *it 
} 

또는`m_Map :: iterator`처럼

for (auto it = m_RecMap.begin(); it != m_RecMap.end(); ++it) 
{ 
    // do stuff with *it 
}