0

I define

typedef std::map< int, std::set<int> > SparseMap;

Then I try to insert a pair in that way:

pair<SparseMap::iterator,bool> result;
result = sparseBlue.insert(SparseMap::value_type(row, set(col)) ); //errors
   if(result.second)
         cout << "Inserted" << endl;
  • row and col are integer matrix coordinates
  • sparseBlue is declared as SparseMap sparseBlue;

Why do I get those errors at the line in which I .insert?

rh0x
  • 1,066
  • 1
  • 13
  • 33

2 Answers2

2

I believe @T.C and @Lightness Races in Orbit had the right idea with std::set<int> being needed. The only problem is std::set<T> doesn't have a constructor that takes a constructor that takes a single item of type T (int in this case).

Assuming you actually need a set as the value in the map, then you'll probably want something like:

std::set<int> tmpSet;
tmpSet.insert(col);
result = sparseBlue.insert(std::make_pair(row, tmpSet));
ryzngard
  • 136
  • 7
  • That's it! It works also with `result = sparseBlue.insert(SparseMap::value_type(row, tmpSet));` – rh0x Jan 05 '16 at 19:53
  • In c++11 set has a constructor that takes an initializer list, so doing it without the separate step is certainly possible. – Jonathan Potter Jan 05 '16 at 22:10
  • oh yeah whoops. haha owned by 41 rep. +1 – Lightness Races in Orbit Jan 05 '16 at 22:32
  • @Jonathan Potter: Forgot my shift when I hit enter :) I think the initializer list must be constant at compile time. I'm not sure though, and it wasn't evident from looking at http://en.cppreference.com/w/cpp/utility/initializer_list. Would love to find this out, actually, since I always assumed the initializer list had to be constant. – ryzngard Jan 05 '16 at 22:48
  • 1
    @AndrewHall It works at least in some compilers: http://coliru.stacked-crooked.com/a/759a9b42998c40a5 – Jonathan Potter Jan 05 '16 at 22:54
  • @Jonathan Potter: You're absolutely right! The more you learn. I'm surprised this didn't work with rh0x's compiler. Thanks for showing me. – ryzngard Jan 05 '16 at 23:17
  • @AndrewHall you answer resolves the problems but I choose PaulMcKenzie's answer as correct because he gives me a very clear example on how to use map with set and this helps me a lot. – rh0x Jan 06 '16 at 09:33
1

Another solution is that you can insert into the map before you add items:

#include <map>
#include <set>

using namespace std;

int main() 
{
    int row = 0;
    int col = 0;
    std::map<int, set<int>> sparseBlue;

    // insert empty item
    auto iter = sparseBlue.insert(std::make_pair(row, std::set<int>()));

    // did a new item get inserted?
    cout << "The item did " << (iter.second?"":"not") << " get inserted\n";

    // add item to set 
    (*iter.first).  // the map iterator
           second.  // the set
           insert(col); // what we want to do 
}

The return value of std::map::insert returns a std::pair, denoting the iterator to the inserted item, and true or false depending on whether a new item was inserted.

Live Example

PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45