c++ - Obtain iterator upon adding to std::map? -


Is there a way to get an Iterator while working with the standard library map container, which is the whole container?

I have a management class for a map, and I want to return the iterator connected to the items added to the map. I do not want to believe in finding if I can avoid searches then I think better.

  std :: map & lt; Char, bool & gt; :: iterator category :: add (four items) {mymap [item] = false; Return mymap.get_iterator_lastitem (); }  

Maybe

  mymap.end () - 1;  

If you are not using C ++ 11, then

  std :: map & lt; Char, bool & gt; :: iterator ClassA :: Add (four items) {std :: pair & lt; Std :: map & lt; Char, bool & gt; :: iterator, bool & gt; Result = mymap.insert (std :: make_pair (object, incorrect)); If (! Result.second) {// item already exists, modify that existing item result.first-> gt; Second = false; } Return Results. Firstly; }  

If you are using C ++ 11 then it is better to use emplace + auto .

  std :: map & lt; Char, bool & gt; :: Iterator Class A :: Add (four items) {auto result = mymap.emplace (items, incorrect); If (! Result.second) {// item already exists, modify that existing item result.first-> gt; Second = false; } Return Results. Firstly; }  

both insert and emplace one pair of an iterator and a boolean back, of which Indicator is inserted or points to the existing element and the boolean indicates that an insertion ( true ) occurred or not ( false ) from which the first Iterator was already- Current element with key


Comments

Popular posts from this blog

Member with no value in F# -

java - Joda Time Interval Not returning what I expect -

c# - Showing a SelectedItem's Property -