【问题标题】:C++ creating a map with a string as key and vector of string as value [closed]C ++创建一个以字符串为键,字符串向量为值的映射[关闭]
【发布时间】:2019-05-10 06:32:39
【问题描述】:

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

学校:名字,名字,名字。 /new line School : name, name , name etc. . . .

我第一次在 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

}

}

【问题讨论】:

  • C++ Loop through Map的可能重复
  • 你确定你在emplace而不是vector&lt;string&gt; names = sAtt.find(school);得到编译器错误吗? emplace 用法对我来说很好。
  • 很抱歉,但是通过从 Java 转换代码来学习 C++ 肯定是一件非常头疼的事情。例如,if(sAtt.find(school)) 始终为真,无论 schoolsAtt 中的内容是什么。我推荐a good C++ book从基础学习C++。
  • @Yksisarvinen for if 语句将 if(sAtt.count(school)) 代替工作
  • IMO,更多的 C++ -ish 方法是将其与end 迭代器:if(sAtt.find(school) != sAtt.end()) 进行比较。 count 会起作用,因为它总是返回 1 或 0,但它看起来很奇怪。

标签: c++


【解决方案1】:

您应该在这段代码中遇到很多错误。总的来说,这个项目看起来应该是你应该用 C++ 从头开始​​编码的东西,而不是简单地尝试从 Java 中逐行翻译。想想你想做什么,而不是如何复制 Java。

例如,下面的代码 sn-p 应该做什么?

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 是值。

【讨论】:

    【解决方案2】:

    在 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/

    【讨论】:

    • 最好使用auto 而不是map&lt;string, vector&lt;string&gt; &gt;:: iterator。最好使用 range based for 而不是传统的循环。如果C++17 是一个选项,使用结构化绑定会更好
    • 你将如何打印与键一起使用的值
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-22
    • 2021-08-30
    • 1970-01-01
    • 1970-01-01
    • 2017-10-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多