Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++ Standard Template Library

Similar presentations


Presentation on theme: "C++ Standard Template Library"— Presentation transcript:

1 C++ Standard Template Library
Based on slides by Marat Dukhan

2 Standard Template Library (STL)
Part of C++ standard Each C++ compiler ships with STL Provides template container classes vector (array) list (double-linked list) Map (hash table) set stack deque (double-ended queue) valarray (vector with defined arithmetic operations) New C++ standard adds few more Provides template algorithm functions initialization (constant, with specified function) sort, partial sort, nth element (e.g. median) search (linear and binary) minimum, maximum, sum, dot product, 1st difference

3 Getting Started with STL
STL headers: Headers for containers have the same name as container E.g. use #include <vector> for vector container #include <algorithm> for most algorithmic functions #include <numeric> for sum, dot product, and 1st difference algorithms All STL functions and classes are defined in namespace std Either prepend names with std:: E.g. std::vector<int> v Or add using namespace std; before you refer to STL names

4 STL vector container std::vector<int> v; /* Reserve space for 100 elements */ v.reserve(100); for (size_t i = 0; i < v.capacity(); i++) { /* Adds element i to the end of a vector */ v.push_back(i); } for (size_t i = 0; i < v.size(); i++) { std::cout << "Element " << i << " is " << v[i] << std::endl; }

5 More STL vector examples
#include <vector> std::vector<int> v; /* Resize to 100 elements. New elements will be created with default constructor */ v.resize(100); for (size_t i = 0; i < v.size(); i++) { /* Change value of element i */ v[i] = i; }

6 Traversing STL Vector with Iterators
std::vector<int> array; array.resize(10); for (std::vector<int>::iterator current = array.begin(); current != array.end(); ++current) { *current = 0 } for (std::vector<int>::const_iterator current = array.begin(); current != array.end(); ++current) { std::cout << (*current) << std::endl; } 1 2 3 4 5 6 7 8 9 10 array.begin() array.end()

7 Auto keyword With new C++ standard (C++11) you can replace std::vector<int>::iterator with auto. auto variable_name = expression Creates a variable of the same type as expression for (auto current = array.begin(); current != array.end(); ++current) { *current = 0 } for (auto current = array.cbegin(), current != array.cend(); ++current) { std::cout << (*current) << std::endl; }

8 STL Algorithms std::vector<double> v = ... const double firstElement = v.front(); const double lastElement = v.back(); /* Sort all elements but the first one */ std::sort(v.begin() + 1, v.end()); /* Linear search */ std::vector<double>::const_iterator iterator = std::find(v.begin(), v.end(), firstElement); if (iterator != v.end()) { std::cout << "Found the first element" << std::endl; } /* Sort in reverse order */ std::sort(v.rbegin(), v.rend()); iterator = std::binary_search(v.rbegin(), v.rend(), lastElement); if (iterator != v.rend()) { std::cout << "Found the last element" << std::endl; }

9 More STL Algorithms const double sum = std::accumulate(v.begin(), v.end(), 0.0); const double minimum = std::min(v.begin(), v.end()); std::vector<double>::const_iterator maximumElementIterator = std::max_element(v.begin(), v.end()); const size_t maximumElementIndex = std::distance(v.begin(), maximumElementIterator); /* Initialize all elements to 1.0 */ std::fill(v.begin(), v.end(), 1.0);

10 More STL Containers std::string text = "Hello" text += ", World!" std::cout << text << std::endl; std::map<std::string, unsigned int> wordCounts; wordCounts["Hello"] = 1; wordCounts["World"] = 1; std::cout << wordCounts["Hello"] << std::endl; assert(wordCounts.find("C++") == wordCounts.end()); std::vector<std::vector<double> > vectorOfVectors;

11 Copy/paste example #include <iostream>
#include <algorithm> #include <vector> using namespace std; #define VECSIZE 100 int main() {     vector<int> v;     v.resize(VECSIZE);     for (int i=0; i<VECSIZE; i++)     {         v[i] = rand() % ;     }     sort(v.begin(), v.end());         cout << v[i] << "\n";     }         return 0; }


Download ppt "C++ Standard Template Library"

Similar presentations


Ads by Google