Pointers in Containers in C++ STL
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++
// C++ Program to implement // Vector of Pointers #include <iostream> #include <vector> using namespace std; int main() { // creating a vector of pointers vector< int *> v; // inserting in the vector 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); // getting the value of the elements using their address for ( int i = 0; i < v.size(); i++) cout << *(v[i]) << " " ; } |
42 51 66
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++
// C++ Program to implement // Array of Pointers #include <iostream> using namespace std; int main() { // creating pointer array int * arr[5]; int a[5]; // inserting in the array for ( int i = 0; i < 5; i++) { arr[i] = &a[i]; } // printing the elements of the array using the address // of the elements cout << "Arrays contains " ; for ( int i = 0; i < 5; i++) { cout << *(arr[i]) << " " ; } return 0; } |
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++
// C++ Program to implement // Map of Pointers #include <iostream> #include <map> using namespace std; int main() { // creating map to string to // pointers of the string 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; } |
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++
// C++ Program to demonstrate // use of pointers with pair #include <bits/stdc++.h> using namespace std; int main() { pair<string, string*> pair; string s = "gfg" ; // first part of the pair storing the string pair.first = s; // second part of the pair is a pointer storing the // address of the string pair.second = &s; // printing the data stored in the pair cout << "String " << " " << "Address pointer" << endl; cout << pair.first << " " ; cout << pair.second << endl; return 0; } |
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++
// C++ Program to implement // Pointer use with stacks #include <iostream> #include <stack> using namespace std; int main() { // defining a pointer stack stack< int *> stack; // initialising a1 pointer variable int * a1 = new int { 10 }; int b = 20; // another way of initialising b1 pointer // variable int * b1 = &b; // inserting in the stack stack.push(a1); stack.push(b1); // printing the elements of the stack cout << "Address of the inserted elements" << endl; while (!stack.empty()) { cout << "Address:" << stack.top() << " Value:" << *stack.top() << endl; stack.pop(); } return 0; } |
Address of the inserted elements Address:0x7ffc39b9eec4 Value:20 Address:0xde62c0 Value:10
Please Login to comment...