Map of sets?

This is a discussion on Map of sets? within the C++ Programming forums, part of the General Programming Boards category; Hi, I am trying to create an std::map, one of its elements is an std::set: Code: string key = "a1"; ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    14

    Map of sets?

    Hi,

    I am trying to create an std::map, one of its elements is an std::set:

    Code:
    string key = "a1";
    set<int> setone;
    
    map<string,set<int>> mapname;
    
    setone.insert(1);
    setone.insert(2);
    setone.insert(3);
    
    mapname.insert(key,setone);
    I'm unable to compile this, the insert() function isn't accepting the std::set as a parameter type.

    How should I create a map which has std::sets as the second element type?



    Many thanks for any advice

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Why not just use the most common operator[] indexation technique:
    mapname[key] = setone;
    Or, if you won't use setone anymore from there, you can optimize this into:
    mapname[key].swap(setone);
    As for why it doens't work; because the function isn't supported. See here for a list of supported insert functions: http://www.sgi.com/tech/stl/Map.html

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    14
    Thanks Evo for pointing that out.

    Here is my full program:

    Code:
    #include <iostream>
    #include <map>
    #include <set>
    using namespace std;
    
    int main()
    {
    	map<string,set<int>> mapname;
    	set<int> setname;
    
    	setname.insert(1);
    	setname.insert(2);
    	setname.insert(3);
    
    	string key = "a1";
    
    	mapname[key] = setname; //compile error here
    
    	system ("pause");
    	return 0;
    }
    I cannot get it to compile, it says because of the type. I'm confused on how to assign the set as the maps element.

    Much appreciated

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    As a general rule it would be better to post the exact error message rather than paraphrasing something like "it says because of the type". Also, are you sure about which line is causing your error?

    I think it's this one:
    Code:
    	map<string,set<int>> mapname;
    You need a space between the angle brackets on the right side. It should be:
    Code:
    	map<string,set<int> > mapname;

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    14
    My mistake, sorry for that.

    I added the space, visual studio gives the following when I try to compile:

    1>c:\program files (x86)\microsoft visual studio 9.0\vc\include\functional(143) : error C2784: 'bool std:perator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)' : could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const std::string'
    1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\xtree(1466) : see declaration of 'std:perator <'
    1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\functional(142) : while compiling class template member function 'bool std::less<_Ty>:perator ()(const _Ty &,const _Ty &) const'
    1> with
    1> [
    1> _Ty=std::string
    1> ]
    I understand a std::set is implemented as a tree, could this be affecting things?

    EDIT:

    I tried using a typedef:

    Code:
    #include <iostream>
    #include <map>
    #include <set>
    using namespace std;
    
    int main()
    {
    	typedef map<string,set<int> > maptype;
    	maptype mapname;
    
    	set<int> setname;
    
    	setname.insert(1);
    	setname.insert(2);
    	setname.insert(3);
    
    	string key = "a1";
    
    	mapname[key] = setname; 
    
    	system ("pause");
    	return 0;
    }
    But still it fails:

    1>c:\program files (x86)\microsoft visual studio 9.0\vc\include\functional(143) : error C2784: 'bool std:perator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)' : could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const std::string'
    1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\xtree(1466) : see declaration of 'std:perator <'
    1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\functional(142) : while compiling class template member function 'bool std::less<_Ty>:perator ()(const _Ty &,const _Ty &) const'
    1> with
    1> [
    1> _Ty=std::string
    1> ]
    Last edited by Orange Lozenge; 01-23-2011 at 09:56 AM.

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    Beats me. I have no problem compiling it with g++, except for the missing space between the brackets, and the system("pause").

    Maybe it's a bug in msvc++.

    Edit: Oh, I just noticed that you didn't #include <string>. I'll bet that's the problem.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    14
    Yey it works now! I was stuck for ages trying to create this type structure.

    Hey thanks so much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 47
    Last Post: 07-13-2010, 07:22 PM
  2. Problem aligning floating point numbers
    By esbo in forum C Programming
    Replies: 4
    Last Post: 01-05-2009, 07:09 PM
  3. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  4. Creating a map engine.
    By suzakugaiden in forum Game Programming
    Replies: 11
    Last Post: 06-21-2005, 05:06 AM
  5. creating new sets
    By axon in forum C++ Programming
    Replies: 7
    Last Post: 12-03-2003, 05:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21