How to iterate std::set?

前端 未结 4 1737
南方客
南方客 2020-11-30 01:37

I have this code:

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


        
4条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-30 02:02

    You must dereference the iterator in order to retrieve the member of your set.

    std::set::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
    }    
    

提交回复
热议问题