Skip to main content

Standard template library in modern C++ to simplify opencv development

The STL is way to simplify and speed up your development in any C++ program. Standard templates library contains powerful algorithm, a set of functions and many useful tools for any C++ developer. The components are algorithm, containers, functions and iterators. Algorithms and functions in STL work with containers of built-in type or your own custom containers.

Containers in STL C++  

Containers are the same as in case of the physical world. You can store the something in the container. In this case, containers simplify manipulation and storing of a different type of information. Every container in real life has a different way to organize an order of stored objects(data). 

vector in C++


The popular one is a vector. This is a powerful container to store many pieces of information of one type. You can store vector<int>, vector<double> and vector of your custom type. No magic.The type that comes from opencv vector<Rect> can be stored by vector. Vector is basically the array of elements with big advantages against the C arrays. You can resize them as you like. Just allocate vector<int> and store theoretically as many integers as you like. You can simply insert element and erase elements. Look at the example of how to create vector<rectangles> and add one rectangle at the end by push_back(object)

vector<int>

Let's start with a simple container type like that collect multiple integers <int>. vector<int> is a way to store a sequence of integers. Start by a vector followed by type in bracket <>. You can store inside vector integer numbers, doubles, float and much more including your custom types defined by structures.

This syntax inits the vector with 4 integer numbers. intvector is indexed as an array. You can reach the elements content from the vector by index like in arrays intVector[0] for 1 element  intVector[1] for second  intVector[2]  intVector[3]  for third and last element.

One way to init vector by 4 integers 
vector<int> intVector{1,2,3,4};

Second way to init vector of integers by push_back. This fuction add the element in bracket at the end ot the vector. Super easy.

vector<int> intVector2;
intVector2.push_back(1);
intVector2.push_back(2);
intVector2.push_back(3);
intVector2.push_back(4);

Examples vector<int> in C++

On the picture is example of intVector and intVector2 one is initialized by {1,2,3,4} and one using 4 push_back(element to vector) functions. The result is the same. 

STL vector c++

Vector is filled by integers and the question is how to read the content whenever needed. The simple way is to use old style array indexing. Vector is indexed from 0 to Number of element - 1, this is why just less than instead of less than equal in for loop condition ( i < intVector2.size())
for (int i=0; i < intVector2.size(); i++) {
      cout << intVector2[i] << endl;
}

The modern way how to get the element from the modern container in C++ is to use something like following easy syntax. Initialize the auto variable element: from a range of intVector2, where elements automatically indexed background. Since collection knows to begin and end the value of a number of elements or size is not needed anymore. This form has one significant benefit as less buggy code. You do not need to know that vector is indexed from 0 and the last element is Total-1. Just give me whatever type auto element: from a vector. The modern C++ cover everything else on the background. 
for (auto element : intVector2)
      cout << element << endl;


STL VECTOR

Vector of custom structure

I have defined the simple structure to hold the detections as follows: (described after)
typedef struct Rect {
int x;
int y;
int w;
int h;
Rect(int _x, int _y, int _w, int _h) :
x(_x), y(_y), w(_y), h(_h)
{
}
Rect()
{
}
}Rectangle;"

The new structure type Rect that have 4 integer value to remember. The first two are position x and y of the detection and the w h are width and height of the detection as in case of a standard rectangle. The only confusing part can be:
Rect(int _x, int _y, int _w, int _h) :
x(_x), y(_y), w(_w), h(_h)
{
}
the body {} is enpty but and the constructor accepts 4 int parameters int _x, int _y, int _w, int _h. This x(_x), y(_y), w(_y), h(_h) is modern way how to set x as  input (_x) and the same for the rest of the parameters. This is the same and all the magic inside the constructor is in the {body}. 
Rect(int _x, int _y, int _w, int _h) 
{
         x = _x;
         y = _y;
         w=_w;
         h= _h;
}
The Rectangle with constructor can be initialozed as follows. Rectangle detection{ 2, 2, 5, 5 }; and stored in vector. The difference between vector of int and vector of Rectangles is type added into collection. The way how to use vector is the same. 
vector<Rectangle> detection = { Rectangle{10, 10, 10, 10} };
Vector detection is used to store Rectangle. The first is store one rectangle x=10,y=10,w=10 and h=10. The another vectors will be added at the end of the collection by push_back(). First create second rectangle and add to the same vector three times. 
Rectangle det2{ 2, 2, 5, 5 };
detection.push_back(det2);
detection.push_back(det2);
detection.push_back(det2)
The result is the vector with first element detection[0] and last detection[3] as is visible in following image.
Vector of detection C++ STL



By default, the vector allocates the memory on the heap, where the elements are stored. The great this is that the memory after vector is cleen up when it goes out of scope. In this case the descructor is triggered and heap is cleaned. 

Array in C++ (#include <array>)

In comparing with vector is fixed size array. A vector can be resized and array not. You know how many elements will be stored in the array. The C style array is:  int T[N] where N is a number of int elements in the array. The new way how to allocate the array that store 3 integer is as follows.

std::array<int, 3> ar = {1,2,3};. 
Arrays have a array.begin() == array.end() and you can iterate through array elements in same way how in vector case. The content of array is available through the same construction as in case of vector. 
for (auto element : ar)
       cout << 
element << endl;

List C++ STL

The list is a sequence that allows insert and erase anywhere inside. Implemented as a double linked list. Each element of the list contains the link to next and previous element. You can think about list as a vector that performs better in inserting, extracting and moving elements in any position within the container. Just include #include <list> and you can start coding. To perform insert or emplace operation the iterator that points to the element to perform the operation is needed. 
The beginning of the list container is coded as follows. 

auto iterator = listContainer.begin();

List C++

Look what is the same. The access of the element of List as in case of array and vector.

Stact C++

Stack is LIFO, Last in first out queue of standard types or custom types. Look at the all regals at a retail store. The item that was refilled as last, the last will be probably picked by the customer as the first one. Same as clothes organization system in any drawer if any. Or simply process just the element on the top.

This is stack of integers. with initial toSave.size() = 0;
stack<int> toSave;
cout << toSave.size() << endl;

Now by push the new integer 2 is into stack and toSave.size() = 1;
toSave.push(2);

The following operation get the 2 from the top of the stack and toSave.size() = 0 again.
cout << toSave.top() << endl;

Stack size is again 0, there is no element in stack.Push 2 element into the stack.
toSave.push(1);
toSave.push(2);

The stack size is 2 as display by following command.
cout << (int)toSave.size() << endl;

The next pop operation takes last element 2 and remove from the stack. Size is now equal to 1;
toSave.pop();

The following figure shows the situation after two push and before one pop. How many elements will be in the stack after the last line? Let me know in the comments.

STL stack C++ library


In next C++ tutorial

The next topic is algorithms over the standard collections. This will simplify your C++ life dramatically. 

Comments

  1. We can learn a lot about Why Deep Learning Works by studying the properties of the layer weight matrices of pre-trained neural networks. And, hopefully, by doing this, we can get some insight into what a well trained DNN looks like–even without peaking at the training data.
    MACHINE LEARNING training in chennai


    ReplyDelete
  2. When I initially commented I clicked the -Notify me when new comments are added- checkbox and now every time a comment is added I get four emails with the identical comment. Is there any way you can take away me from that service? Thanks! real money casino

    ReplyDelete

Post a Comment