std::map

Can someone please confirm if I understand the following code correctly and help me out where I am unable to understand? Thank you.

1
2
3
4
5
6
7
inline void CountFish(int fishID, string fishName)
{
std::map<string, std::set<int> > fishMap; //line 1
std::set<int> fishIdSet = fishMap[fishName]; //line 2
std::pair<std::set<int>::iterator, bool> counted = fishIdSet.insert(fishID); // line 3
fishMap[fishName]=fishIdSet; // line 4
}


- line 1: Here a map called fishMap is declared where it shall have keys of type string and the associated key values of type int?

- line 2: In the fishMap, a key element called fishName with a value of fishIdSet is set? I hope my understanding of the [] is correct.

- line 3: A new value fishId is compared with the previous value fishIdSet and either 1 or 0 is set into the variable counted? But what is the point of this like in the code anyway? As I see it the counted variable is not used anywhere?

- line 4: The associated fishIdSet is again assiciated to the key fishName in the map fishMap? But what is the point of this when already this was done in line 2, assuming my understanding is correct? Or is it that somehow from line 3, a new value is associated to the fishName if the fishId was different than fishIdSet?


Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// key_type: string    mapped_type: set of int
std::map< std::string, std::set<int> > fishMap { { "carp", { 5, 2, 7 } }, { "pike", { 22, 7, 6, 9 } } } ;

// key "pike" exists, get a reference to the mapped value (set of int)
auto& fishIdSet = fishMap[ "pike" ];

// key "salmon" does not exist, inset the pair { "salmon", empty set of int } into the map
// get a reference to the mapped value (empty set of int)
auto& fishIdSet2 = fishMap[ "salmon" ];

// key "carp" exists, insertion fails. return pair { iterator, false }
// pair.second == false (not inserted)
const auto pair = fishMap.insert( { "carp", { 1, 2, 3, 4 } } ) ;

// key "minnow" does not exist, insert pair { "minnow", { 1, 2, 3, 4 } } into the map
// return pair { iterator, true } pair2.second == true (inserted)
const auto pair2 = fishMap.insert( { "minnow", { 1, 2, 3, 4 } } ) ;

// key "pike" exists, get a reference to the mapped value (set of int), assign set { 1, 2, 3, 4 } to it
// at the end, key == "pike", mapped value == set { 1, 2, 3, 4 }
fishMap[ "pike" ] = { 1, 2, 3, 4 } ;

// key "mackerel" does not exist, inset the pair { "salmon", empty set of int } into the map
// get a reference to the mapped value (empty set), assign set { 5, 6, 7 } to it
// at the end, key == "mackerel", mapped value == set { 5, 6, 7 }
fishMap[ "mackerel" ] = { 5, 6, 7 } ;
What exactly are you trying to accomplish?

Unless this is just a snippet of your actual code and there are more lines the code provided doesn't make much sense to me. You're creating a new empty std::map<string, std::set<int>>. Next you try to copy a non-existing element from your std::map to create fishIdSet. Next the only thing being done on line 3 is inserting an integer into your empty fishIdSet, so this set now has one element, the variable counted is never used so it seems useless to me. Now you insert the fishIdSet into your map using the fishName as a key. Lastly the function returns to the calling function and you lose all the information you tried so hard to create because all of those local variables are destroyed when the function returns.



Last edited on
Topic archived. No new replies allowed.