What is the set-like data structure in c++ - c++

I need to use the advantages of delphi sets like "in" in c++, but I don't know if there is a data structure like sets in c++
I know that I may use an array instead, but as I have said I want to use sets advantages like "in", so is there any built in data structure like sets in c++?
If yes, please explain how to use it, I'm still a starter in c++
If no, is there any way to represent it (exept array since I know it).
thanks in advance :)

There is a standard library container called std::set... I don't know delphi, but a simple element in set operation would be implemented by using the find method and comparing the result with end:
std::set<int> s;
s.insert( 5 );
if ( s.find( 5 ) != s.end() ) {
// 5 is in the set
}
Other operations might be implemented as algorithms in the standard library (std::union, std::difference... )

Use std::set. See http://www.cplusplus.com for reference.

In C++ there is nothing similarly integrated. Depending on your needs you might want to use bit flags and bitwise operations or the std::bitset standard container (besides std::set, of course). If you are using C++Builder there is also a class that simulates Delphi sets - search System.hpp for something like BaseSet or SetBase or similar - I don't recall the exact name.

Yes, there is a C++ STL set container class described on p. 491 of Stroustrup's TC++PL (Special Ed.).

STL algorithm has the following
From MSDN
set_difference
Unites all of the elements that belong to one sorted source range, but not to a second sorted source range, into a single, sorted destination range, where the ordering criterion may be specified by a binary predicate.
set_intersection
Unites all of the elements that belong to both sorted source ranges into a single, sorted destination range, where the ordering criterion may be specified by a binary predicate.
set_symmetric_difference
Unites all of the elements that belong to one, but not both, of the sorted source ranges into a single, sorted destination range, where the ordering criterion may be specified by a binary predicate.
set_union
Unites all of the elements that belong to at least one of two sorted source ranges into a single, sorted destination range, where the ordering criterion may be specified by a binary predicate.

Related

why does mutiset don't act like set

Why is multiset a set while a set can only contains only different elements, while multiset can contain the same elements? It could of just be called sortedArray or sortedList. Even if it just wants a sorted "collections", why is it a set?
Why is multiset a set
In mathematics there are two distinct concept of set and multiset. Standard library has two containers that model these concepts: std::set and std::multiset. These concepts are not the same and therefore container names are also different because they model different mathematical concepts.
Why is multiset a set [...]
It's not. The word "set" does appear in "multiset", but that does not make a multiset a set. A multiset is a generalization of a set, not necessarily itself a set. This linguistic setup is similar to a hypergraph, which is a generalization of a graph but not necessarily a graph, and to a hyperplane, which is a generalization of a plane but not necessarily a plane.
A less mathematical example would be penultimate, which is not "ultimate", or any other word with a prefix that changes the meaning of the root.
Perhaps "butterfly" and "dragonfly" would be apropos examples. Neither is a fly, despite the word "fly" appearing in both names. (For that matter, neither is buttery or draconic.) Sometimes a name is just a name.

std::set_intersection is for sorted ranges as [ _ ] is for unsorted ranges/containers

std::set_intersection takes sorted ranges of elements (well, iterator pairs). But suppose I have unsorted data, e.g. two std::unordered_sets. Is there a standard facility for intersection them?
I don't know of any such function in C++11. The answer is "no".
There's no shortcut in this case. You should check each element of the smaller set for membership in the larger set and insert it into an output set if it's found. Since unordered_set is implemented using a hash with buckets, the lookup times should (with a decent hash function and reasonable maximum loading of the hash) be small. You should be able to write a call to for_each on the smaller set that performs the check on the larger one and the insert into the output set without it getting too ugly.
If you want to build the intersection in place in one of the two original sets, you could check whether each of its elements is in the other set and remove that element if not. This could be written with remove_if on the unordered_set that will hold the result.
Yet another option would be to use copy_if with an insertion iterator. There are lots of options for doing this within the same time and space. Pick one that seems to optimize for clarity.
I know of no canned library function that will just do it for you.

C++ container for storing sorted unique values with different predicates for sorting and uniqueness

