Finding permutations of a list in a map or multimap

D

Daniel Fortin

I was wondering if anyone had any suggestions for this problem:

I would like to use a map to store separate lists. Each list can have
a variable number of elements. The problem is that I would like to be
able to to check the map for permutations of currently stored items.
So in other words, if the list (3,2,5,12,4,6) is in the map, then I
would like to know when I try to insert a sequence (3,2,5,12,6,4) that
it is already in the map. Is there an easy way to do this with STL?
 
J

John Harrison

Daniel Fortin said:
I was wondering if anyone had any suggestions for this problem:

I would like to use a map to store separate lists. Each list can have
a variable number of elements. The problem is that I would like to be
able to to check the map for permutations of currently stored items.
So in other words, if the list (3,2,5,12,4,6) is in the map, then I
would like to know when I try to insert a sequence (3,2,5,12,6,4) that
it is already in the map. Is there an easy way to do this with STL?

Perhaps you need to store sets in the map, not lists. Or if you really need
lists, then perhaps you need a set of sets that would help you catch the
examples that are duplicates. The point is that a list can be ordered many
ways, but a set of the same elements is unique because set imposes an
ordering.

If your lists could contain duplicate numbers, the you need multisets not
sets.

john
 
D

David Harmon

The problem is that I would like to be
able to to check the map for permutations of currently stored items.
So in other words, if the list (3,2,5,12,4,6) is in the map, then I
would like to know when I try to insert a sequence (3,2,5,12,6,4) that
it is already in the map.

Is it important to preserve the permuted order within your lists?
If you can sort them then checking becomes much easier.
 
F

Frank Schmitt

I was wondering if anyone had any suggestions for this problem:

I would like to use a map to store separate lists. Each list can have
a variable number of elements. The problem is that I would like to be
able to to check the map for permutations of currently stored items.
So in other words, if the list (3,2,5,12,4,6) is in the map, then I
would like to know when I try to insert a sequence (3,2,5,12,6,4) that
it is already in the map. Is there an easy way to do this with STL?

Add a set containing the sorted lists already in the map, and before
insertion of a new sequence into the map, check if it already exists in
the set - if not, insert it into both the set and the map.

HTH & kind regards
frank
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top