C++ 检查二维向量中是否存在元素
我在二维向量中插入了一些元素,想知道给定元素是否存在于二维向量的任何位置。有没有快速找到元素存在的方法C++ 检查二维向量中是否存在元素,c++,vector,stl,C++,Vector,Stl,我在二维向量中插入了一些元素,想知道给定元素是否存在于二维向量的任何位置。有没有快速找到元素存在的方法 向量声明为:vectorv不像我希望的那样优雅。给定int的2D向量: std::vector<std::vector<int>> foo = { {{1, 2, 3}}, {{5, 6, 7}}, {{8, 9, 13, 15}} }; std::vector foo={ {{1, 2, 3}}, {{5, 6, 7}}, {{8, 9, 1
向量声明为:
vector>v代码>不像我希望的那样优雅。给定int的2D向量:
std::vector<std::vector<int>> foo = {
{{1, 2, 3}},
{{5, 6, 7}},
{{8, 9, 13, 15}}
};
std::vector 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();
bool已找到=
查找_if(foo.begin(),foo.end(),
[](const std::vector&v)->bool{
返回find(v.begin(),v.end(),13)!=v.end();
}) != foo.end();
如果您没有关于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;
}
bool不存在(常量向量&v,int项){
向量<向量>::常量迭代器行;
for(row=v.begin();row!=v.end();row++){
如果(查找(行->开始(),行->结束(),项)!=行->结束())
返回true;
}
返回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;
}
#包括
#包括
使用名称空间std;
int main(){
int项=12;
向量v;
向量v1;
v1.推回(1);
v1.推回(2);
v1.推回(3);
矢量v2;
v2.推回(4);
v2.推回(5);
v2.推回(6);
v、 推回(v1);
v、 推回(v2);
如果(是否存在(第五项))
太长了,读不下去了
使用
在C++中,如果使用了<强> >强>函数,请使用< < /P>
详尽阐述
假设你有一个二维向量
std::vector<std::vector<int> > matrix = {
{1,2,3},
{4,5,6},
{7,8,9}
};
您可以使用另一个布尔变量来获取函数key\u exist
bool_var = element_exist(matrix, key);
整个计划
我希望这可能是1D向量的重复。你的意思是vector v;
对于一些标量类型T?你知道如何搜索向量吗?然后用同样的方法搜索向量中的每个向量。真的,没有我所希望的那么优雅。但是,迭代会更好!!我更喜欢函数方法而不是命令式方法CHS像迭代,但是DangC++ C++ lambda语法很难看。2种方法中的任何一种方法的时间复杂度都会发生变化吗?使用函数式编程风格通常会给编译器提供更多优化的空间,但我建议使用你认为更清晰的方法。“使用函数编程风格通常给编译器提供了更多优化的空间”,你能指出驱动这个想法的资源吗?我想读一下。有人提到了这个帖子,所以我把它放在这里:更多信息[迭代二维向量STL向量C++ ]。
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