-1

I have 2 std::multiset

multiset<myObject *,dfa_ptr_states_less> m1;
multiset<myObject *,dfa_ptr_states_less> m2;

where I defined in an other file (myObject.h ) the Comparison function

struct dfa_ptr_states_less
{
   bool operator()(const myObject *l, const myObject *r) const
   {
       return ( l->get_num_states() < r->get_num_states() );
   };
};

where get_num_states is a field of myObject class.

m2 is empty. m1 point to dynamics objects. I have to do a copy of m1 in m2,namely a shallow copy of pointers from m1 to m2.

for(auto &ptr : m1)
   m2.insert(ptr); 

Simple but my doubt is: The order of elements in m1 is preserved in m2? If not, how can I do? (For my purposes the order is very important)

UPDATE:
Maybe is it possible do:

m2=m1

?

Nick
  • 1,439
  • 2
  • 15
  • 28
  • Possible duplicate of [Does std::multiset guarantee insertion order?](https://stackoverflow.com/questions/2643473/does-stdmultiset-guarantee-insertion-order) – underscore_d Aug 03 '17 at 14:39
  • Re your update, yes, that would make more sense. Have you tried it? See also http://en.cppreference.com/w/cpp/container/multiset `The order of the elements that compare equivalent is the order of insertion and does not change. (since C++11)` – underscore_d Aug 03 '17 at 14:41
  • @underscore_d Yes, now I tried it. It works – Nick Aug 03 '17 at 16:33

1 Answers1

3

Yes, the order is preserved on copy (as well as on iteration and insertion).

Yes, multiset supports copy assignment.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • And what happen to myObjects pointers? I want a shallow copy. Does operator= do this? – Nick Aug 03 '17 at 14:44
  • 2
    @Umbert Nothing happens to them. Copying does not modify the source object. There is also no difference between a shallow and deep copy of a pointer (or any fundamental type). – eerorika Aug 03 '17 at 14:46