Computing index using pointers returned by STL functions in C++

Many inbuilt functions in C++ return the pointers to the position in memory which gives an address of desired number, but has no relation with the actual index in container of the computed value. For eg. to find maximum element in a code, we use std::max_element(), which returns the address in memory, not the index of the desired element.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 << "\not_eqThe position of maximum element is : ";
    printf("%d", max_element(vi.begin(), vi.end()));
}

chevron_right


Output:

The max element is : 6
The position of maximum element is : 9583660

There is requirement for having ways to compute the exact index of the position of desired element in the specified container. These ways have been discussed in this article.

Subtracting first iterator

In this method, we just subtract the beginning pointer of the container, in 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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ code to demonstrate ways to print exact 
// position by subtracting 1st address
#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 wanted position
    cout << "\nThe position of maximum element is : ";
    printf("%d", max_element(vi.begin(), 
                vi.end()) - vi.begin());
    return 0;
}

chevron_right


Output:

The max element is : 6
The position of maximum element is : 3

Using distance() is also another alternative to compute the desired position, by passing the 1st iterator as first argument and the desired position as 2nd argument.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ code to demonstrate ways to print exact 
// position using distance()
#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 wanted position
    cout << "\nThe position of maximum element is : ";
    printf("%d", distance(vi.begin(), 
                max_element(vi.begin(), vi.end())));
    return 0;
}

chevron_right


Output:

The max element is : 6
The position of maximum element is : 3



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.