Solved

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

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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.

685 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