检查2D矢量中是否存在元素

时间:2018-05-06 19:41:41

标签: c++ vector stl

我已经在2D矢量中插入了一些元素,并且想要知道给定元素是否存在于2D矢量中的任何位置。有没有快速找到元素存在的方法?

向量声明为:vector < vector< int > > v;

3 个答案:

答案 0 :(得分:1)

不像我希望的那样优雅。给定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();

答案 1 :(得分:1)

如果您没有关于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;
}

答案 2 :(得分:0)

TL; DR

使用 std::find()

在C ++中,请使用 Standard Template Library (STL) 中的函数(如果已提供)。

阐述

假设您有一个二维向量

std::vector<std::vector<int> > matrix = {
   {1,2,3}, 
   {4,5,6},
   {7,8,9}
};

如果要迭代上面2d向量中的所有元素。

我建议您使用2d iterator

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