How to repeat std :: set?

I have this code:

std::set<unsigned long>::iterator it; for (it = SERVER_IPS.begin(); it != SERVER_IPS.end(); ++it) { u_long f = it; // error here } 

No value ->first . How can I get the value?

+67
c ++ set iteration
Oct. 12
source share
4 answers

You must dereference an iterator to get a member of your set.

 std::set<unsigned long>::iterator it; for (it = SERVER_IPS.begin(); it != SERVER_IPS.end(); ++it) { u_long f = *it; // Note the "*" here } 

If you have C ++ 11 features, you can use a range-based for loop :

 for(auto f : SERVER_IPS) { // use f here } 
+109
Oct 12 '12 at 16:23
source share

Just use * before it :

  set<unsigned long>::iterator it; for (it = myset.begin(); it != myset.end(); ++it) { cout << *it; } 

This dereferences it and allows you to access the element on which the iterator is currently located.

+9
Oct 12 '12 at 16:25
source share

Another example for the C ++ 11 standard:

 set<int> data; data.insert(4); data.insert(5); for (const int &number : data) cout << number; 
+6
Dec 09 '16 at 8:17
source share

This is a complete solution to the question:

How do you repeat std :: set?

 int main(int argc,char *argv[]) { std::set<int> mset; mset.insert(1); mset.insert(2); mset.insert(3); for ( auto it = mset.begin(); it != mset.end(); it++ ) std::cout << *it; } 
+2
May 19 '16 at 18:59
source share



All Articles