how to iterate through a vector within a map

I would like to iterate through a vector within a map, but for some reason I am getting no operator matches these operands "<<" on line 17.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
  # include <iostream>
# include <map>
# include <vector>

using namespace std;

int main()
{
	map <string, vector<int>> grades = { {"Peter" , {1,2,3}} ,{"Glen" , {44, 55}} };


	map <string, string> names = { { "Peter" , "abb"} , {"glen" , "ccc" } }; 


	for (const auto& pair : grades)
	{
		std::cout << "key: " << pair.first << "  value: [  ";
		for (std::size_t i = 0; i < pair.second.size(); ++i)  std::cout << pair.second[i] << "  ";
		std::cout << "]\n";
	}




}



You program seems to work fine in terms of printing out the elements of the container 'grades' but what you probably want to do is to search for a name in 'names' and print out the grades of this name (if present) in grades:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# include <iostream>
# include <map>
# include <vector>
# include <string>

//using namespace std;
//https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice

int main()
{
	std::map <std::string, std::vector<int>> grades = { {"Peter" , {1,2,3}} ,{"Glen" , {44, 55}} };


	std::map <std::string, std::string> names = { { "Peter" , "abb"} , {"glen" , "ccc" } };


	for (const auto& elem : names)
    //avoid variable names like pair that're already in use by the standard
	{
        auto itr = grades.find(elem.first);
        if (itr != grades.end())
        {
            std::cout << "Grades for " << (*itr).first << " : ";
            for (const auto& elem : (*itr).second)
            {
                std::cout << elem << " ";
            }
        }
        else
        {
            std::cout << "name " << elem.first << " not found in grades";
        }
        std::cout << "\n";
	}
}
Last edited on
#include <string>


PS: ¿why all that whitespace?
Last edited on
well...imo it is neat
I am still getting the error on line 17
well, if you don't show your program how can anyone tell what's on line 17?
as mentioned above, the OP might be poorly written but it compiles and runs:
http://coliru.stacked-crooked.com/a/ffe35526e27238f1
so the following statement is not correct:

but for some reason I am getting no operator matches these operands "<<" on line 17.
My first suggestion is to change the name of the "iterator" in your ranged based loop to something other than pair, there is a std::class with that name and since you're using the "using" statement you'd be best to avoid name conflicts.

Second what exactly is your error message?

Are you positive that your compiler is using the C++11 or higher standard?

I get this output with the program following (compiled using the C++14 standard):

key: Glen value: [ 44 55 ]
key: Peter value: [ 1 2 3 ]


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <map>
#include <vector>

using namespace std;

int main()
{
	map <string, vector<int>> grades = { {"Peter" , {1,2,3}} ,{"Glen" , {44, 55}} };

	for (auto& key : grades)
	{
		std::cout << "key: " << key.first << "  value: [  ";
		for(auto& grade : key.second)
                   std::cout << grade << "  ";
		std::cout << "]\n";
	}
}



Last edited on
when i build it , i am getting an error at the line:

std::cout << "key: " << key.first << " value: [ ";

error C2679:binary '<<': no perator found which takes a right - hand operand of type
onst std string ( or there is no acceptable conversion)
Assuming that is the only warning or error the next step would be to find what part of that cout statement is the problem. You do this by placing each statement on it's own line.
1
2
3
std::cout << "key: ";
std::cout << "key.first;
std::cout << " value: ["; 


By the way the program compiled and ran successfully for me using g++ and compiling for the C++14 standard. What version of Visual C++ are you using?

Topic archived. No new replies allowed.