如何检查C++特定索引处的特定值列表
然后,我有一个列表
list<int>generic_list;
,然后将一个随机数附加到列表中
rand_num = rand();
generic_list.push_front(rand_num);
,然后检查列表是否有一个值,
find(generic_list.begin(), generic_list.end(), "generic string");
一旦我证明它在列表中,我如何找到它的特定索引?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 std :: distance 来解决此问题。这将从列表的开头返回“啤酒花”的数量到找到的项目。
正如您可以在 end()如果找不到任何东西)。
然后,您可以简单地
std :: distance
显示获取索引。请参阅下面的示例代码:
You can solve this problem with std::distance. This will return the number of "hops" from the begin of your list to the found item.
As you can read in the desciption of the std::find function, this will return an iterator to the found element. (
end()
if nothing could be found).Then you can simply
std::distance
to show get the index.Please see the example code below: