STL map container



I currently have this ode but I am not sure how to do the following question.


#include <list>
#include <string>
#include <iostream>
using namespace std;


int main(int argv, char* argc[])
{
int i;
list<int> l;
list<string> s;
int counter = 0;
do {
if (counter % 2 == 0) //counter is even
{
//read int and store in list<int>
int num;
cin >> num;
l.push_back(num);

if (num == 0)
break;
}
else // counter is odd
{
//read string and store in list<string>
string str;
cin >> str;
s.push_back(str);
}

counter++;

} while (true);

}


Copy your answer above. Adapt it so that each corresponding int and string are used as a key-value
pair for insertion into an STL map container.

I know I need to use map<key,val> but I am unsure how to do it.
I have looked at the link but I am still not sure how to do it.

Also,I have shortened my code to
#include <list>
#include <string>
#include <iostream>
#include <map>
using namespace std;


int main(int argv, char* argc[])
{
int i;
list<int> l;
list<string> ls;
int counter = 0;
do {
cin >> i;
l.push_back(i);
cin >> s;
ls.push_back(s);
//map<l[0], s[0]>
} while (true);
Something like this

1
2
3
4
5
std::map<int, std::string> theMap;
theMap[0] = "First string";
theMap[1000] = "string at index 1000";

std::cout << "The first string is " << theMap[0] << std::endl;
The link has:
1
2
3
map<char,string> mymap;

mymap['a'] = "an element";

Lets change the type of the key:
1
2
3
map<int,string> mymap;

mymap[42] = "the answer";

Just in case your "more descriptive" variable names help:
1
2
3
map<int,string> l;

l[i] = s;

Topic archived. No new replies allowed.