Changes

Jump to: navigation, search

Team Hortons

1,894 bytes added, 12:22, 1 October 2017
no edit summary
== Standard Template Library ==The Standard Template Library (STL) for C++ is a library that provides a set of classes and functions for C++, like iterators and vectors. The STL is divided in four parts: algorithms, containers, functors, and iterators. For this project, we will focus on the ''algorithms'' library. The algorithms library provides many functions to be used for ranges of elements, and use iterators or pointers to navigate through them. Some of these algorithms are: * '''all_of''' - Tests if a condition is true for all the elements in the range* '''for_each''' - Applies a function to all elements in the range* '''find''' - Finds a value in the range* '''sort''' - Sorts the elements in the range These algorithms provide a ''functional'' way to work on collections, and are very common in several programming languages. <code>#include <iostream>#include <algorithm>#include <vector> using namespace std; int main(){ vector<int> myVector = { 10, 6, 7, 8, 5, 4, 1, 2, 3, 9 };  // Sorting the elements sort(myVector.begin(), myVector.end(), [](int a, int b) { return b < a; });  cout << "== FOR EACH ==" << endl; for_each(myVector.begin(), myVector.end(), [](int i) { cout << "Element: " << i << endl; }); cout << endl;   cout << "== COUNTING NUMBERS LARGER THAN 6 ==" << endl; long nLt6 = count_if(myVector.begin(), myVector.end(), [](int i) { return i > 6; }); cout << "There are " << nLt6 << " numbers larger than 6 in the vector" << endl << endl;   cout << "== FINDING AN ELEMENT ==" << endl; vector<int>::iterator elFound = find_if(myVector.begin(), myVector.end(), [](int i) { return i > 4 && i % 6 == 0; }); cout << "The element " << *elFound << " is the first that satisfies i > 4 && i % 6 == 0" << endl << endl;  return 0;}</code> 
<pre style="color: red">
What is it?
== Sources ==
* STL: https://en.wikipedia.org/wiki/Standard_Template_Library
* STL: http://www.cplusplus.com/reference/algorithm/
* Policy-Based execution for C++17: https://scs.senecac.on.ca/~oop345/pages/content/multi.html#alg
* Intel Parallel STL: https://software.intel.com/en-us/get-started-with-pstl*

Navigation menu