Why std::find returns a pointer?

Why the find() method uses a pointer. That is, in this example

#include <iostream>     // std::cout
#include <algorithm>    // std::find
#include <vector>       // std::vector

int main () {
  // using std::find with array and pointer:
  int myints[] = { 10, 20, 30, 40 };
  int * p;

  p = std::find (myints, myints+4, 30);
  if (p != myints+4)
    std::cout << "Element found in myints: " << *p << '\n';
  else
    std::cout << "Element not found in myints\n";

Why use the pointer p, and not just the variable p. We are not going to change the array element in any way. We just need to search for it in the array and that's it. Why can't this be done without a pointer? Thank you in advance!

Author: Qwertiy, 2021-01-10

3 answers

Hello! And why, after the request "find a person with a red cap in the crowd ", do you point your finger at this person, and do not answer "the person with a red cap"?..

Accepts any type that behaves as a pointer (итератор для чтения) and returns it(in this case, a pointer). This is a generalized algorithm, and the command "from here to here find this" is logical for it. The algorithm finds and "says" that it has found it в этом месте (returns an iterator pointing to this location) or I didn't find it(returns an iterator pointing beyond the end of the sequence.

Otherwise, you will not build the logic of this kind of command and execution, because if the algorithm somehow just reports what it found, then the question remains: "and in what place?". Even worse, if it returns a value(and what value to return, so that you know that it did not find? And if you found it, why do you need this value, if you already have it?). And knowing the place, all questions are exhausted.

 7
Author: AR Hovsepyan, 2021-01-11 08:46:29
  1. The fact that you are not going to do something in a particular example does not mean that it is not necessary. What find returns can be used to change the value, or even to change the container - for example, to crop the vector after or before the found element. And you can also do something with neighboring elements.

  2. Even if you imagine that for some reason there are two functions-one is looking for a pointer (and in general, it is a template and the pointer is yours option, and there can be any iterator), and the other is a value, then there is a problem with the value. We must somehow be able to distinguish the situation when the value is found from the situation when it is not found. Since we can search for any value, we must somehow ensure that we can return one more value than the type used can hold. For example, when reading a file, the normal characters fit in char, and getc returns int to be able to provide EOF out of the allowed range char. Obviously, for an arbitrary type, this is difficult to do, and almost the only option will be to return a structure with the found value and flag. Stop. The structure with the found value? But do not copy the value into it? Then a structure with a pointer and a flag? And why do we need a flag now, if the pointer itself can be used to show that nothing was found?

  3. Well, specifically about find. You give him the number 30. If you imagine that it will return you are the same 30, then why do you need it? A function that takes a pair of iterators and returns its third argument?

  4. And in your code, you can replace find with count, then you can immediately use the number as a flag. However, formally, the performance of this code will be worse, unless the compiler thinks of something to line and throw out the extra, so I would leave find.

 14
Author: Qwertiy, 2021-01-11 07:14:15

Read in the documentation:

template InputIterator find (InputIterator first, InputIterator last, const T& val); Find value in range Returns an iterator to the first element in the range [first,last) that compares equal to val. If no such element is found, the function returns last.

I.e., the method returns an iterator of the element, it is not really a pointer. And it is made so that when you find the desired element, you can go to to the next or previous element. If the find() method returned the value of the element directly, then its functionality would be limited to this. All for the sake of advanced features, something like that. I hope I got the gist of the question right...

 2
Author: LShadow77, 2021-01-10 23:54:32