在 ifcondition al中 find() C++ STL 中的 == a.end() 有什么用?
what is the use of == a.end() in find() c++ stl in if conditional?
我刚刚解决了一个codeforce问题,我在作者的教程解决方案中发现了关于find(( c ++ stl的一些新东西......但我无法理解。在这里,find(a.begin(), a.end(), s-i) == a.end()
==a.end()
做什么?
(问题链接:http://codeforces.com/contest/1293/problem/A(
//Author's tutorial solution
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define endl 'n'
int n, s, k;
vector<int> a;
void Input() {
cin >> n >> s >> k; a.clear(); a.resize(k);
for (auto &z: a) cin >> z;
}
void Solve() {
for (int i=0; i<=k; i++) {
if (s-i >= 1 && find(a.begin(), a.end(), s-i) == a.end()) {cout << i << endl; return;} //SEE HERE
if (s+i <= n && find(a.begin(), a.end(), s+i) == a.end()) {cout << i << endl; return;} //SEE HERE
}
assert(false);
int main(int argc, char* argv[]) {
ios_base::sync_with_stdio(0); cin.tie(NULL);
int T; cin >> T; while (T--) {Input(); Solve();} return 0;
}
std::find
和其他类似函数将迭代器返回到找到的元素。如果在容器中找不到该元素,则返回的迭代器将指向指定范围的末尾(在本例中为std::end
(。因此,find(a.begin(), a.end(), s-i) == a.end()
意味着元素s-i
不在容器a
中。
CPP 参考
根据文档 (http://www.cplusplus.com/reference/algorithm/find/(:
返回:对比较范围中第一个元素的迭代器 等于瓦尔。如果没有匹配的元素,函数将返回最后。
find(a.begin(), a.end(), s-i)
试图在a
中找到s-i
,并在失败时返回a.end()
。
find(a.begin(), a.end(), s-i) == a.end()
是一个布尔表达式,当a
中找不到s-i
时,它的计算结果为true
,否则false
。
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- 警告处理为错误这里有什么问题
- 什么时候调用组成单元对象的析构函数
- #定义c-预处理器常量..我做错了什么
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- 努力将整数转换为链表。不知道我在这里做错了什么
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 在 ifcondition al中 find() C++ STL 中的 == a.end() 有什么用?
- vector.back()和vector.end()有什么区别
- begin(),end()和cbegin(),cend()之间的区别是什么?
- 使用什么作为 end() 迭代器C++?
- 在二叉搜索中,mid=(beg+end)/2 和 mid=beg+(end-beg)/2 有什么区别?
- 当调用带有警告"control reaches end of non-void function"的函数时,(实际上)会发生什么?
- 查找语句中的"!=vector.end()"是什么意思?
- 集合数据结构中的 end() 迭代器应该返回什么
- 我读了map.erase(map.end());删除地图的最后一个元素。但是最后一个元素是什么?它是否基于元素的插入顺序?
- std::begin和std::end的const重载的目的是什么
- 为我的向量包装器和迭代器返回'begin'和'end'函数返回什么?
- 删除' std::list '的' end() '的行为是什么?
- 那么,是什么区别了template(c.end(),_1)和template_back(_1)呢