如何迭代std :: set? [英] How to iterate std::set?

查看:115
本文介绍了如何迭代std :: set?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码:

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

没有 - >第一个值。
如何获取值?

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的功能,您可以使用基于范围的循环

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

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

这篇关于如何迭代std :: set?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