Beginner’s Guide to the Standard C ++ Template Library

0

C ++ is one of the most powerful and intimidating programming languages ​​that you will come across as a beginner. The reason is quite simple. It takes a lot of code to get the desired result. The Standard Model Library, or STL, can help you solve this puzzle.


Because of the time and effort spent writing code for functions like sorting and searching, STL can help you do all of these operations with a single line of code. This library can be extremely useful for problem solving and preparing for technical interviews.


What is the Standard Template Library?

The Standard Template Library, or STL, is a C ++ library that consists of predefined functions and containers. It includes important template classes for common data structures such as vectors, stacks, queues, and some handy algorithmic functions such as binary lookup to facilitate programming.

The standard C ++ template library consists of four components:

  1. Algorithms

  2. Containers

  3. Functions

  4. Iterators

Let’s take a closer look at algorithms and containers, as these are the most commonly used components of STL.

VIDEO OF THE DAY MAKEUSE

STL algorithms

The The header file is a part of the STL which consists of several algorithmic functions which can be used instead of coding them manually. Some of the algorithms included are binary find, sort, and invert, which are extremely useful.

To get started, you need to import the header in your C ++ file. The syntax is as follows:

#include 

For future methods, consider an array variable with the values ​​of {6, 2, 9, 1, 4} as an example.

int arr[] = {6, 2, 9, 1, 4};

sort ()

The sort () The function helps you sort all the elements inside the specified data structure in ascending order. This function takes two parameters: the start iterator and the end iterator.

Related: Introduction to the Merge Sort Algorithm

Syntax:

sort(start_iterator, end_iterator);

Here’s a quick example:

sort(arr, arr+5);
for (int i = 0; i < 5; i++) {
cout << arr[i] << " ";
}

Go out:

1 2 4 6 9

reverse()

The reverse() The function reverses the order of the elements in the specified data structure. It accepts two parameters: the starting iterator and the ending iterator.

Syntax:

reverse(start_iterator, end_iterator);

Here is a short example of the above method:

reverse(arr, arr+5);
for(int i = 0; i < 5; i++) {
cout << arr[i] << " ";
}

Go out:

4 1 9 2 6

* min_element () and * max_element ()

Functions * max_element () and * min_element () return the maximum and minimum value, respectively, within the specified data structure. These two functions take two arguments: the start iterator and the end iterator.

Syntax:

*max_element(start_iterator, end_iterator);
*min_element(start_iterator, end_iterator);

Let’s see what values ​​these functions return by calling them on the example array:

cout << *max_element(arr, arr+5) << endl;
cout << *min_element(arr, arr+5) << endl;

Go out:

9
1

The binary search () The method is used to determine whether or not the specified value is present in the data structure. It accepts three arguments: the start iterator, the end iterator, and the value you want to find.

Binary search only works on sorted data structures. Therefore, you will need to call the sort () method before binary search () method.

Syntax:

binary_search(start_iterator, end_iterator, value_to_find)

Here is a demonstration of this method:

sort(arr, arr+5);
binary_search(arr, arr+5, 2) ? cout << "Element found" : cout << "Element not found";
binary_search(arr, arr+5, 7) ? cout << "Element found" : cout << "Element not found";

Go out:

Element found
Element not found

to count()

The to count() The method returns the number of occurrences of the specified value in the data structure. It takes three arguments: the start iterator, the end iterator, and the value to count.

Syntax:

count(start_iterator, end_iterator, value_to_count);

Here is an example of this method:

cout << count(arr, arr+5, 2) << endl;

Go out:

1

STL containers

Containers are the data structures that store objects and data. Vectors, lists, stacks, queues, sets, and maps are some of the examples that store data in them according to the specified primitive data type. You can use these containers by importing their respective headers into the C ++ file.

When initializing the container variable, you must mention the primitive data such as whole, charred, string of characters inside of supports.


Let’s explore some of these containers in more detail:

Vector

Vectors are dynamic arrays that are resizable and flexible to use. When you insert or remove an element from the vector, it automatically adjusts the size of the vector. This is similar to the ArrayList data structure in Java.

Syntax:

#include 
vector variable_name;

Here are some important vector methods:

  1. push_back (value): This method adds the data to the vector.
  2. pop_back (): This method removes the last element of the vector.
  3. insert (index, value): This method inserts new elements before the element at the specified position.
  4. Cut(): This method returns the size of the vector.
  5. empty(): This method checks if the vector is empty or not.
  6. front(): This method returns the first value of the vector.
  7. back(): The back method returns the last value of the vector.
  8. at (index): This method returns the value at the specified position.
  9. erase (index): The erase method removes items from the given index.
  10. clear (): This method erases all the elements of the vector.

