C ++使用字符串作为键和字符串矢量作为值创建映射

时间:2018-12-08 21:58:04

标签: c++

此代码提示用户询问其姓名和所就读的学校。将两者存储到地图中(将名称存储到向量中)。然后,我要以这种格式打印出学校以及每个就读该学校的人的姓名。

学校:名称,名称,名称。 / new line School:名称,名称,名称等。 。

我首先在Java中做过,并且尝试转换为c ++,不确定我是否正确执行此操作,也不确定如何将底部的for循环转换为c ++(是否有与java中的map.keySet()类似的东西)在c ++中?)

我不断收到emplace()错误,是我做错了还是需要#include?

int main() {

//Program that takes in users school that they attend and prints out the people that go there

string name;
string school;

//create the map
map<string, vector<string> > sAtt;



do {

    cout << "PLEASE ENTER YOUR NAME: (type \"DONE\" to be done" << endl;
    cin >> name;
    cout << "PLEASE ENTER THE SCHOOL YOU ATTEND:" << endl;
    cin >> school;

    if(sAtt.find(school)) {//if school is already in list

        vector<string> names = sAtt.find(school);
        names.push_back(name);
        sAtt.erase(school); //erase the old key
        sAtt.emplace(school, names); //add key and updated value

    } else { //else school is not already in list so add i

        vector<string> names;
        names.push_back(name);
        sAtt.emplace(school, names);

    }

}while(!(name == "done"));
sAtt.erase("done");

cout << "HERE ARE THE SCHOOLS ATTENDED ALONG WITH WHO GOES THERE: " << endl;


for (string s: sAtt.keySet()) { //this is the java way of doing it not sure how to do for c++
    cout << "\t" << s << sAtt.find(s) << endl;

    //should be School: name, name, name, etc. for each school

}

}

2 个答案:

答案 0 :(得分:0)

在C ++中,您可以使用迭代器浏览地图。

例如:

cout << "HERE ARE THE SCHOOLS ATTENDED ALONG WITH WHO GOES THERE: " << endl;

// Create iterator
map<string, vector<string> >:: iterator itr;

// Iterator
for (itr = sAtt.begin(); itr != sAtt.end(); ++itr){

         // Print school name
         cout << "School Name: " << itr -> first << endl;


         // Code that would go through vector and print contents

}

如果您想了解有关C ++中地图的更多信息,可以使用以下资源: https://www.geeksforgeeks.org/map-associative-containers-the-c-standard-template-library-stl/

答案 1 :(得分:0)

使用此代码,您应该会遇到许多错误。总的来说,这个项目看起来应该是用C ++从头开始编写的东西,而不仅仅是尝试从Java逐行翻译。考虑一下您想做什么,而不是如何复制Java。

例如,以下代码段应该做什么?

if(sAtt.find(school)) {//if school is already in list

    vector<string> names = sAtt.find(school);
    names.push_back(name);
    sAtt.erase(school); //erase the old key
    sAtt.emplace(school, names); //add key and updated value

} else { //else school is not already in list so add i

    vector<string> names;
    names.push_back(name);
    sAtt.emplace(school, names);

}

您可以逐行进行解释,但是整个过程是将name添加到与school关联的向量的末尾,并在需要时创建该向量。现在来看一下std::map::operator []。它返回(引用)与给定键关联的值,并在需要时创建该值。因此,更简单的方法是:

sAtt[school].push_back(name);

一行,并且与尝试将迭代器转换为布尔值或值类型没有任何问题。

关于从地图中获取值,您将遍历地图,而不是构造一组辅助值并对其进行遍历。出发点(并非完全是您想要的)是:

for ( auto & mapping : sAtt ) {           // Loop through all school -> vector pairs, naming each pair "mapping".
    cout << mapping.first << ":";         // The first element in the pair is the school name.
    for ( auto & name : mapping.second )  // Loop through the associated vector of names.
        cout << '\t' << name;             // Using tabs instead of commas.
    cout << endl;                         // End the school's line.
}

基本上,您应该记住std::map包含对,其中.first是键,而.second是值。