Sequence Container in C++
A collection of template classes known as sequence containers in C++ are used to store data elements. Since they are template classes, they can store any data item, even custom classes. They create a sequential-access data structure.
The Standard Template Library for C++ provides five different sequence containers. These include array, vector, deque, forward_list, and list. The container classes array, vector, and deque are implemented using an array data structure. A linked list data structure implements the container classes list and forward_list. Because arrays are static, these two types of data structures fundamentally vary. Therefore, even if there is a possibility to raise the size of the need, the container's size is fixed and established during compilation. Purely dynamic is how the linked-list implementation is used. With each additional element that is stored in the container, it grows larger.
The Standard Template Library (STL) contains five different kinds of sequential containers:
- Array
- Vector
- Deque
- List
- Forward_list
Array:
Arrays are fixed-size containers. They can hold a certain quantity of components. These components are placed in a strict linear sequence. By indicating their index, we can use array items immediately without going through the entire array. Look at the below example:
Example of an Array:
Code:
#include <iostream>
#include <array>
using namespace std;
int main()
{
//Create an array with name nums
array<int, 6> nums = {15, 23, 10, 15, 45, 24};
// create an iterator
array<int, 6>::iterator it;
// iterate each element in the array through using iterator
cout << "The array of number values is: ";
for(it = nums.begin(); it < nums.end(); it++)
{
cout << *it << " ";
}
cout << endl;
cout << "Array's size is: " << nums.size() << endl;
return 0;
}
Output:
![Sequence Container in C++](https://static.tutorialandexample.com/cpp/sequence-container-in-cpp1.png)
Vector:
A random access sequence is known as a std:: vector. We can add and remove values from the end of the std::vector without adding unnecessary effort. It takes longer to insert and remove items at the start or the center of the std:: vector because relocating the other entries is required to make space or close the deleted object space. A std:: vector is a collection of contiguous objects with a pointer or instance counter denoting the container's end. Using a subscript operation allows for random access.
Example of Vector:
Code:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
//Create a vector with the name Value
vector<int>Value;
//iterating through using for loop
for (int val = 1; val <= 15; val = val * 3)
Value.push_back(val);
cout << "Output with Value.begin() and Value.end(): ";
for (auto val = Value.begin(); val != Value.end(); val++)
cout << *val << " ";
cout << "\nOutput with Value.rbegin() and Value.rend(): ";
for (auto ir = Value.rbegin(); ir != Value.rend(); ir++)
cout << *ir << " ";
return 0;
}
Output:
![Sequence Container in C++](https://static.tutorialandexample.com/cpp/sequence-container-in-cpp2.png)
Deque:
Deque is an abbreviation for double-ended queue. A deque is a container with dynamically adjustable size on both ends. Random access iterators, generic pointers with read, write, and random access capabilities.
Example of Deque:
Code:
#include <iostream>
#include <deque>
using namespace std;
int main()
{
deque<int> DQ;
DQ.push_back(7); // append value at the back
DQ.push_front(5); // append value at the front
DQ.push_back(4); // append value at the back
DQ.push_front(2); // append value at the front
deque<int>::iterator it;
cout << "DQ is: ";
for (it = DQ.begin(); it != DQ.end(); it++)
{
cout << *it << " ";
}
cout << "\nDQ.size(): " << DQ.size();
//Remove the initial value of DQ
DQ.pop_front();
cout << "\nDeque after applying pop_front() is: ";
for (it = DQ.begin(); it != DQ.end(); it++)
{
cout << *it << " ";
}
return 0;
}
Output:
![Sequence Container in C++](https://static.tutorialandexample.com/cpp/sequence-container-in-cpp3.png)
List:
You can access a sequence in both directions using a std:: list. You can conduct inserts and deletions everywhere on a std:: list without suffering unjustified performance costs. Iterating to the target item forward or backward simulates random access. Non-contiguous objects connected with forward and backward pointers make up a std:: list.
Example of List:
Code:
#include <iostream>
#include <list>
int main() {
// Declare a list to store integers
std::list<int> intList;
// Add elements to the list
intList.push_back(10);
intList.push_front(5);
intList.push_back(20);
// Access and print the elements using iterators
std::cout << "Elements in the list: ";
for (std::list<int>::iterator it = intList.begin(); it != intList.end(); ++it) {
std::cout << *it << " ";
}
std::cout << std::endl;
// Insert an element after the first element
std::list<int>::iterator insertPos = std::next(intList.begin());
intList.insert(insertPos, 15);
// Remove the second element
std::list<int>::iterator erasePos = std::next(intList.begin());
intList.erase(erasePos);
// Print the updated list
std::cout << "Updated elements in the list: ";
for (int val : intList) {
std::cout << val << " ";
}
std::cout << std::endl;
return 0;
}
Output:
![Sequence Container in C++](https://static.tutorialandexample.com/cpp/sequence-container-in-cpp4.png)
Forward_list:
Forward lists are sequence containers that enable insert and erase operations in constant time, wherever in the sequence they are performed. Using singly linked lists, forward lists are implemented. As a result, each item in a forward list can be kept in a different location in memory.
Example of Forward list:
Code:
#include <iostream>
#include <forward_list>
using namespace std;
int main()
{
//Create a forward list with the name ForwardList
forward_list<int> ForwardList = {21, 22, 23, 24, 25};
// append new values by using the function push_front()
ForwardList.push_front(26);
ForwardList.push_front(27);
// printing the forward list
cout << "Our forward list is: ";
for (int& i : ForwardList)
{
cout << i << " ";
}
cout << endl;
// removing the first element by using the function pop_front()
ForwardList.pop_front();
//Print the modified forward list
cout << "After applying pop_front the ForwardList is : ";
for (int& i : ForwardList)
{
cout << i << " ";
}
return 0;
}
Output:
![Sequence Container in C++](https://static.tutorialandexample.com/cpp/sequence-container-in-cpp5.png)