Home c++ Map Stl C++ container

Map Stl C++ container

Author

Date

Category

Let’s say in the map container one key stored. What will happen if we add another key with the same name as in the first key? Will an exception?


Answer 1, Authority 100%

Give advice – try to write a short test: you will find out, and you yourself attract a little:

# include & lt; iostream & gt;
#Include & lt; Map & GT;
TEMPLATE & LT; TYPENAME T1, TYPENAME T2 & GT;
Void Printmap (Const Std :: Map & LT; T1, T2 & GT; & amp; Container)
{
  STD :: COUT & LT; & LT; "CONTAINER SIZE =" & LT; & LT; container.size () & lt; & lt; STD :: ENDL;
  For (TypeName Std :: Map & LT; T1, T2 & GT; :: Value_Type Item: Container) {
    STD :: COUT & LT; & LT; "Key =" & lt; & lt; Item.First & LT; & LT; "Val =" & lt; & lt; item.second & lt; & lt; "";
  }
  STD :: COUT & LT; & LT; STD :: ENDL;
}
INT MAIN () {
  STD :: MAP & LT; STD :: STRING, INT & GT; MyMap;
  mymap.insert (std :: make_pair ("key1", 50));
  Printmap (MYMAP);
  MyMap.insert (Std :: Make_Pair ("Key1", 100));
  Printmap (MYMAP);
  MyMap ["Key1"] = 300;
  Printmap (MYMAP);
}

Result:

container size = 1
Key = Key1 Val = 50
CONTAINER SIZE = 1
Key = Key1 Val = 50
CONTAINER SIZE = 1
Key = Key1 Val = 300

It can be seen that Map :: Insert () will not change the value according to the existing key, Map :: Operator [] will restart

Previous articlejava. Use jar.
Next articleHow Git Sync

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions