-4

In C++:

  • A list is a collection that can contain non-unique values in sequence

  • A multiset is a collection that can contain non-unique elements in sequence

What then is the specific difference between the two? Why would I use on over the other?

I've tried finding this information online but most references (e.g. cplusplus.com) talk about the two containers in different ways, such that the difference is not apparent.

Toby
  • 9,696
  • 16
  • 68
  • 132
  • 1
    std::multiset is sorted – Joseph D. Apr 27 '18 at 14:26
  • In life bicycle is a device that has wheels and a car is a device that has wheels. I cannot find article that would describe specific difference btw these two :( – Slava Apr 27 '18 at 14:28
  • @Slava, the difference will be how many persons would use it. :) – Joseph D. Apr 27 '18 at 14:31
  • 1
    Did these descriptions come from something you're reading? If so, read on. They are fundamentally different, and any good descriptive text will tell you what they're used for. – Pete Becker Apr 27 '18 at 14:49
  • @PeteBecker The descriptions came from my summary of the related cplusplus.com descriptions. The pages obviously go on to list attributes and methods but aside from particular use-cases (which I do not yet have) it does not inform my understanding of the difference. The more descriptive texts I've come across so far online do not appear to realise that they describe the two as the same thing just with different words... any suggestions (comments!) on good sources on this would be welcome – Toby Apr 27 '18 at 15:41
  • 1
    @Slava Here you go ;) https://www.quora.com/What-are-the-differences-between-bikes-and-private-cars (someone was polite enough to answer such a question without being condescending) – Toby Apr 27 '18 at 15:44

4 Answers4

6

From multiset:

std::multiset is an associative container that contains a sorted set of objects

Search, insertion, and removal operations have logarithmic complexity.

From list:

std::list is a container that supports constant time insertion and removal of elements from anywhere in the container

Fast random access is not supported

Thus, if you want to have a faster search, use multiset.

For faster insertion and removal: use list.

Joseph D.
  • 11,804
  • 3
  • 34
  • 67
  • 1
    Don't just jump on `list` "for faster insert" -- for _many_ use cases it's just _not better_. See: https://isocpp.org/blog/2014/06/stroustrup-lists – Chad Apr 27 '18 at 14:31
  • It's worth noting that fast insertion into a list assumes an iterator to the position of insertion is already available. Otherwise, finding that iterator may have worse than constant time complexity. – François Andrieux Apr 27 '18 at 14:31
  • @Chad, true it may not be better. thanks for sharing. just wanted to point out that an insert would need the iterator position for list. – Joseph D. Apr 27 '18 at 14:34
  • @FrançoisAndrieux, thank you for pointing out the worst time complexity for list. I totally agree with you. – Joseph D. Apr 27 '18 at 14:36
  • 3
    `std::vector` usually beats `std::list` on modern hardware for all operations - regardless of theoretical big O complexity. – Jesper Juhl Apr 27 '18 at 14:59
  • @JesperJuhl, true. would be happy to include it if OP asks for it. :) – Joseph D. Apr 27 '18 at 15:00
5

The biggest difference is std::list is a linked list while std::multiset is a tree structure (typically an RB Tree). This means element access in a std::list has O(N) access while a std::multiset has O(logN).

This also means iterating a std::multiset from begin() to end() will give you sorted data while iterating a std::list will give you the order the data was inserted.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
0

Its not that straightforward, but to be short:
If you need to query search by value many times, opt for std::multiset.
Otherwise std::list.

Eduard Rostomyan
  • 7,050
  • 2
  • 37
  • 76
0

There are quite a few containers in standard library, they have different properties and implication on timing for access, insert and remove elements. You should choose one that is most applicable in particular case from whole group. Your definition of std::multiset and std::list is artificial and does not make any sense. If chair and horse both have legs it does not mean they are similar.

Slava
  • 43,454
  • 1
  • 47
  • 90