There is a set visited
. And I wanna check all its elements from 4-th to last.
I'm trying to do something like that
int visited_pointer = 4;
for ( set<int>::iterator i_visited=visited.begin()+visited_pointer
; i_visited!=visited.end()
; i_visited++
)
and got errors with operator+
.
How can I do that in the right way?
You have to access the elements using an iterator. set<int> myset; myset. insert(100); int setint = *myset. begin();
You can't access set using index but the above method would provide an "index" i if you want to copy the elements from set into an array, provided you have created an array of sufficient size before hand.
A set in c++ is an associative(STL) container used to store unique elements, and they are stored in a specific sorted order(increasing or decreasing). Elements of the set are unique, i.e., no duplicate values can be stored in the set because each value in the set is a key, and the set doesn't support indexing.
That usage of operator+
is only provided for random-access iterators. set
iterators are bi-directional iterators.
But the function std::advance
can be used to move any iterator a certain number of places:
#include <iterator>
//...
set<int>::iterator i_visited = visited.begin();
for ( std::advance(i_visited, visited_pointer)
; i_visited!=visited.end()
; ++i_visited
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With