0

I have a class Normal defined as:

    class Normal
    {
        bool value;
        float time;
        public:
            Normal(bool val,float time): value(val),time(time) {}
    }

Also, I have declared a map variable as:

    map<string,Normal> myMap;

Now I want to insert an data into this map. Is this way of inserting correct?

    Normal temp(true,45.04);
    myMap.insert(pair<string,Normal>("one",temp));

or

    myMap["one"]=temp;

How should i insert data into the map?

2 Answers2

4

In C++03 :

myMap.insert(std::make_pair(
    "one",
    Normal(true, 45.04)
));

In C++11 :

m.emplace(std::piecewise_construct,
          std::forward_as_tuple("one"),
          std::forward_as_tuple(true, 45.04)
);

Both avoid default-constructing a key-value pair inside operator[] and then overwriting it.

Quentin
  • 62,093
  • 7
  • 131
  • 191
-2

Use this code

Normal *temp =  new Normal(true,45.9);
mymap.insert(make_pair("one",temp));

avoid shallow copy since pointer is involved.

EDIT: Use insert function to insert data in map. Index is not the best way. specially when u r accessing See this link for details In STL maps, is it better to use map::insert than []?

EDIT2: For deletion,use the below code.

for(std::map<string, Normal*>::iterator itr = mymap.begin();it !=  mymap.end();)
{
  if(it->second != NULL)
   {
      delete (it->second);
      (it->second) = NULL;
      it=mymap.erase(it);
    }
    else
    {
         ++it;
    }

  }
Community
  • 1
  • 1
Spanky
  • 1,070
  • 6
  • 11
  • 1
    There's nothing in OP's code that suggests they need dynamic allocation (not even before the edit.) A pointer does not automatically mean dynamic allocation. – juanchopanza Jun 09 '15 at 09:36
  • @juanchopanza : i fully agree with you. But if you are using pointer, your better off creating dynamic ones. I prefer it that way. :) – Spanky Jun 09 '15 at 09:38
  • @juanchopanza : Can you please explain in detail with an example. It will help me a lot. – Spanky Jun 09 '15 at 09:40
  • @Spanky you are not deleting the element from map, just updating the elem.second as null after deleting the elem.second. Map size will not change after your deletion. – Steephen Jun 09 '15 at 09:46
  • @Steephen : yeah!!! thats a good point. Edited the code to add erase. 1 Question here...mymap.clear(), will it not remove all the entries? – Spanky Jun 09 '15 at 09:49
  • @Spanky it will clear the map and lose all of the pointers. Leak ! – Quentin Jun 09 '15 at 09:51
  • Your usage of erase is wrong, it will invalidate iterator. map::clear() is going to remove all elements from map. did you test your code? – Steephen Jun 09 '15 at 09:52
  • @Steephen : if I delete the pointer and then clear the map as I have done above. Then there should not be any leak. Am I correct? ignore the erase for a moment. – Spanky Jun 09 '15 at 09:56
  • @Spanky No. When you clear you are going to loose all elements of map not only the one you deleted. – Steephen Jun 09 '15 at 09:58
  • @Steephen: So erase should be better option. my code should free the pointer and then erase the element. clear should not be done at all. correct? – Spanky Jun 09 '15 at 10:04
  • You should use erase properly, and clear is not needed to delete an element from a map. – Steephen Jun 09 '15 at 10:06
  • @Steephen : Ok!! thanks a lot. good learning for me. Is the erase I have written is wrong? If yes , then can you just write the erase properly. – Spanky Jun 09 '15 at 10:12
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/80036/discussion-between-spanky-and-steephen). – Spanky Jun 09 '15 at 10:25
  • This URL may help you to understand more details :http://stackoverflow.com/questions/5191644/removing-elements-from-a-c-map-through-a-for-loop – Steephen Jun 09 '15 at 10:34