×

Computing index using Pointers Returned by STL Functions in C++

In this tutorial we will learn how to compute index using pointers which returned by STL functions in C++. Many built-in C++ functions return pointers to memory places that provide an address for the requested number but have no relationship to the computed value's actual index in a container. To find the maximum element in a code, for example, we use std::max element(), which returns the memory location rather than the index of the needed element. Because we sometimes need the index corresponding to that address, the following example explains how we can do so.

C++ Program:

// C++ code displaying max element's return value ()
#include <bits/stdc++.h>
using namespace std;


int main ()
{
	// initialization of vector
	vector<int> vi1 = { 2, 4, 5, 7, 3 };


	// printing maximum element
	cout << "The maximum element is : "
		<< ( *max_element ( vi1.begin (), vi1.end () ) );
	// printing the position in memory
	cout << "\nThe position of the maximum element is : ";
	printf ( "%d", max_element ( vi1.begin (), vi1.end () ) );
}

Output:

The maximum element is : 7
The position of the maximum element is : 202059452

The output in the preceding output does not appear to be correct, hence there is a need for methods to compute the exact index of the position of the requested element in the supplied container. These are some examples:

  • Subtracting the first iterator
  • Using std::distance()
  • Subtracting the First Iterator: In this method, we simply subtract the container's beginning pointer, in the case of vectors, the "begin()" iterator that corresponds to the address of the container's first element; by subtracting this, we may obtain the exact index of the needed value with respect to the container.

C++ Program:

// CPP code displaying how to print the exact position after subtracting the first iterator.
#include <bits/stdc++.h>
using namespace std;


int main ()
{
	// initialization vector
	vector<int> vi1 = { 2, 4, 5, 7, 3 };


	// printing the maximum element
	cout << "The max element is : "
		<< ( *max_element ( vi1.begin (), vi1.end () ) );


	// printing the position
	cout << "\nThe position of maximum element is : "
		<< max_element ( vi1.begin (), vi1.end () ) - vi1.begin ();
	;
	return 0;
}

Output:

The max element is : 7
The position of maximum element is : 3
  • Using std::distance() : Another option for computing the desired position is to use distance(), which bypasses the first iterator as the first parameter and the intended point as the second.

C++ Program:

// C++ code showing how to output a precise position using distance ()
#include <bits/stdc++.h>
using namespace std;


int main ()
{
	// initialization of vector
	vector<int> vi1 = { 2, 4, 5, 7, 3 };


	// printing the maximum element
	cout << "The maximum element is : "
		<< ( *max_element ( vi1.begin (), vi1.end () ) );


	// printing the position
	cout << "\nThe position of maximum element is : ";
	cout << distance ( vi1.begin (),max_element ( vi1.begin (), vi1.end () ) );
	return 0;
}

Output:

The maximum element is : 7
The position of maximum element is : 3

Related Topics

Abstract class in C++

In this article, you will get exposure to an abstract class in C++. We will discuss this topic using some practical examples too. To understand the abstract classes, you should...

6 minutes read.

Returning a Function Pointer from a Function in C/C++

Pointers to functions can be used in the C programming language just like standard data pointers such as "int *," "char *," etc. The following is a basic example of a...

3 minutes read.

C++ Pipe Tutorial

A pipe is a mechanism for inter-process communication (IPC) in a Unix-like operating system. It allows two or more processes to communicate with each other by sending and receiving data...

3 minutes read.

Binary to Decimal in C++

We must create a software to convert a binary number into an equivalent decimal value given a binary number as input. Example: // C++ program to convert binary to decimal #include < iostream...

3 minutes read.

Palindrome using Do-while loop in C++

What is Palindrome? A palindrome is a word, number, phrase, or other sequence of letters that reads the same backward as forward, such as 101 or MOM. Like other programming languages, C++...

5 minutes read.

Scope Resolution Operator vs this Pointer in C++

In this tutorial, we will compare the Scope Resolution operation to this Pointer in C++ language. Scope Resolution Operator The Scope Resolution Operator in C++ programming language is usually denoted by (::)....

3 minutes read.

Computing index using Pointers Returned by STL Functions in C++

In this tutorial we will learn how to compute index using pointers which returned by STL functions in C++. Many built-in C++ functions return pointers to memory places that provide...

2 minutes read.

Difference between exit() and _Exit() in C++

Before understanding the difference between the exit() and _Exit(), one must know about exit() and _Exit() functions. The exit() function in C/C++ The exit() method in the C language kills the calling...

3 minutes read.

C++ String Class and its Applications

The String class is available in C++. The character array is represented by the C string. The string class in C++ has a few different attributes. It contains several functions...

4 minutes read.

Type conversion in C++

Type conversion is the process of changing the type of variables in a program. The ultimate goal of type conversions is to allow variables of a single data type operate with...

7 minutes read.

C++ storage classes

Storage Classes are used to characterize a variable's or function's characteristics. These characteristics include scope, visibility, and life-time, which allow us to track the presence of a variable over the...

5 minutes read.

Dynamic Memory Allocation in C++

In some programming situations, the number of data items changes as the program is running, which is known as dynamic data or input. Consider a real-world situation where a program...

3 minutes read.

LCM Program in C++

What is LCM? LCM is an acronym for "least common multiple". It is used to discover the lowest positive integer divisible by all integers (whose LCM is calculated). For instance, the...

4 minutes read.

C++ OOPs Concept

The main goal of C ++ programming is to add the idea of ​​object orientation to the C programming language. Inheritance, data binding, polymorphism and other concepts are part of...

4 minutes read.

C++ program to read string using cin.getline()

C++ program to read string using cin.getline() C++ getline() is a standard library feature for reading a string or a line from an input source. A getline() function gets characters from...

3 minutes read.

Difference between Exit and Return

Define Exit() At the point when a client needs to leave a program from this capability is utilized. A void return type capability calls all capabilities enrolled at the exit and ends...

3 minutes read.

C++ add two numbers using the function

Here we will learn how to add two numbers by creating function in C++. Let’s learn this with help of example. Code: - #include <iostream> using namespace std; int add_two_no(int a, int b); int main(){   int...

1 minute read.

Memset in C++

Memset () is a function in the C++ programming language that fills memory blocks. The value of "ch" is first converted to an unsigned character. In this case, "ch" denotes the...

3 minutes read.

Iostream in C++

Using Iostream in C++, we can perform input and output operation capabilities. This represents input and output, and the stream is used to carry out this capability. A stream is...

4 minutes read.

Initialization of Data Members

In this tutorial, we'll look at how to initialise static member variables in C++. Static members, such as functions or variables, can be added to C++ classes. After declaring the...

1 minute read.