
C++ is a general-purpose programming language that can be used for many tasks. However, to make programming easier in C++, we use a library called the Standard Template Library (STL). The STL in C++ is a set of tools that includes different templates, which are ready-made structures like containers, iterators, algorithms, and function objects. In this tutorial, you will learn everything you need to know about the STL in C++ in a simple and easy-to-understand way.
| Array In STL In C++ |
| array<int, 5> myArray; |
| Vectors STL In C++ |
| #include <iostream> #include <vector> using namespace std; int main() { vector<int> myVector; myVector.push_back(1); myVector.push_back(2); myVector.push_back(3); cout << "Elements in the vector: "; for (int i = 0; i < myVector.size(); ++i) { cout << myVector[i] << " "; } cout << endl; for (const auto& element : myVector) { cout << element << " "; } cout << std::endl; myVector.push_back(4); myVector.push_back(5); for (const auto& element : myVector) { std::cout << element << " "; } std::cout << std::endl; return 0; } |
| List STL in C++ |
| #include <iostream> #include <list> Using namespace std; int main() { list<int> myList; myList.push_back(1); myList.push_front(2); myList.push_back(3); myList.push_front(4); for (const auto& element : myList) { cout << element << " "; } cout << endl; return 0; } |
| Unordered Map STL in C++ |
| unordered_map<string,int> freqWords; string words[]={“apple”, “Banana”, “apple”, “orange”} |
| Unordered Set STL in C++ |
| unordered_set<int> mySet; mySet.insert(1); mySet.insert(2); mySet.insert(1); mySet.insert(3); mySet.insert(5); for(const auto & element: mySet){ cout<<element<” “; } |
| Output |
| 1 2 3 5 |
| Stack In C++ In STL |
| #include <stack> int main() { stack<int> numbers; numbers.push(1); numbers.push(2); numbers.push(3); int item = numbers.top(); numbers.pop(); cout << item << endl; return 0; } |
| Output |
| 3 |
| Queue In C++ In STL |
| #include <queue> int main() { queue<int> numbers; numbers.push(1); numbers.push(2); numbers.push(3); int item = numbers.front(); numbers.pop(); cout << item << endl; return 0; } |
| Output |
| 1 |
| Dequeue STL In C++ |
| #include <deque> int main() { deque<int> numbers; numbers.push_front(6); numbers.push_front(4); numbers.push_front(9); int item = numbers.front(); numbers.pop_front(); cout << item << endl; numbers.push_back(4); numbers.push_back(5); numbers.push_back(6); item = numbers.back(); numbers.pop_back(); cout << item << endl; return 0; } |
| Output |
| 9 6 |
Image Source[/caption]
Types of algorithms: