Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 978
Tamer Shlash Avatar asked Nov 22 '10 21:11

Tamer Shlash


3 Answers

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

like image 101
David Rodríguez - dribeas Avatar answered Oct 05 '22 08:10

David Rodríguez - dribeas


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

like image 22
Carlos Melo Avatar answered Oct 05 '22 09:10

Carlos Melo


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.

like image 45
Uli Gerhardt Avatar answered Oct 05 '22 09:10

Uli Gerhardt