gpt4 book ai didi

c++ - 不能对二维数组使用 find()

转载 作者:行者123 更新时间:2023-11-28 05:20:53 24 4
gpt4 key购买 nike

我在二维数组上使用查找时遇到问题。每次我尝试执行这样的查找时,都会出现无法理解的错误

我的代码:

#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;

int st[200*1000][2];
int si[200*1000][2];

int main() {
if (find(si, si+200000, true) == si+200000){
//do something
}
for (int j = 0; j < ileSt; j++){
for (int k = 0; k < 2; k++){
if (find(st[j], st[j]+2, si[znakSi][k]) != st[j]+2){
//do another thing
}
}
}
return 0;
}

错误:

In file included from /usr/include/c++/5/bits/stl_algobase.h:71:0,
from /usr/include/c++/5/bits/char_traits.h:39,
from /usr/include/c++/5/ios:40,
from /usr/include/c++/5/ostream:38,
from /usr/include/c++/5/iostream:39,
from prog.cpp:1:
/usr/include/c++/5/bits/predefined_ops.h: In instantiation of 'bool __gnu_cxx::__ops::_Iter_equals_val<_Value>::operator()(_Iterator) [with _Iterator = int (*)[2]; _Value = const bool]':
/usr/include/c++/5/bits/stl_algo.h:120:14: required from '_RandomAccessIterator std::__find_if(_RandomAccessIterator, _RandomAccessIterator, _Predicate, std::random_access_iterator_tag) [with _RandomAccessIterator = int (*)[2]; _Predicate = __gnu_cxx::__ops::_Iter_equals_val<const bool>]'
/usr/include/c++/5/bits/stl_algo.h:161:23: required from '_Iterator std::__find_if(_Iterator, _Iterator, _Predicate) [with _Iterator = int (*)[2]; _Predicate = __gnu_cxx::__ops::_Iter_equals_val<const bool>]'
/usr/include/c++/5/bits/stl_algo.h:3790:28: required from '_IIter std::find(_IIter, _IIter, const _Tp&) [with _IIter = int (*)[2]; _Tp = bool]'
prog.cpp:19:31: required from here
/usr/include/c++/5/bits/predefined_ops.h:194:17: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
{ return *__it == _M_value; }
^

此外,这是我的第一个问题,所以如果您认为它可以写得更好,请随时对其进行编辑!

最佳答案

您可以使用对二维数组元素的直接寻址来重写它。例如:

    for (int j = 0; j < ileSt; j++)
{
for (int k = 0; k < 2; k++)
{
if (find(&st[j][0], &st[j][0] + 2, si[znakSi][k]) != &st[j][0] + 2 )
{
//do another thing
}
}
}

或者使用 std::begin 获取数组的行地址

find(std::begin(st[j]), std::begin(st[j]) + 2, si[znakSi][k]) != std::begin(st[j]) + 2

关于c++ - 不能对二维数组使用 find(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41526421/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com