Containers
Containers are data structures that manage collection of objects.
Array : Array is a data type that stores multiple elements of same data type. We have discussed details about arrays in previous section.
Vectors : These are dynamic arrays that can grow and shrink their size. Elements in vectors can be accessed using indexing.
vector.cpp
#include <iostream>
#include <vector> //header file for vector
using namespace std;
int main(){
//declaration of vector
//vector <datatype> nameOfVector;
vector <int> v;
v.push_back(1);
cout<<"Element in v is "<<v[0]<<endl;
//pushing some more elements
v.push_back(2);
v.push_back(3);
v.push_back(4);
//printing all elements of vector
for(int i=0; i<v.size(); i++){
cout<<"Element "<<i+1<<" is "<<v[i]<<endl;
}
v.pop_back();
cout<<"After poping last element :\n";
for(int i=0; i<v.size(); i++){
cout<<"Element "<<i+1<<" is "<<v[i]<<endl;
}
}
Methods of Vectors:
Methods | |
---|---|
push_back() | |
pop_back() | |
capacity() | |
size() | |
front() | |
back() | |
swap() | |
empty() | |
clear() | |
resize() |
Deque:
Deque is a short from of Double Ended Queue. It allows the insertion and deletion of elements from both ends.deque.cpp
#include <iostream>
#include <deque> //header file for deque
using namespace std;
int main(){
//declaration of deque
//deque <datatype> nameOfDeque;
deque <int> d;
d.push_back(1);
cout<<"Element in v is "<<d[0]<<endl;
d.push_front(33);
d.push_back(2);
d.push_back(3);
d.push_back(4);
//printing all elements of deque
for(int i=0; i<d.size(); i++){
cout<<"Element "<<i+1<<" is "<<d[i]<<endl;
}
//removing elements at the end of deque
d.pop_back();
d.pop_front();
cout<<"After poping last element :\n";
for(int i=0; i<d.size(); i++){
cout<<"Element "<<i+1<<" is "<<d[i]<<endl;
}
}
Lists:
List is a linear data structure that stores elements in a sequence, and each element (node) in the list contains a value and two pointers: one to the previous element and one to the next element.list.cpp
#include <iostream>
#include <list>
using namespace std;
int main() {
std::list<int> myList;
// Inserting at the back of the list
myList.push_back(10);
myList.push_back(20);
myList.push_back(30);
// Inserting at the front of the list
myList.push_front(5);
myList.push_front(15);
// Removing from the list
myList.pop_back();
myList.pop_front();
// Traversing the list using iterators
for (const auto& value : myList) {
cout << value << " ";
}
cout << endl;
return 0;
}
Maps:
Maps are containers that stores elements in key-value pairs where each key is unique.maps.cpp
#include <iostream>
#include <map>
using namespace std;
int main() {
map<string, int> scores;
// Inserting elements into the map
scores["Amy"] = 90;
scores["Daniel"] = 85;
scores["Charlie"] = 95;
// Accessing elements
cout << "Amy's score: " << scores["Amy"];
cout<<endl;
// Traversing the map using a for loop
for (const auto& pair : scores) {
const string& name = pair.first; // Key
int score = pair.second; // Value
cout << name << ": " << score << endl;
}
return 0;
}