构建多级字典:从 python 到 c++翻译

发布于 2024-12-28 13:52:40 字数 1161 浏览 0 评论 0原文

我正在尝试用 C++ 模仿 python 字典。例如,我想构建一些东西,比如

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

我正在使用 STL 映射和列表来构建这种字典。但我不确定如何使用迭代器。有人可以帮我举一个构建上述字典的例子吗?我开始像下面这样构建一个简单的字典,例如 {"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;

}

I'm trying to mimic the python dictionary in C++. For example I want to build something like

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

I'm using the STL map and lists for building up this kind of dictionary. But I'm not sure how to use the iterators. Can some one help me with an example of building the above dictionary. I started something like below to just build a simple dictionary like {"cast":["action","crime","Thriller"]}. I'm confused how to build the above mentioned multilevel dictionaries, and specially iterating over them.

#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;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

妞丶爷亲个 2025-01-04 13:52:41

循环容器就像从 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

Looping over a container is as easy as iterating from begin() to end(). The iterator type is provided by the type of the container by adding ::iterator or ::const_iterator.

Here's a complete example. I tried to stick to your code. Note the comment about the new syntax in 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();   
}

the result is

key: was: Abcd 
  Abcd  contains Tom
  Abcd  contains Eve
key: was: Efgh 
  Efgh  contains Joe
  Efgh  contains Lisa
≈。彩虹 2025-01-04 13:52:41
void MultiLevDict::Display() const
{
    for (auto x : MultiDict)
        for (auto y : x.second)
            cout << x.first << ": " << y << endl;
}
void MultiLevDict::Display() const
{
    for (auto x : MultiDict)
        for (auto y : x.second)
            cout << x.first << ": " << y << endl;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文