Ari  Bogisich

Ari Bogisich

1593029340

Standard Template Library (STL) in C++ | Vectors

The Standard Template Library (STL) in C++ is nothing but a set of template classes which provides us with the widely used data structures such as lists, stacks, maps, etc. along with iterators, functions and algorithms in order to play around with the data elements.

A. Main Components of STL

1.Containers

Containers are nothing but holder objects which stores other objects i.e. it’s elements. A container does the memory management for its elements and provides us with member functions or iterators in order to interact with it’s data elements.

2.Algorithms

Algorithms, defined under the header , are nothing but a collection of several functions which are designed in order to used on any sequence of elements that can be accessed using iterators or pointers. One thing to note about algorithms is that they do not change the way a data structure is organised in any way. They merely deal with the elements within the structure.

3.Iterators

Iterators are something which is used to point at the memory addresses of STL containers’ elements. Thus providing a means to access the data stored within the container. They can be simply thought of as pointers. More about them can be read here.

Now with that being said we are good to go ahead and have a look at our very first container Vector.


B. Vectors in STL

We all know about the commonly used data structure, arrays. A vector can be simply thought of as an array with the added functionality that it’s size can vary over time. Thus we can say that vectors are sequence containers representing arrays that can change it’s size dynamically.

Just like an array, a vector too uses contiguous memory allocation for its elements thus we can make use of offsets in order to access the elements of a vector. Internally vectors make use of dynamically allocated arrays. This array may need to be reallocated in order to grow in size thus resulting in a costly operation of reallocation and copying the elements. Thus vectors do not reallocate each time instead they have an extra amount of space which is greater than their actual size (current size of the vector) and re-allocations usually happen at logarithmic intervals. Thus, vectors occupy more space when compared to an array in exchange of dynamic nature and efficient memory management.

C. Let’s have a look at the syntax

We need to import the header  so that we can make use of vectors

#include <vector>

Now, in order to declare a vector we use the following syntax

vector vector_name;

Eg : vector v;

This declares a vector with 0 elements in it.

D. Different ways of vector declaration and initialisation

#include <iostream>
#include <vector>

using namespace std;
int main()
{
 vector <int> myVec;     // simple declaration
 myVec.assign(10, 33);   // 10 ints with a value of 33

 vector <int> v{1,2,3};  // initialise on the go
 vector <int> v1(3);     // 3 ints with value of 0
 vector <int> v2(3,7);   // 3 ints with value of 7
 return 0;
}

#vector #standard-template-library #cplusplus #stl #programming-c

What is GEEK

Buddha Community

Standard Template Library (STL) in C++ |  Vectors
Ari  Bogisich

Ari Bogisich

1593029340

Standard Template Library (STL) in C++ | Vectors

The Standard Template Library (STL) in C++ is nothing but a set of template classes which provides us with the widely used data structures such as lists, stacks, maps, etc. along with iterators, functions and algorithms in order to play around with the data elements.

A. Main Components of STL

1.Containers

Containers are nothing but holder objects which stores other objects i.e. it’s elements. A container does the memory management for its elements and provides us with member functions or iterators in order to interact with it’s data elements.

2.Algorithms

Algorithms, defined under the header , are nothing but a collection of several functions which are designed in order to used on any sequence of elements that can be accessed using iterators or pointers. One thing to note about algorithms is that they do not change the way a data structure is organised in any way. They merely deal with the elements within the structure.

3.Iterators

Iterators are something which is used to point at the memory addresses of STL containers’ elements. Thus providing a means to access the data stored within the container. They can be simply thought of as pointers. More about them can be read here.

Now with that being said we are good to go ahead and have a look at our very first container Vector.


B. Vectors in STL

We all know about the commonly used data structure, arrays. A vector can be simply thought of as an array with the added functionality that it’s size can vary over time. Thus we can say that vectors are sequence containers representing arrays that can change it’s size dynamically.

