Home  »  ArticlesProgrammingTechnologyTutorials   »   The C++ Standard Template Library Modifying Algorithms – Part 8

The C++ Standard Template Library Modifying Algorithms – Part 8

Welcome to the eighth and final part of our C++ Standard Template Library series. In this eighth part, we will be discussing in brief Modifying Algorithms. This series comes to an end even though some quick and dirty research will show the STL is rather large.

The reason we are stopping here is to change the focus to talking about other libraries like Boost and Qt. More importantly, we will be starting our C++ tutorial series on modern C++ which will include videos on our Partner YouTube Channel.

As usual, if you are catching up with us now I would like to direct you to the start of this series. If you have some knowledge in STL then you can fast track to ore recent articles in this series. So without much ado, we will pick up from where we left off on talking about Non-Modifying algorithms.

What are Modifying Algorithms?

In simplest terms, Modifying Algorithms can be described as those algorithms that alter the data on which they are applied to operate on. For example, if I apply an algorithm to change the order of elements in an array and return the array with the new order, that algorithm can be termed as a modifying algorithm.

Again we will show you some of the common algorithms to illustrate how these algorithms are used. If you missed our article on Algorithms and Iterators you can go back to part six of this STL series and read that article to brush up further on algorithms.

See also  The Message to the Serious Programmer

Let’s start off by looking at some of the many copy algorithms. These basically modify the contents of the one variable based on the value of a different variable. Look at these examples:

vector<int> src = {2,5,3}; // our source of data
vector<int> dest = {0,0,0,0,0,0,0,0,0,0}; // array to be modified

// Lets copy all of src to dest
copy(src.begin(), src.end(),dest.begin());
// dest is now {2,5,3,0,0,0,0,0,0,0}

// lets copy part of src to dest
copy_n(src.begin(), 2, dest.begin());
// dest is now {2,5,0,0,0,0,0,0,0,0}

//lets copy to the end of dest
copy_backward(src.begin(), src.end(),dest.end());
//dest is now {0,0,0,0,0,0,0,2,5,3}

Now let’s take a look at moving some data around. Take a look at these examples:

vector<int> src = {2,5,3}; // our source of data
vector<int> dest = {0,0,0,0,0,0,0,0,0,0}; // array to be modified

// Lets move all of src to dest
move(src.begin(), src.end(),dest.begin());
// dest is now {2,5,3,0,0,0,0,0,0,0}
// src is now undefined

//lets move src to the end of dest assuming its redefined
move_backward(src.begin(), src.end(),dest.end());
//dest is now {0,0,0,0,0,0,0,2,5,3}
// src is now undefined

Here are a few last examples:

vector<int> src = {2,5,3}; // our source of data
vector<int> src2 = {7,8,9}; // 2nd source of data
vector<int> dest = {0,0,0,0,0,0,0,0,0,0}; // array to be modified

// Lets exchange contents of src and src2 
swap_rages(src.begin(), src.end(),src2.begin());
// src2 is now contents of src while
// src is now contents or src2

//lets move src to the end of dest assuming its redefined
fill(dest.begin(), dest.end(),1);
//all elements in dest are now 1

//replace all occurences of 6 with 5
replace(src.begin(), src.end(),6,5);

//remove all occurences of 7
remove(src.begin(), src.end(),7);

//remove consequent equal elements
unique(src.begin(), src.end());

There you have it. By now you should have a general idea about algorithms that do not modify data vs the modifying algorithms.

Found this article interesting? Follow Brightwhiz on Facebook, Twitter, and YouTube to read and watch more content we post.

See also  Qt 5.7.1 Release Now Available for Download

Share it!