加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 如何使用地图矢量

发布时间:2020-12-16 10:12:21 所属栏目:百科 来源:网络整理
导读:vector mapstring,string dictionary; map string,string word1; map string,string word2; word1.insert(pairstring,string("UNREAL","Abc")); word2.insert(pairstring,string("PROPS","Efg")); dictionary.push_back(word1); dictionary.push_back(word2)
vector <map<string,string>> dictionary;
    map <string,string> word1;
    map <string,string> word2;

    word1.insert(pair<string,string>("UNREAL","Abc"));
    word2.insert(pair<string,string>("PROPS","Efg"));

    dictionary.push_back(word1);
    dictionary.push_back(word2);

    vector<map<string,string>>::iterator it;
    it = dictionary.begin();

    for( it; it != dictionary.end(); it++)
    {
                cout << it << " " << it << endl; //ERROR
    }

我想显示存储在矢量中的数据.请建议我如何显示矢量字典的输出?

解决方法

为了解决您的问题,您应该这样做:

for(it = dictionary.begin(); it != dictionary.end(); it++){
    for(auto it1=it->begin();it1!=it->end();++it1){
        cout << it1->first << " " << it->second << endl; 
    }
}

但是,我认为这个设计有问题.在你的情况下,你不需要地图矢量……你需要成对的矢量或只需要一张地图.

对矢量:

std::vector <std::pair<string,string>> dictionary;
dictionary.emplace_back("UNREAL","Abc");
dictionary.emplace_back("PROPS","Efg");
for(auto const& item:dictionary){
    std::cout << item.first << " " << item.second;
}

地图:

std::map<string,string> dictionary;
 dictionary.insert("UNREAL","Abc");//also :  dictionary["UNREAL"]="Abc";
 dictionary.insert("PROPS","Efg");//also :  dictionary["PROPS"]="Efg";
 for(auto const& item:dictionary){
     std::cout << item.first << " " << item.second;
 }

因为地图不仅仅是一对东西,它是一组对(有些不精确).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读