简体   繁体   English

使用std :: find在std :: vector中找到一个std :: tuple

[英]use std::find to find a std::tuple in a std::vector

So I have a vector of tuple coordinates made with the following code: 所以我有一个用以下代码制作的元组坐标向量:

vector<tuple<int, int>> coordinates;
for (int i = 0; i <  7; i++){
   for (int j = 0; j < 6; j++){
      coordinates.push_back(make_tuple(i, j));
   }
}

I'm trying to fill the board up with either 'x', 'o', or '.' 我正在尝试用“ x”,“ o”或“。”填充董事会。 with the following: 具有以下内容:

void displayBoard(vector<tuple<int,int>>& board, vector<tuple<int,int>>& p1, vector<tuple<int,int>>& p2){  // prints out board
  cout << "  a   b   c   d   e   f   g\n";  // top row
  for (int i = 1; i < 43; i++){
    if (i % 7 == 0) {
      if (find(p1.begin(), p1.end(), board[i])) cout << "| x |\n";
      else if (find(p2.begin(), p2.end(), board[i])) cout << "| o |\n";
      else cout << "| . |\n";
    } else {
      if (find(p1.begin(), p1.end(), board[i])) cout << "| x ";
      else if (find(p2.begin(), p2.end(), board[i])) cout << "| o ";
      else cout << "| . ";
    }
  }
}

my int main looks as follows: 我的诠释主要如下所示:

int main() {
  vector<tuple<int, int>> coordinates;
  for (int i = 0; i <  7; i++){
    for (int j = 0; j < 6; j++){
      coordinates.push_back(make_tuple(i, j));
    }
  }
  vector<tuple<int,int>> p1 = {make_tuple(0,1)};
  vector<tuple<int,int>> p2 = {make_tuple(3,1)};
  displayBoard(coordinates, p1, p2);
  return 0;
}

I used (0,1) and (3,1) as test coordinates to see if the code would run. 我使用(0,1)和(3,1)作为测试坐标,以查看代码是否可以运行。 Long story short, I wanted to use std::find to find if a tuple coordinate was chosen by either p1 or p2 and format the outputted string accordingly. 长话短说,我想使用std :: find查找p1或p2是否选择了元组坐标,并相应地格式化输出的字符串。 So if if std::find_if(p1.begin(), p1.end(), make_tuple(2,2)) was true to put fill the cell with an 'x' for example. 因此,如果std::find_if(p1.begin(), p1.end(), make_tuple(2,2))为true,则用“ x”填充单元格。 The problem is I get the following error when compiling: 问题是编译时出现以下错误:

error: could not convert 'std::find<__gnu_cxx::__normal_iterator<std::tuple<int, int>*, std::vector<std::tuple<int , int> > >, std::tuple<int, int> >((& p2)->std::vector<_Tp, _Alloc>::begin<std::tuple<int, int>, std::allocator<std::tuple<int, int> > >(), (& p2)->s td::vector<_Tp, _Alloc>::end<std::tuple<int, int>, std::allocator<std::tuple<int, int> > >(), (*(const std::tuple<int, int>*)(& board)->std::vector<_ Tp, _Alloc>::operator[]<std::tuple<int, int>, std::allocator<std::tuple<int, int> > >(((std::vector<std::tuple<int, int> >::size_type)i))))' from '__ gnu_cxx::__normal_iterator<std::tuple<int, int>*, std::vector<std::tuple<int, int> > >' to 'bool'

So the question is if i can use std::find_if to find an std::tuple in an std::vector. 所以问题是我是否可以使用std :: find_if在std :: vector中找到一个std :: tuple。 And if not how can you find a tuple in a vector. 如果不是,如何在向量中找到元组。

Note: I included: iostream, string, tuple, vector, and algorithm and am using namespace std. 注意:我包括:iostream,字符串,元组,向量和算法,并且正在使用命名空间std。

Your issue is not searching for the tuple in the vector. 您的问题不是在向量中搜索元组。 Your search is just fine. 您的搜索很好。

Your issue is that std::find returns either an iterator to the found sequence member, or the ending iterator value. 您的问题是std::find返回一个迭代器到找到的序列成员,或者返回结束的迭代器值。

Your code assumes that std::find () returns a bool indication that the value has been found. 您的代码假定std::find ()返回bool值,表明已找到该值。 This is not true. 这不是真的。 std::find() returns an iterator. std::find()返回一个迭代器。 Either the iterator to the found value, or the ending iterator value. 迭代器为找到的值,或结束的迭代器值。

You can use find_if as follows: 您可以按如下方式使用find_if

int main()
{
vector<tuple<int, int>> coordinates;
coordinates.push_back(make_tuple(0,1));
coordinates.push_back(make_tuple(2,3));

auto t = make_tuple(2,3);

auto it = std::find_if(coordinates.begin(), coordinates.end(), [&t](const auto& item) {
    return std::get<0>(t) == std::get<0>(item)
           && std::get<1>(t) == std::get<1>(item);
    });

    if(it!=coordinates.end())
      cout << "found" << endl;
}

It returns an iterator to the found sequence, or the end iterator if it hasn't found the element you are looking for. 它将迭代器返回到找到的序列,如果没有找到您要查找的元素,则返回结束迭代器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM