Hi
I m trying to create a map of STL which has set as one of it's element.
Following is snippet of my code

..
std::map<std:string,std::set<int,int> > map_of_set;
int a=9;
int b=10;
std::string str="test";
map_of_set.clear();
map_of_set.insert(test,9,10); //This gives compilation error

kindly help

Recommended Answers

All 6 Replies

I m really sorry abt the basic mistake in the errorous code the correct code is :-

..
std::map<std:string,std::set<int,int> > map_of_set;
int a=9;
int b=10;
std::string str="test";
map_of_set.clear();
map_of_set.insert(str,a,b); //This gives compilation error

No idea as to how i should insert the values in the set which is a part of map

Was that a statement, or a question?

Maybe

std::set<int,int> tobermory(a,b);
map_of_set.insert(str,tobermory);

I'll leave you to figure out the detail of the syntax.

How to create a set which is two dimensional for eg

std::set<int,int> myset;
myset.insert(10,9);//this gives compilation error

kindly help

Hi here is my code that i think could be the solution to your problem. It compiles using g++ in my ubuntu boot:

.....
#include <string>
#include <set>
#include <map>
#include <utility>   //Dont forget this - U need it for using the "pair"
.....
std::string str1="test1";
std::string str2="test2";
std::string str3="test3";
 
int a=8;
int b=9;
int c=0;
int d=a+b;
int e=12; 

//Lets make our lives easier
typedef std::pair<int,int> pairOfInt;

std::map<std::string,pairOfInt> map_of_set;
 // map_of_set.first=["hallo",a];
map_of_set[str1]=pairOfInt(a,b);
map_of_set[str2]=pairOfInt(c,d);
map_of_set[str3]=pairOfInt(e,a);

std::cout<<"test1 maps to: a="<< map_of_set[str1].first<<" and b=" << map_of_set[str1].second<<std::endl;
std::cout<<"test2 maps to: c="<< map_of_set[str2].first<<" and d=" << map_of_set[str2].second<<std::endl;
std::cout<<"test3 maps to: e="<< map_of_set[str3].first<<" and a=" << map_of_set[str3].second<<std::endl;

//Note: with .first and .second u have access to the 1st and 2nd item of the "pair" respectively

The output it gives is:

test1 maps to: a=8 and b=9
test2 maps to: c=0 and d=17
test3 maps to: e=12 and a=8

I hope I helped u.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.