Solved

Printing the elements of a set declared inside a map in C++.

Posted on 2016-10-02
3
67 Views
Last Modified: 2016-10-02
Hi all, I am trying to print the elements of the set declared inside a map in c++. However, the code isn't printing the elements. I have this code :
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <map>
#include <utility>
#include <stdio.h>
#include <ctype.h>
#include <typeinfo>
#include <string.h>
#include <vector>
#include <algorithm>
#include <set>



using namespace std;

int main()
{
	std::set<string> st;
	std::map<string,std::set<string> > mp;
	std::map<string,std::set<string> >::iterator row;
	std::set<string>::const_iterator col;

	mp["A"].insert("pawan");
	mp["A"].insert("patil");

	
	for (row = mp.begin(); row!=mp.end(); row++) 
	{
		cout<<row->first;
    	for (col = row->second.begin(); col!=row->second.end(); ++col);
    	{
    		cout<<*col<<" ";
    	}
    	cout<<mp["A"].size()<<"\n";
	}
	//cout<<row->second;
	
	return 0;
}

Open in new window

0
Comment
Question by:Pawan Patil
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 32

Accepted Solution

by:
phoffric earned 500 total points
ID: 41825630
I would remove the ; on line 32 of the for-loop. The ; at the end of the for-loop will result in getting to the end of the set and when you try to dereference col, you  no longer a valid iterator.

(I admit that seeing those little ; on a smudged tablet is a little hard to see.)
1
 
LVL 32

Assisted Solution

by:phoffric
phoffric earned 500 total points
ID: 41825633
Looking a little more, I am surprised that you don't get compiler errors.
In any case, remove the included superfluous .h header files; and especially replace:
#include <string.h>
with
#include <string>
1

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question