I have a record with 2 fields (say, A and B). 2 instances of the record should be considered equal, if their As are equal. On the other hand, a collection of the record instances should be sorted by the B field.
Is there a container like std::set, which can be defined with two different predicates, one for sorting and one for uniqueness, so I could avoid explicit sorting and just append elements? If no, how can it be workarounded?
Regards,
There is nothing in the standard library which would support your use case directly. You could use Boost.MultiIndexContainer for this purpose, though. Something like this:
typedef multi_index_container<
Record,
indexed_by<
ordered_non_unique<member<Record, decltype(Record::B), &Record::B>>,
hashed_unique<member<Record, decltype(Record::A), &Record::A>>
>
> RecordContainer;
(Code assuming correct headers and using namespace directives for brevity).
The idea is to create a container with two indices, one which will guarantee the ordering based on B and the other which will guarantee uniqueness based on A. decltype() in the code can of course be replaced by the actual types of A and B which you know, but I don't.
The order of the indices matters slightly, since for convenience, the container itself offers the same interface as its first index. You can always access any index by using container.get(), though.
The code is not intended as a copy&paste solution, but as a starting point. You can add customisations, index tags etc. Refer to Boost documentation for details.
Is there a container like std::set, which can be defined with two different predicates, one for sorting and one for uniqueness
std::set defines whether particular element is unique OR not in terms of the sorting criteria you provide to it( by default it uses less<>) . There's no need to explicitly pass another criteria for checking equality of elements.
With that said, however, you can use a predicate with algorithms to check for equality of elements of std::set.

Implementing Dijkstra's shortest path algorithm using C++ and STL

I am trying to implement Dijkstra's shortest path algorithm using C++ and STL. Since STL's priority queues do not support a decrease-key operation, I decided to use regular ordered sets. My algorithm is almost identical to this one.
However, I have some concerns. Namely, the ordering of the edges in the set will depend both upon the vertex number of the destination and the weight (as the regular relational operators of std::pair will be used). I do believe that it should only depend on the weight. If I were to declare the set by using a custom comparator which would compare only the weights, how would I make std::set::erase work, as it is needed to erase the edges between the same vertices but with greater weight?
Are there any other flaws that you guys can think of? Or do you perhaps have some better ideas than using std::set?
Have a great Sunday, everyone.
Your question seem to confuse the technical implementation and the algorithm.
First, on the technical side, for std::set you seem to need a special ordering as well as an erasement of certain elements. The ordering can be changed by a custom comparator, for example see here. However, I would not order only by the weights, as there might be duplicates. Just put the weights in the component of std::pair which has a higher priority (--the first component).
Next, in order to erase an element, you must first be sure which one, which is done by providing an iterator pointing to that element. This step is not at all influenced by your custom comparison function.
Quickly summarizing: you should (i) find out which elements need to be erased exactly, (ii) find the corresponding iterators via std::set::find and (iii) erase them. To me it seems as if the first point would be the problem here.

C+11 Associative Container that keeps insertion order?

is there, in the c++ "Standard Library", any "Associative" (i.e. "Key-Value") Container/Data Structure, that has the ability, to preserve order, by order of insertion?
I have seen several topics on this, however, it seems, most before C++11.
Some suggest using "boost::multi_index", but, if at all possible, I would "rather" use standard containers/structures.
I see that C++11 has several, apparently, "unordered" associative containers :link.
Are any of these, by some way, "configurable", such that they are only sorted by insertion order?
Thanks!
C
No.
You are mixing linear access with random. Not very good bed fellows.
Just use both a vector/list (i.e. order of insertion) along with a map using an index into the former.
No; such capability was apparently sacrificed in the name of performance.
The order of equivalent items is required to be preserved across operations including rehashes, but there's no way to specify the original order. You could, in theory, use std::rotate or the like to permute the objects into the desired order after each insertion. Obviously impractical, but it proves the lack of capability is a little arbitrary.
Your best bet is to keep the subsequences in inner containers. You may use an iterator adaptor to iterate over such a "deep" container as if it were a single sequence. Such a utility can probably be found in Boost.
No.
In unordered maps too, there are not stored according to the order of insertion.
You can use vector to keep the track of the key!