Solved

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

Posted on 2016-10-02
3
46 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
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
TTimer no longer functioning 7 90
FMX StringGrid1->Canvas->FillRect Problem 3 150
c++ syntax question 9 47
How can i compile this github project?? 2 82
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

815 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now