arr.end() Print Value/ Index Position

Looking at the find function, I'm playing around with this tutorial https://www.learncpp.com/cpp-tutorial/introduction-to-standard-library-algorithms/ & I'm struggling with it.

I'm suprised that std::cout << arr.end(); does not print the integer index of +1 of the array. I think it's for 2 reasons.

1 - arr.end() returns a const expr, which is actually indexed +1 from the last element in the array.

2 - As it's a constant we can't alter it's position.

Am I right? I know how to return values from certain elements in the array. I'm struggling with existing libraries atm. I know their benefits, but struggling with existing libraries atm.


1
2
3
4
5
6
7
8
9
10
11
12
#include <algorithm>
#include <array>
#include <iostream>

int main()
{
    std::array arr{ 13, 90, 99, 5, 40, 80 };

    auto found{ std::find(arr.begin(), arr.end(), 15)};

    std::cout << arr.end();
}
Last edited on
I can't understand your question. Of course you can't change where end() points. Why would you want to? It looks like you might actually want something like this:

1
2
3
4
5
6
7
8
9
10
#include <algorithm>
#include <array>
#include <iostream>

int main()
{
    std::array<int,6> arr{ 13, 90, 99, 5, 40, 80 };
    auto found = std::find(arr.begin(), arr.end(), 15);
    std::cout << std::distance(arr.begin(), found) << '\n'; // prints 6 
}

Last edited on
Thanks dutch, sorry for my ambiguity, I was just trying to understand how it works. You pretty much understood me exactly though so many thanks.
Topic archived. No new replies allowed.