Pointers in Containers in C++ STL
Last Updated :
09 Jan, 2023
Prerequisites:
There are different containers in C++ having their own features where if are adding extra functionality of pointer it will contribute in a different way. Although not all of the containers are useful in this context so let’s see a few of the containers which can be helpful.
There are 5 containers that can be helpful:
- Using vector
- Using array
- Using map
- Using pairs
- Using stack
How to create Pointers in Containers?
1. Using Vector
Using vectors to create a container of pointers is the most easiest and effective way. It is easy to use also as a vector has dynamic size we can store as many addresses as we want.
Example:
C++
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector< int *> v;
int * a = new int { 42 };
v.push_back(a);
int * b = new int { 51 };
v.push_back(b);
int * c = new int { 66 };
v.push_back(c);
for ( int i = 0; i < v.size(); i++)
cout << *(v[i]) << " " ;
}
|
2. Using Arrays
Can also be called a Pointer array as each element of the array stores the memory address of the variable.
Example:
C++
#include <iostream>
using namespace std;
int main()
{
int * arr[5];
int a[5];
for ( int i = 0; i < 5; i++) {
arr[i] = &a[i];
}
cout << "Arrays contains " ;
for ( int i = 0; i < 5; i++) {
cout << *(arr[i]) << " " ;
}
return 0;
}
|
Output
Arrays contains 6294936 0 -554512983 32640 1
3. Using the map
Maps are associative containers that store elements in a mapped fashion. Each element has a key value and a mapped value. No two mapped values can have the same key values.
The use of a Pointer is quite common with conditions where we want to store an address as a value and a fixed value as a key. For example, we can use it with Linked Lists or with Trees, wherever Nodes are being used.
Example:
C++
#include <iostream>
#include <map>
using namespace std;
int main()
{
map<string*, string> myMap;
map<string*, string>::iterator itr;
string str = "Hello" ;
string* s = &str;
string str1 = "world" ;
string* s1 = &str1;
myMap[s] = str;
myMap[s1] = str1;
cout << "\tAddress(Key)\t String(ELEMENT)\n" ;
for (itr = myMap.begin(); itr != myMap.end(); ++itr) {
cout << '\t' << itr->first << '\t' << '\t'
<< itr->second << '\n' ;
}
return 0;
}
|
Output
Address(Key) String(ELEMENT)
0x7ffce12ebeb0 Hello
0x7ffce12ebec0 world
4. Using Pairs
Pair is used to combine together two values that may be of different data types. Pair provides a way to store two heterogeneous objects as a single unit. It is basically used if we want to store tuples. The first element is referenced as ‘first’ and the second element as ‘second’ and the order is fixed (first, second).
Pair can be quite helpful where we want to store a value and address simultaneously.
Example:
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
pair<string, string*> pair;
string s = "gfg" ;
pair.first = s;
pair.second = &s;
cout << "String "
<< " "
<< "Address pointer" << endl;
cout << pair.first << " " ;
cout << pair.second << endl;
return 0;
}
|
Output
String Address pointer
gfg 0x7fff1e7ceab0
5. Using stack
Stacks are a type of container adapter with LIFO(Last In First Out) type of work, where a new element is added at one end (top) and an element is removed from that end only.
A pointer can be used with stack in a few cases where we want to store addresses rather than values.
Example:
C++
#include <iostream>
#include <stack>
using namespace std;
int main()
{
stack< int *> stack;
int * a1 = new int { 10 };
int b = 20;
int * b1 = &b;
stack.push(a1);
stack.push(b1);
cout << "Address of the inserted elements" << endl;
while (!stack.empty()) {
cout << "Address:" << stack.top()
<< " Value:" << *stack.top() << endl;
stack.pop();
}
return 0;
}
|
Output
Address of the inserted elements
Address:0x7ffc39b9eec4 Value:20
Address:0xde62c0 Value:10
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...