Just like an array, a vector too uses contiguous memory allocation for its elements thus we can make use of offsets in order to access the elements of a vector. Internally vectors make use of dynamically allocated arrays. This array may need to be reallocated in order to grow in size thus resulting in a costly operation of reallocation and copying the elements. Thus vectors do not reallocate each time instead they have an extra amount of space which is greater than their actual size (current size of the vector) and re-allocations usually happen at logarithmic intervals. Thus, vectors occupy more space when compared to an array in exchange of dynamic nature and efficient memory management.

C. Let’s have a look at the syntax

We need to import the header  so that we can make use of vectors

#include <vector>

Now, in order to declare a vector we use the following syntax

vector vector_name;

Eg : vector v;

This declares a vector with 0 elements in it.

D. Different ways of vector declaration and initialisation

#include <iostream>
#include <vector>

using namespace std;
int main()
{
 vector <int> myVec;     // simple declaration
 myVec.assign(10, 33);   // 10 ints with a value of 33

 vector <int> v{1,2,3};  // initialise on the go
 vector <int> v1(3);     // 3 ints with value of 0
 vector <int> v2(3,7);   // 3 ints with value of 7
 return 0;
}

#vector #standard-template-library #cplusplus #stl #programming-c

Ari  Bogisich

Ari Bogisich

1593006480

Standard Template Library (STL) in C++ | Lists

The **Standard Template Library (STL) **is a very useful set of template classes containing various containers. One among these containers is Lists. Today we’ll be having a look at Lists in STL. But before we proceed further I’d like to say that if this is your first time learning STL then I’d recommend you have a look at this article before beginning with this one.

With that being said let’s dive further into the topic by knowing what are lists in STL ?

A. Lists in STL

Lists are a type of sequence containers which provides us with constant insertion and deletion time anywhere within the sequence given that we have obtained the corresponding iterator to the same. It also allows us traversal in both the directions.

List containers are implemented as doubly-linked lists. The memory management is done internally by maintaining a link to the previous as well as the next element in the sequence with respect to each element. A significant drawback of lists being that we don’t have direct access to the elements since it uses non contiguous memory allocation for each element in the sequence. Thus in order to get to a specific element in the sequence one has to iterate through the list which takes linear time. Apart from that it also takes up a bit more extra space when compared to vectors, which it uses to keep track of the links to previous and next elements corresponding to each element in the sequence.

#standard-template-library #lists-in-cplusplus #stl #cplusplus #programming-c

Hudson  Larkin

Hudson Larkin

1600260420

C++ Vector Example | Vector in C++ Tutorial

C++ Vector Example | Vector in C++ Tutorial is today’s topic. C++ Vector is a template class that is a perfect replacement for the right old C-style arrays. It allows the same natural syntax that is used with plain arrays but offers a series of services that free the C++ programmer from taking care of the allocated memory and help to operate consistently on the contained objects.

C++ Vector is a sequence of containers that can be changed dynamically. This means you can change its size any time you want. Like an array, it also takes contiguous memory location, but the difference is Vector is dynamic where Array is not. So, let’s start the C++ Vector Tutorial With Example From Scratch.

C++ STL

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.

#c++ #c++ stl #vector in c++ #c++ programmer

Shaylee  Lemke

Shaylee Lemke

1590240840

The numerical algorithms that are found in the Standard Template Library (STL)

In this article I will discuss the numerical algorithms that are found in the Standard Template Library (STL). These algorithms do not replace the math library for the types of functions found in cmath but provide a different set of algorithms for solving certain numerical processing problems.

#c #c# #c++ #programming-c

Cody  Lindgren

Cody Lindgren

1625736600

Vector | C++ STL (Standard Template Library) | std::vector

In this lesson we study about vector container and different functions of vector.
Complete C++ STL playlist: https://www.youtube.com/playlist?list=PL1w8k37X_6L9NXrP1D31hDTKcdAPIL0cG
Github Link: https://github.com/KnowledgeCenterYoutube/STL/blob/master/Vectors

#stl #vector #standardtemplatelibrary #CPP

#stl #vector #cpluplus