在只有第一个元素已知的STL列表中找到一对 [英] find a pair in a STL list where only first element is known

查看:45
本文介绍了在只有第一个元素已知的STL列表中找到一对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个(已填写)列表

assumend I have a (filled) list

std::list<std::pair<int,otherobject>> myList;

并想要find()此列表中的第一个元素,其中int具有特定值-我该怎么做?

and want to find() the first element within this list, where int has a specific value - how can I do that?

进一步解释一下:

我想将这些对添加到具有标识其他对象但不是唯一的int的列表中.这些int/otherobject对到达的顺序必须保持.

I want to append these pairs to the list with an int that identifies otherobject but is not unique. The order where these int/otherobject pairs arrive has to be kept.

在访问此列表的元素时发现一个int时,必须将该int的首次出现归还(并删除).

When an int is found during access to elements of this list the first occurence of that int has to be given back (and removed).

谢谢!

推荐答案

我认为我会使用标准的find_if算法:

I think I'd use the standard find_if algorithm:

auto pos = std::find_if(myList.begin(), myList.end(),
                        [value](std::pair<int, otherobject> const &b) { 
                            return b.first == value; 
                        });

这将为元素提供一个具有所需值的迭代器-从那里,您可以复制该值,删除该值等,就像其他迭代器一样.

That gives an iterator to the element with the required value -- from there, you can copy the value, delete the value, etc., just like with any other iterator.

这篇关于在只有第一个元素已知的STL列表中找到一对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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