vector < int > v = { 23, 12, 56, 10 };
v.push_back(5);
v.push_back(25);
v.pop_back();
auto i = v.insert(v.begin() + 1, 7);
cout << "The size of the given vector " << v.size() << endl;
if (v.empty()) {
cout << "Vector is empty" << endl;
} else {
cout << "Vector is not empty" << endl;
}
cout << "Element at the first position is " << v.front() << endl;
cout << "Element at the last position is " << v.back() << endl;
cout << "Element at the given position is " << v.at(4) << endl;
v.erase(v.begin() + 1);
for (int i = 0; i < v.size(); i++) {
cout << v[i] << " ";
}

Go out:

The size of the given vector 6
Vector is not empty
Element at the first position is 23
Element at the last position is 5
Element at the given position is 10
23 12 56 10 5

Waiting line

In the queue data structure, items are inserted at the back and deleted at the front. Therefore, it follows the FIFO (“first in, first out”) approach.

Syntax:

#include 
queue variable_name;

Here are some important queuing methods:

  1. push (value): This method adds items to the queue.
  2. pop(): This method removes the first item from the queue.
  3. Cut(): This method returns the size of the queue.
  4. front(): This method returns the first item in the queue.
  5. back(): This method returns the last item in the queue.
queue < int > q;
q.push(30);
q.push(40);
q.push(50);
q.push(60);
q.push(70);
cout << "The first element is " << q.front() << endl;
cout << "The last element is " << q.back() << endl;
cout << "The size of queue is " << q.size() << endl;
q.pop();
cout << "Printing all the elements of the Queue" << endl;
while (!q.empty()) {
cout << q.front() << " ";
q.pop();
}

Go out:

The first element is 30
The last element is 70
The size of the queue is 5
Printing all the elements of the Queue
40 50 60 70

Stack

Stacked containers work according to the LIFO method. LIFO stands for “last in, first out”. Data is pushed and pulled from the same end.

Syntax:

#include 
stack variable_name;

Here are some important stack methods:

  1. push (value): This method pushes the item onto the stack.
  2. pop(): This method removes the top item from the stack.
  3. High(): This method returns the value of the last element entered in the stack.
  4. Cut(): This method returns the size of the stack.
  5. empty(): This method checks if the stack is empty or not.
stack < int > s;
s.push(30);
s.push(40);
s.push(50);
s.push(60);
cout << "The top of the stack contains " << s.top() << endl;
s.pop();
cout << "The top of the stack after performing pop operation: " << s.top() << endl;
cout << "Printing all elements of the stack" << endl;
while (!s.empty()) {
cout << s.top() << " ";
s.pop();
}

Go out:

The top of the stack contains 60
The top of the stack after performing pop operation: 50
Printing all elements of the stack
50 40 30

Adjust

Set containers are used to hold unique values, and the item’s value cannot be changed once it is inserted into the set. All the elements of the set are stored in a sorted manner. The set container is similar to the set data structure in Python.

Syntax:

#include 
set variable_name;

Here are some important set methods:

  1. insert (value): This method inserts elements into the set.
  2. to start(): This method returns the iterator to the first element of the set.
  3. to finish(): This method returns the iterator to the last element of the set.
  4. Cut(): This method returns the size of the set.
  5. empty(): This method checks whether the set is empty or not.
  6. find (value): This method returns the iterator to the element passed as a parameter. If the element is not found, this function returns the iterator at the end of the set.
  7. clear (value): This method removed the specified item from the set.

set < int > s;
s.insert(20);
s.insert(30);
s.insert(40);
s.insert(50);
s.insert(60);
s.insert(60);
s.insert(60);
auto i = s.begin();
cout << "Element at the first position " << * i << endl;
cout << "The size of the set " << s.size() << endl;
s.find(20) != s.end() ? cout << "Element found" << endl : cout << "Element not found" << endl;
s.erase(30);
cout << "Printing all the elements" << endl;
for (auto i = s.begin(); i != s.end(); i++) {
cout << * i << " ";
}

Go out:

Element at the first position 20
The size of the set 5
Element found
Printing all the elements
20 40 50 60

C ++ doesn’t have to be difficult

Like any other skill, practice is essential to getting the most out of the STL. These containers and algorithms can save you a lot of time and are easy to use. Start by practicing the examples above and you’ll end up using it in your own projects as well.


However, if this is your first time learning C ++, start by learning the basics before you start understanding STL.


learn-cpp-featured-programming-language
How to learn C ++ programming: 6 sites to get started

Do you want to learn C ++? Here are the best C ++ websites and online courses for new and experienced programmers.

Read more


About the Author


Source link

Share.

Comments are closed.