检查项目是否在列表中的简单方法? [英] Easy way to check if item is in list?

查看:125
本文介绍了检查项目是否在列表中的简单方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C ++中编写一个搜索算法,我需要做的一件事是有一些if语句检查上面,下面,左边和右边的单元格。

I'm writing a search algorithm in C++, and one of the things I need to do is have a few if statements that check cells above, below, left of, and right of.

每次发现单元格被打开并添加到堆栈中时,我想将它添加到已经选中的单元格列表中。

Each time a cell is found to be open and added to the stack, I want it added to a list of cells already checked.

I想要能够在if语句中 if(thisCell不在checkedCells中)

I want to be able to say in the if statement if(thisCell is not in checkedCells).

任何简单的想法?

谢谢!

推荐答案

std :: set 容器,因为它提供了搜索项比列表更快的能力。然后你可以写:

For this purpose it's better to use the std::set container, because it provides you with the ability to search for items faster than a list. Then you can write:

std::set<itemType> myset;
...

if (myset.find(item) != myset.end()) {
  // item is found
}

Google可以找到更大的示例。例如,此处

A larger example can be found by googling. For example, here.

这篇关于检查项目是否在列表中的简单方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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