检查元素是否存在于二维矢量中
Check whether an element exists in a 2D vector
我在 2D 向量中插入了一些元素,并想知道给定元素是否存在于 2D 向量中的任何地方。有没有快速的方法可以找到元素的存在?
向量声明为:vector < vector< int > > v;
如果您没有关于 2D 矢量的更多信息(例如以某种方式排序),那么最好的方法是迭代 2D 矢量的每一行并使用find
方法来检查它是否存在。
您可以执行以下操作:
bool does_exist(const vector< vector<int> >& v, int item){
vector< vector<int> >::const_iterator row;
for (row = v.begin(); row != v.end(); row++) {
if(find(row->begin(), row->end(), item) != row->end() )
return true;
}
return false;
}
您可以使用以下代码对其进行测试:
#include <iostream>
#include <vector>
using namespace std;
int main(){
int item = 12;
vector < vector <int> > v;
vector <int> v1;
v1.push_back(1);
v1.push_back(2);
v1.push_back(3);
vector <int> v2;
v2.push_back(4);
v2.push_back(5);
v2.push_back(6);
v.push_back(v1);
v.push_back(v2);
if( does_exist(v, item))
cout << "Item " << item << " exist" << endl;
else
cout << "Item " << item << " does not exist" << endl;
}
没有我希望的那么优雅。给定一个 int 的 2D 向量:
std::vector<std::vector<int>> foo = {
{{1, 2, 3}},
{{5, 6, 7}},
{{8, 9, 13, 15}}
};
您可以执行此操作以查看是否存在 13
:bool found =
find_if(foo.begin(), foo.end(),
[](const std::vector<int>& v) -> bool {
return find(v.begin(), v.end(), 13) != v.end();
}) != foo.end();
TL;博士
使用std::find()。
在C++中,请使用标准模板库 (STL)中的函数(如果已提供)。
阐述
假设您有一个 2D 矢量
std::vector<std::vector<int> > matrix = {
{1,2,3},
{4,5,6},
{7,8,9}
};
如果您想迭代上述 2d 矢量中的所有元素。
我建议您使用2diterator
:
bool element_exist(const vector< vector<int> >& input, int key){
// 2d vector iterator
vector< vector<int> >::iterator row_it; //iterate over each row(s)
vector<int>::iterator col_it; //iterate over column(s)
for (row_it = input.begin(); row_it != input.end(); row_it++) { // iterate each row(s)
for (col_it = row_it->begin(); row_it != row_it->end(); col_it++) {
if(find(row_it->begin(), row_it->end(), key) != row_it->end())
return true;
}
}
}
并且您可以使用另一个布尔变量来获取函数的返回值key_exist
bool_var = element_exist(matrix, key);
整个程序
#include <vector>
#include <iostream>
using namespace std;
bool element_exist(const vector< vector<int> >& input, int key){
// 2d vector iterator
vector< vector<int> >::const_iterator row_it; //iterate over each row(s)
vector<int>::const_iterator col_it; //iterate over column(s)
for (row_it = input.begin(); row_it != input.end(); row_it++) { // iterate each row(s)
for (col_it = row_it->begin(); row_it != row_it->end(); col_it++) {
if(find(row_it->begin(), row_it->end(), key) != row_it->end())
return true;
}
}
}
int main() {
// declaration
bool bool_var = false; // default false
std::vector<std::vector<int> > matrix = {{1,2,3}, {4,5,6},{7,8,9}};
bool_var = element_exist(matrix,1);
cout << "bool_var: " << bool_var << endl;
return 0;
}
结果
bool_var: 1
相关文章:
- 将二维矢量传递给类
- 将值从二维数组输出到文本文件
- 我在二维向量中是否正确分配了内存
- 带结构的二维矢量:如何存储元素
- 在二维数组中查找最小值和最大值?
- 移动二维数组中的字符
- C++ 传递二维字符数组
- 如何正确填充在堆上分配的二维数组?
- 传递二维数组时出现问题
- 具有随机数的二维数组不会更改
- 如何在C++中获取二维数组中最少的一列数?
- 如何使用用户输入变量制作二维数组?
- C++ 中的二维整数数组,每行中的元素数量不均匀
- C++ 中动态二维数组的访问冲突
- 检查元素是否存在于二维矢量中
- C++ ->尝试逐字阅读一行文本。如何使指针等效于用于存储输入的当前二维数组
- 通过访问二维数组实现双线性插值的概念类似于一维数组
- 是在内存中连续设置的二维数组,类似于模拟二维数组的一维数组
- 给定一个二维字符矩阵,我们必须检查给定的单词是否存在于其中
- 是否存在一种算法来确定二维空间中的一组点是否形成一个封闭区域