How do i get access to an element in a set ?
vector<int> myvec (4,100);
int first = myvec.at(0);
set<int> myset;
myset.insert(100);
int setint = ????
Can anybody help ?
How do i get access to an element in a set ?
vector<int> myvec (4,100);
int first = myvec.at(0);
set<int> myset;
myset.insert(100);
int setint = ????
Can anybody help ?
set<int>::iterator iter = myset.find(100);
if (iter != myset.end())
{
int setint = *iter;
}
You can't access set elements by index. You have to access the elements using an iterator.
set<int> myset;
myset.insert(100);
int setint = *myset.begin();
If the element you want is not the first one then advance the iterator to that element. You can look in a set to see if an element exists, using set<>::find()
, or you can iterate over the set to see what elements are there.
In general I find that this is a good documentation of the STL.