加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何迭代c映射中的一组特定键?

发布时间:2020-12-16 03:47:56 所属栏目:百科 来源:网络整理
导读:我在迭代C地图.假设我想获取除第一个之外的地图中存在的键.键在地图中排序.因此我想到使用这样的东西: mapint,int table;for( auto i = table.begin()+2; i != table.end(); i++ ) couti-first"t"i-secondendl; 虽然这适用于矢量,但由于没有为地图实现”运
我在迭代C地图.假设我想获取除第一个之外的地图中存在的键.键在地图中排序.因此我想到使用这样的东西:
map<int,int> table;
for( auto i = table.begin()+2; i != table.end(); i++ )
  cout<<i->first<<"t"<<i->second<<endl;

虽然这适用于矢量,但由于没有为地图实现”运算符,因此它会导致地图出错.实现结果的一种方法是:

auto i = table.begin();
int count = 0;
while( count < 2 && i != table.end() ){
  count++;
  i++;
}
for( ; i!=table.end(); i++ )
  cout<<i->first<<"t"<<i->second<<endl;

有没有其他有效的方法来实现这一点?

解决方法

它没有效率,但可能更容易阅读
for (auto i = std::next(table.begin(),2); i != table.end(); i++)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读