Solved

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

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
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.

757 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

22 Experts available now in Live!

Get 1:1 Help Now