stl从双端队列中找到函数return是什么
What does the stl find function return from deque
我有一个标准双端队列并在其中搜索元素。我的问题是我不明白 find 函数返回的是哪种形式。
std::deque< DataCellHandle > dataCellHandleArray;
std::_Deque_iterator<DataCellHandle, const DataCellHandle&, const DataCellHandle*> it =
std::find( dataCellHandleArray.cbegin(), dataCellHandleArray.cend(), releaseHandle ); // works
std::deque< DataCellHandle >::iterator itr =
std::find( dataCellHandleArray.cbegin(), dataCellHandleArray.cend(), releaseHandle ); // does not work
我预计将返回索引或迭代器。
std::find
returns 与 first
和 last
参数相同类型的对象,在您的情况下是 std::deque<DataCellHandle>::const_iterator
std::find
的 return 类型与您用来实例化此函数模板的迭代器类型相同。在你的例子中,你传递了 dataCellHandleArray.cbegin()
和 .cend()
,它们的类型是 std::deque::const_iterator
,而不是 std::deque::iterator
。因此,这是您的解决方法:
std::deque<DataCellHandle>::const_iterator it = ...
请注意,这是开箱即用的:
auto it = std::find(dataCellHandleArray.cbegin(), dataCellHandleArray.cend(),
releaseHandle);
请注意,const_iterator
可以从 iterator
构造,但反之则不行。
// Ok, iterator to const_iterator
std::deque<DataCellHandle>::const_iterator ci = dataCellHandleArray.begin();
// Ok, compare const_iterator and iterator:
assert(ataCellHandleArray.begin() == ataCellHandleArray.cbegin());
// Error, can't loose constness of the "pointee"
std::deque<DataCellHandle>::iterator ci = dataCellHandleArray.cbegin();
我有一个标准双端队列并在其中搜索元素。我的问题是我不明白 find 函数返回的是哪种形式。
std::deque< DataCellHandle > dataCellHandleArray;
std::_Deque_iterator<DataCellHandle, const DataCellHandle&, const DataCellHandle*> it =
std::find( dataCellHandleArray.cbegin(), dataCellHandleArray.cend(), releaseHandle ); // works
std::deque< DataCellHandle >::iterator itr =
std::find( dataCellHandleArray.cbegin(), dataCellHandleArray.cend(), releaseHandle ); // does not work
我预计将返回索引或迭代器。
std::find
returns 与 first
和 last
参数相同类型的对象,在您的情况下是 std::deque<DataCellHandle>::const_iterator
std::find
的 return 类型与您用来实例化此函数模板的迭代器类型相同。在你的例子中,你传递了 dataCellHandleArray.cbegin()
和 .cend()
,它们的类型是 std::deque::const_iterator
,而不是 std::deque::iterator
。因此,这是您的解决方法:
std::deque<DataCellHandle>::const_iterator it = ...
请注意,这是开箱即用的:
auto it = std::find(dataCellHandleArray.cbegin(), dataCellHandleArray.cend(),
releaseHandle);
请注意,const_iterator
可以从 iterator
构造,但反之则不行。
// Ok, iterator to const_iterator
std::deque<DataCellHandle>::const_iterator ci = dataCellHandleArray.begin();
// Ok, compare const_iterator and iterator:
assert(ataCellHandleArray.begin() == ataCellHandleArray.cbegin());
// Error, can't loose constness of the "pointee"
std::deque<DataCellHandle>::iterator ci = dataCellHandleArray.cbegin();