2012-01-23 1 views
2

저는 파이썬 사전을 C++으로 모방하려고합니다. 예를 들어, 다음과 같은 것을 만들고 싶습니다.다중 레벨 사전 작성 : 파이썬에서 C++ 로의 변환

{"The Dark Night Rises": {"year": 2012, "StoryLine": "this is the story.....", "Genres": ["action","crime","Thriller"]}} 

저는 이러한 종류의 사전을 구축하기 위해 STL 맵과 목록을 사용하고 있습니다. 그러나 iterators를 사용하는 방법을 잘 모르겠습니다. 위의 사전을 작성하는 예를 들어 도와 줄 수 있습니까? {"cast":["action","crime","Thriller"]}과 같은 간단한 사전을 만들기 위해 아래에서 시작했습니다. 위에서 언급 한 다단계 사전을 작성하는 방법을 혼란스럽게하고, 특별히 다단계 사전을 반복합니다.

#include<iostream> 
#include<string> 
#include<map> 
#include<iterator> 
#include<list> 

using namespace std; 

class MultiLevDict 
{ 
private: 
    list<string> lis; 
    map<string,list<string> > MultiDict; 
public: 
    void Setter(); 
    void Display() const; 
}; 

void MultiLevDict::Setter() 
{ 
    string field; 
    string cast; 
    int sizeCast; 
    cout<<"enter the field of the movie:"; 
    cin>>field; 
    cout<<endl; 
    cout<<"how many cast are there in this movie?:"; 
    cin>>sizeCast; 
    for (int i=0; i<sizeCast; i++) 
    { 
     cin>>cast; 
     lis.push_back(cast); 
    } 
    MultiDict[field]=lis; 

} 

void MultiLevDict::Display() const 
{ 
    list<string>::iterator lisIt; 

} 

답변

2

컨테이너를 반복하는 것은 begin()에서 end()까지 반복하는 것만큼 쉽습니다. 반복자 유형은 :: iterator 또는 :: const_iterator를 추가하여 컨테이너 유형에 따라 제공됩니다.

다음은 완전한 예입니다. 나는 당신의 코드를 고수하려고 노력했다. C++ 11의 새로운 구문에 대한 주석을 참고하십시오.

#include<iostream> 
#include<string> 
#include<map> 
#include<iterator> 
#include<list> 

using namespace std; 

class MultiLevDict 
{ 
private: 
    list<string> lis; 
    map<string,list<string> > MultiDict; 
public: 
    void Setter(); 
    void Display() const; 
}; 

void MultiLevDict::Setter() 
{ 
    string field; 
    string cast; 
    field="Abcd "; 
    lis.push_back("Tom"); 
    lis.push_back("Eve"); 
    MultiDict[field]=lis; 
// This works in C++11 : 
    MultiDict["Efgh "]={"Joe","Lisa"}; 

} 

void MultiLevDict::Display() const 
{ 
    for(map<string,list<string> >::const_iterator it=MultiDict.begin(); 
     it!=MultiDict.end();++it){ 
     std::cout << "key: was: "<<it->first<<std::endl; 
     for (list<string>::const_iterator it2=it->second.begin(); 
     it2!=it->second.end();++it2){ 
    std::cout << " "<<it->first<< " contains " <<*it2<<std::endl; 
     } 
    } 
} 

int main() { 
    MultiLevDict myd; 
    myd.Setter(); 
    myd.Display(); 
} 

결과가

key: was: Abcd 
    Abcd contains Tom 
    Abcd contains Eve 
key: was: Efgh 
    Efgh contains Joe 
    Efgh contains Lisa 
+0

'multimap'에'list's의'지도'를 선호에 대한 특별한 이유는? –

+0

@Zhenya. 나는 아무것도 신경 쓰지 않았다. –

+0

@ JohanLundberg. 지도에 여러 개의 데이터 유형을 저장할 수있는 방법을 묻는 질문으로 내 질문을 확장하고 싶습니다. 예를 들어 map >을이 맵에 선언하면 문자열의 키와 목록 유형의 값을 저장할 수 있습니다. 이제 { "someval": [ "val1"], "somval2" "여기에 문자열", "someval3": 24}. 나는 [CODE] [http://www.dreamincode.net/forums/topic/129708-c-map-with-multiple-data-types/] 링크를 발견했다. 그 링크를보고 나서 나는 >과 같은 것을 생각하고있다. – Rkz

0
void MultiLevDict::Display() const 
{ 
    for (auto x : MultiDict) 
     for (auto y : x.second) 
      cout << x.first << ": " << y << endl; 
} 
+0

with C++ 11, 예. –