-3

Lets say I have a complex map defined as

std::map<int, std::pair< vector<int>, float> > complex_map;  

Let us assume I initialized this map as

for (int k=0;k<5;k++)
{
    std::pair< vector<int>, float> complex_map_child;
    complex_map[k]=complex_map_child;
}

Next, I populate some entries of this map:

float test_1 = .7888;
vector<int> test_2;
test_2.push_back(1);
test_2.push_back(2);
complex_map[1].first = test_2;
complex_map[1].second = test_1;

So corresponding to key value 1 of complex_map, I have the pair of values corresponding to test_1 and test_2.

Now how do I check if I have explicitly added values to the map? i.e in this example how do I say that I havent explicitly filled up say complex_map[0]?

suzee
  • 563
  • 4
  • 25
  • I do not understand what you are asking. – Barry Jul 23 '15 at 16:37
  • changed to better reflect the question! – suzee Jul 23 '15 at 16:37
  • 1
    `Now how do I check if I have explicitly added values to the map? or that they are in the original initialised state?`??? When you crate a map there are no values in it. Are you trying to ask on how you can tell if a certain key exist in the map? – NathanOliver Jul 23 '15 at 16:39
  • Maybe map::insert, which returns a pair, where the bool indicates an insertion (change) –  Jul 23 '15 at 16:46
  • You have to better define what you mean by "default". Why did you add 0-4 elements in the first place if you don't want them? – Lightness Races in Orbit Jul 23 '15 at 16:58
  • I dont understand your question, if you want to know if the map was having values when you initialized it in the for loop, then try to print them – Mido Jul 23 '15 at 17:04

2 Answers2

0

Now how do I check if I have explicitly added values to the map? i.e in this example how do I say that I havent explicitly filled up say complex_map[0]?

If by "explicitly" you mean you want to find elements that you wrote to after the initialisation loop that executed complex_map[k]=complex_map_child;, then:

  • you can compare values in the map to complex_map_child to see if they're equal

  • you can not detect if a map entry was written to with the same value, or changed then reverted (unless you change the data types to ones that track that themselves, or add some extra bookkeeping outside the map yourself)

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
0

Looks like you are using std::map::operator[] improperly and try to go over it - you are getting object like this:

auto &complex_value = complex_map[0];

and now you try to check if you inserted it before or it was implicitly created by std::map::operator[].

Just do not use std::map::operator[] to access elements. Only use it when you need to set value in a map.

So proper solution would be to use different methods:

// I just want to check if key 0 is there
if( complex_map.count( 0 ) ) {
     ...
}

// I want to access element by key 0 if it is there
auto it = complex_map.find( 0 );
if( it != complex_map.end() ) {
    auto &complex_value = it->second;
    ...
}

and so on. I know that writing complex_map[0] is shorter but you are creating a problem that trying to solve convoluted way.

Slava
  • 43,454
  • 1
  • 47
  • 90