Tamer Shlash
Tamer Shlash

Reputation: 9523

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 no, is there any way to represent it (exept array since I know it).

thanks in advance :)

Upvotes: 5

Views: 3640

Answers (5)

Greg Domjan
Greg Domjan

Reputation: 14095

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.

Upvotes: 0

Uli Gerhardt
Uli Gerhardt

Reputation: 13991

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.

Upvotes: 2

pr1268
pr1268

Reputation: 1186

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

Upvotes: 0

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... )

Upvotes: 10

Carlos Melo
Carlos Melo

Reputation: 3152

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

Upvotes: 3

Related Questions