What is the set-like data structure in 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 n开发者_StackOverflow社区o, 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.
精彩评论