-8

i just solved a codeforces problem and i found something new about the find() c++ stl in the author's tutorial solution...but i cant understand that. here, in find(a.begin(), a.end(), s-i) == a.end() what does the==a.end() do?

(link to the question: 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;
}
singing sun
  • 43
  • 1
  • 8
  • 13
    Do you know what `find` returns? – NathanOliver Jan 27 '20 at 15:49
  • It's a regular comparison. Perhaps you're misreading it as being part of the call to `find`? – molbdnilo Jan 27 '20 at 15:52
  • `a` is a vector type. If you check out the [reference](https://en.cppreference.com/w/cpp/algorithm/find), `find` can use two iterators as a range to find the specified value among that range. In this case, `std::vector::end()` returns the final iterator in the vector `a`. You can see the documentation for `end()` [here](https://en.cppreference.com/w/cpp/container/vector/end). – alteredinstance Jan 27 '20 at 15:53
  • 9
    Please learn C++ from a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), not by looking at random code, especially not random code from competitive programming sites. You are not going to learn the language properly. – walnut Jan 27 '20 at 15:55
  • 1
    The internet is 90% crud, and it's often hard to pick the stuff that's worth learning from out of the crud. One sign that what you're looking at is good stuff is it uses descriptive identifiers. The names used describe what what the named does or represents. This makes the code easier to read and has the side benefit of making it harder to accidentally exchange `b` for `d` and much easier to spot if it happens anyway. – user4581301 Jan 27 '20 at 16:30

2 Answers2

4

std::find and other similar functions return the Iterator to the found element. If the element is not found in the container, the returned Iterator points to the end of the specified range (which in your case is std::end). So, find(a.begin(), a.end(), s-i) == a.end() means that element s-i is not in the container a.

cpp reference

pptaszni
  • 5,591
  • 5
  • 27
  • 43
  • the coder wrote```find(a.begin(), a.end(), s-i) == a.end()``` in order to print ```i``` iff ```s-i``` is present in vector ```a```. suppose ```find()``` returned an iterator instead of ```a.end()``` (i.e. in case ```s-i``` is found in ```a```), in this situation ```find(a.begin(), a.end(), s-i)``` ==``` a.end()``` must be ```false```, because the iterator retuned is not == a.end()....& therefore in this case, ```if``` statement stands ```false``` and ```i``` must not get printed, which is against the purpose of using the conditional. ```i``` has to be printed in case```s-i```` is found. – singing sun Jan 27 '20 at 16:32
  • sorry if its a silly doubt...but still i cant find ```find(a.begin(), a.end(), s-i) == a.end()``` is not ```find(a.begin(), a.end(), s-i) != a.end()``` which i think is the suitable logic – singing sun Jan 27 '20 at 16:34
  • ```find(a.begin(), a.end(), s-i) != a.end``` will always be ```true``` if ```s-i``` is found in the vector. ***my thought***:```find(a.begin(), a.end(), s-i) ``` returns a iterator to ```s-i``` if ```s-i``` is found. cleary find didnot returned ```a.endl()``` in this case. so ```find(a.begin(), a.end(), s-i) != a.end``` stands ```true``` and thus allows ```if```conditional to print ```i```. – singing sun Jan 27 '20 at 16:43
  • sir/madam please do help me with my doubt – singing sun Jan 27 '20 at 16:46
  • @noobCoder003 I think you're misreading something. [The actual editorial text](https://codeforces.com/blog/entry/73051) states "determine if either s−x or s+x is ... **not [in] the closed list**", hence the check for `== end()`. – Salem Jan 27 '20 at 17:18
  • oh sorry for that. i must have checked it again. i am really sorry for that – singing sun Jan 27 '20 at 17:29
4

According to the documentation (http://www.cplusplus.com/reference/algorithm/find/):

Returns: An iterator to the first element in the range that compares equal to val. If no elements match, the function returns last.

find(a.begin(), a.end(), s-i) tries to find s-i in a, and returns a.end() on failure.

find(a.begin(), a.end(), s-i) == a.end() is a boolean expression that evaluates to true when s-i is not found in a, and false otherwise.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Nightara
  • 117
  • 5