如何迭代 std::set?

2022-01-17 00:00:00 iteration set c++

我有这个代码:

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

没有 ->first 值.如何获取价值?

There is no ->first value. How I can obtain the value?

推荐答案

您必须取消引用迭代器才能检索您的集合的成员.

You must dereference the iterator in order to retrieve the 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
}

如果你有 C++11 的特性,你可以使用 range- 基于 for 循环:

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

for(auto f : SERVER_IPS) {
  // use f here
}    

相关文章