Exercise with a map

Asked

Viewed 57 times

-1

Create a map which has three keys ("Local", "Resp", "Prioridade"). Ask the user to fill in this map and then show the result on the screen.

#include <iostream>
#include <map>


using namespace std;

int main(){

    map<string,map<string,string>> alfa;

    string local;
    string prioridade;
    string responsavel;


    cout<<"Digite o local: ";
    cin>>local;
    cout<<"Digite a prioridade: ";
    cin>>alfa[local][prioridade];
    cout<<"Digite o responsável: ";
    cin>>alfa[local][responsavel];

     for(auto elem : alfa){
        cout << elem.first << " " << elem.second[prioridade]<< " " << elem.second[responsavel]<<endl;

    }
}
  • What is your question ?

1 answer

0

An Std::map has two fields: Key and Value. The key is the identifier, from which you can recover the value associated with that key.

The exercise says: crie um map com três chaves (“Local”, “Resp”, “Prioridade”). Therefore, you must create a map with 3 entries, whose keys are listed. The value associated with each key will be informed by the user.

Follow the corrected code.

#include <iostream>
#include <map>

using namespace std;

int main() {
   map<string,string> alfa;

   cout << "Digite o local: ";
   cin  >> alfa["Local"];
   cout << "Digite o responsavel: ";
   cin  >> alfa["Resp"];
   cout << "Digite a prioridade: ";
   cin  >> alfa["Prioridade"];

   for(auto& elem : alfa){
      cout << elem.first << ": " << elem.second << " " << endl;
   }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.