Computing Index Using Pointers Returned By STL Functions in C++ - GeeksforGeeks

04 Jan, 2022
Many inbuilt functions in C++ return the pointers to the position in memory which gives an address of the desired number but has no relation with the actual index in a container of the computed value. For example, to find the maximum element in a code, we use std::max_element(), which returns the address in memory, not the index of the desired element. Sometimes, we need the index corresponding to that address, so the following example demonstrates how we can do that,
CPP
// C++ code to demonstrate return value of// max_element()#include <bits/stdc++.h>using namespace std; int main(){    // initializing vector    vector<int> vi = { 1, 4, 5, 6, 3 };     // printing max element    cout << "The max element is : "         << (*max_element(vi.begin(), vi.end()));     // printing position    // prints position in memory    cout << "\nThe position of maximum element is : ";    printf("%d", max_element(vi.begin(), vi.end()));}
Output
The max element is : 6
The position of maximum element is : 2015522492
The output doesn’t look right in the above output so, there is a requirement for having ways to compute the exact index of the position of the desired element in the specified container. These ways are:
1) Subtracting the first iterator
2) Using std::distance()
In this method, we just subtract the beginning pointer of the container, in the case of vectors its “begin()” iterator that points to the address of 1st element of the container, subtracting this we can get the exact index of the required value w.r.t the container.
CPP
// C++ code to demonstrate ways to print exact// position subtracting first iterator#include <bits/stdc++.h>using namespace std; // Driver Codeint main(){    // initializing vector    vector<int> vi = { 1, 4, 5, 6, 3 };     // printing max element    cout << "The max element is : "         << (*max_element(vi.begin(), vi.end()));     // printing position    // prints wanted position    cout << "\nThe position of maximum element is : "         << max_element(vi.begin(), vi.end()) - vi.begin();    ;    return 0;}
The max element is : 6
The position of maximum element is : 3
Using distance() is also another alternative to compute the desired position, bypassing the 1st iterator as the first argument and the desired position as the 2nd argument.
CPP
// C++ code to demonstrate ways to print exact// position using distance()#include <bits/stdc++.h>using namespace std; // Driver Codeint main(){    // initializing vector    vector<int> vi = { 1, 4, 5, 6, 3 };     // printing max element    cout << "The max element is : "         << (*max_element(vi.begin(), vi.end()));     // printing position    // prints wanted position    cout << "\nThe position of maximum element is : ";    cout<< distance(vi.begin(),max_element(vi.begin(), vi.end()));    return 0;}
The max element is : 6
The position of maximum element is : 3
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
anshikajain26
cpp-vector
STL
C++
STL
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Operator Overloading in C++
Polymorphism in C++
Friend class and function in C++
Sorting a vector in C++
Iterators in C++ STL
Convert string to char array in C++
Inline Functions in C++
List in C++ Standard Template Library (STL)
new and delete operators in C++ for dynamic memory
Multithreading in C++