A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/cplusplus/cpp_stl_tutorial.htm below:

C++ STL Tutorial

C++ STL Tutorial

Hope you have already understood the concept of C++ Template which we have discussed earlier. The C++ STL (Standard Template Library) is a powerful set of C++ template classes to provide general-purpose classes and functions with templates that implement many popular and commonly used algorithms and data structures like vectors, lists, queues, and stacks.

At the core of the C++ Standard Template Library are following three well-structured components −

Sr.No Component & Description 1

Containers

Containers are used to manage collections of objects of a certain kind. There are several different types of containers like deque, list, vector, map etc.

2

Algorithms

Algorithms act on containers. They provide the means by which you will perform initialization, sorting, searching, and transforming of the contents of containers.

3

Iterators

Iterators are used to step through the elements of collections of objects. These collections may be containers or subsets of containers.

We will discuss about all the three C++ STL components in next chapter while discussing C++ Standard Library. For now, keep in mind that all the three components have a rich set of pre-defined functions which help us in doing complicated tasks in very easy fashion.

Example

Let us take the following program that demonstrates the vector container (a C++ Standard Template) which is similar to an array with an exception that it automatically handles its own storage requirements in case it grows −

#include <iostream>
#include <vector>
using namespace std;
 
int main() {

   // create a vector to store int
   vector<int> vec; 
   int i;

   // display the original size of vec
   cout << "vector size = " << vec.size() << endl;

   // push 5 values into the vector
   for(i = 0; i < 5; i++) {
      vec.push_back(i);
   }

   // display extended size of vec
   cout << "extended vector size = " << vec.size() << endl;

   // access 5 values from the vector
   for(i = 0; i < 5; i++) {
      cout << "value of vec [" << i << "] = " << vec[i] << endl;
   }

   // use iterator to access the values
   vector<int>::iterator v = vec.begin();
   while( v != vec.end()) {
      cout << "value of v = " << *v << endl;
      v++;
   }

   return 0;
}

When the above code is compiled and executed, it produces the following result −

vector size = 0
extended vector size = 5
value of vec [0] = 0
value of vec [1] = 1
value of vec [2] = 2
value of vec [3] = 3
value of vec [4] = 4
value of v = 0
value of v = 1
value of v = 2
value of v = 3
value of v = 4

Here are following points to be noted related to various functions we used in the above example −

C++ Standard Template Library Components

At the core of the C++ Standard Template Library are following four well-structured components −

Containers

Containers are data structures that are used to store and manage collections of data or objects such as vectors, lists, sets, and maps. Here we will see several types of Containers.

1. Sequence Containers

They are containers that store elements in a specific linear order. They allow for direct access to elements and functionalities to manage the order and arrangement of elements.

2. Associative Containers

It stores elements in a sorted order which allows fast retrieval based on keys. These containers work in a key-value pair structure, where each element is associated with a unique key. This key is used for quick access to the corresponding value.

3. Unordered Associative Containers

It stores elements in an unordered manner allowing for efficient access, insertion, and deletion based on keys. Instead of maintaining a sorted order of elements they use hashing to organize data.

4. Container Adapters

It provides a different interface for existing containers.

Algorithms

Algorithms in the C++ Standard Template Library (STL) is a big collection of functions which is specifically designed to perform operations on containers. Where these are implemented using iterators which are used to traverse containers without the need to know their internal structure.

Non-modifying Sequence Algorithms 1. Modifying Sequence Algorithms 2. Sorting Algorithms 3. Searching Algorithms 4. Heap Algorithms 5. Set Algorithms 6. Numeric Algorithms 7. Other Algorithms Iterators

Iterators in the C++ Standard Template Library (STL) are objects that act as pointers to the elements within a container which provides a uniform interface for accessing and manipulating data. They serve as a bridge between algorithms and containers.

Function Objects (Functors)

A functor (or function object) in C++ is an object that can be called as if it were a function. It can be invoked like regular functions. This capability is achieved by overloading the function call operator().

Here you will explore different types of functors based on the operations they perform.

1. Arithmetic Functors

In C++, arithmetic operators are used to perform basic mathematical operations. Here are the common arithmetic operators along with examples.

2. Comparison Functors

Comparison Functors are used for comparing values, especially for sorting or searching in containers.

3. Logical Functors

Logical functors perform logical operations and can be useful in scenarios involving boolean logic.

4. Bitwise Functors Utilities

Utility refers to a collection of general-purpose components which provide basic functionalities in programming, and allow for efficient operations on data and types.

1. Pair and Tuple 2. Smart Pointers 3. Optional, Variant, and Any Type (C++17) 4. Memory Management and Numeric Limits 5. Type Traits

It's a set of templates that allow you to perform compile-time type checks.

Basic STL Features: Simple Example

Here's a simple example demonstrating some basic STL features

#include <iostream>
#include <vector>
#include <algorithm>

int main() {
   std::vector<int> vec = {5, 2, 8, 1, 3};

   // Sorting the vector
   std::sort(vec.begin(), vec.end());

   // Displaying sorted elements
   std::cout << "Sorted vector: ";
   for (int num : vec) {
      std::cout << num << " ";
   }
   std::cout << std::endl;

   // Finding an element
   auto it = std::find(vec.begin(), vec.end(), 3);
   if (it != vec.end()) {
      std::cout << "Element 3 found at position: " 
         << std::distance(vec.begin(), it) << std::endl;
   } else {
      std::cout << "Element 3 not found" << std::endl;
   }

   return 0;
}
Output
Sorted vector: 1 2 3 5 8 
Element 3 found at position: 2
Explanation

This code provides the use of STL in various places,

Benefits of Using STL

Using the Standard Template Library (STL) in C++ has several benefits because it provides a big collection of pre-defined data structures and algorithms, which saves time and effort and reduces the need to implement these from scratch. It also promotes code reuse, modularity, type safety, and flexibility. Which makes the same code work with different data types. Overall it enhances programming efficiency and code quality.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4