How to iterate std::set?(如何迭代 std::set?)
问题描述
我有这个代码:
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
}
这篇关于如何迭代 std::set?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!