Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++ Programming Standard Library

Similar presentations


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

1 C++ Programming Standard Library
Strings Containers

2 Organization The SL library is organized into several parts.
STL (Standard Template Library): Containers Iterators Algorithms Strings Streams Already explained in the previous presentation Numerics Internationalization Will not be explained in this course

3 Strings String classes enable you to use strings as normal types that cause no problems for the user Different from “ordinary strings” of type char* or const char* Copy, assignment and compare functions, as for fundamental types, are already available - no worry about memory corruption Defined in the header <string> Type: std::string - chain of characters of char type

4 String Operations Assignment: String size: Search in a string:
Conversion to C-string (char*): std::string s = "abcd"; std::string t = s; t += “efgh”; std::string s = "abcd"; int size = s.size(); std::string s = "abcd"; int pos = s.find("bc"); std::string s = "abcd"; char* cs = s.c_str();

5 STL STL = Standard Template Library The heart of SL
Provides a variety of collection classes that meet different needs, together with several algorithms that operate on them Programmers can forget about programming dynamic arrays, search algorithms etc. Just need to choose the appropriate container and call its functions to process data All components are templates Can be used for arbitrary element types Extremely efficient Cooperation of different well-structured components: Containers, iterators and algorithms

6 Containers Manage a collection of elements
Sequence containers = ordered collections in which every element has a certain position (independent of the value) Vector: 1 dimensional array List: doubly linked list Associative containers = sorted collections (the actual position of an element depends on its value) Set / Multiset: Element sorted according to their own value Map / Multimap: Elements are key/value pairs, sorted accordingly

7 Vector In the header <vector>
Type: std::vector<T> - where T is the element type Useful functions: push_back(elem) Add an element at the end at(idx) or [idx] Return the idx-th element front(), back() Return the first, the last element size() Return the vector size clear() Remove all elements #include <iostream> #include <vector> int main () { std::vector<int> myVector; myVector.push_back(3); myVector.push_back(4); std::cout << myVector[0] << std::endl; myVector.at(1) = 5; std::cout << myVector.back() << std::endl; myVector.clear(); return 0; }

8 List In the header <list>
Type: std::list<T> - where T is the element type Useful functions: push_front(elem), push_back(elem) Add an element at the beginning, at the end pop_front(), pop_back() Remove the element at the beginning, at the end front(), back(), size(),clear() Same as for vector #include <iostream> #include <list> int main () { std::list<int> myList; myList.push_front(3); myList.push_front(2); myList.push_back(4); myList.pop_front(); myList.pop_back(); std::cout << myList.front() << std::endl; std::cout << myList.back() return 0; } Note that some methods are common to both vector and list (and also other containers): size(), clear(), ...

9 Iterators Iterator = object that can “iterate”(navigate) over elements
It represents a certain position in a container All containers provide the same basic functions that allow iterators to navigate over their elements Functions defined for all containers: begin(), end() Return an iterator that represents the beginning, the end of elements in the container. The end is the position behind the last element v.begin() v.end() T vector it

10 Iterators Header files:
All containers define their own iterator type, there is no special header for using container iterators Types: container_type::iterator container_type::const_iterator Basic operations: Operator * Returns the element of the actual position Operator ++ Lets the iterator step forward to the next element Operators == and != Return the result whether two iterators represents the same position or not Operator = Assign an iterator (the position of the element to which it refers)

11 Iterators - Example #include <iostream> #include <vector>
int main() { std::vector<int> myVector; // ... fill the vector here // Iterating over vector elements std::vector<int>::iterator it; for ( it = myVector.begin(); it != myVector.end(); ++it ) { int i = *it; std::cout << "i = " << i << std::endl; } return 0;

12 When to Use which Container
Vector By default, you should use a vector If you need to access the N-th element Insertion and removal can be costly Very efficient when browsing the container List If you need to insert or remove an element often in the middle Inclusion in constant time Access to the N-th element by iterating from the 1st Map If you need an element via a key A list maintained sorted when inserting new elements Associative list


Download ppt "C++ Programming Standard Library"

Similar presentations


Ads by Google