Literals are compile-time constants. The specifiers below allow us to be fairly specific about the literal type to avoid needing silent conversions when there is a compatible type mismatch in code expressions.
type | value | interpretation |
---|---|---|
integer literal | 10 | integer |
10u | unsigned integer | |
10l | long integer | |
10ul | unsigned long integer | |
floating literal | 10.0 | double precision float |
10.0f | single precision float | |
10.0l | long double | |
10.0e-2 | 0.1 using exponential notation | |
string literal | "a string" | const char[] |
L"a string" | const wchar_t[] | |
u8"a string" | const char[] with utf-8 encoding | |
u"a string" | const char16_t[] with utf-16 encoding | |
U"a string" | const char32_t[] with utf-32 encoding | |
R"a string" | const char[] with raw encoding => everything is a char including \ and markup |
C++14 will be introducing user defined literal suffixes, e.g. ns for nanosecond, kg for kilogram, etc.
Here's a list of the most frequently used containers, annotated to show what was added in C++11. All the containers can be accessed with container specific iterators, e.g., vector<double>::iterator;
Container | Discussion |
---|---|
std::vector<T> |
An indexable collection of items of type T stored in continguous memory. Constant time insertion at back, O(N) if inserted at front. |
std::list<T> | A doubley linked list of items of type T. Constant time to store in front or back, but O(N) to insert in the middle. |
std::deque<T> | An indexable collection of items of type T. Constant time to store in front or back, but O(N) to insert in the middle. This is a stringe beast composed of three blocks of contiguous memory, at the front, at the back, and in the middle. |
std::queue<T> | A First In First Out (FIFO) collection of items of type T. Items are Enqueued on the front and Dequeued from the back. It is a wrapper around a std::deque<T> so it is constant time to Enqueue and Dequeue. I don't think you can access interior elements with an iterator but I'll have to check that to be sure. |
std::stack<T> | A Last in First Out (LIFO) collection of items of type T. Constant time to push and pop elements. Same access comments as for the std::queue<T> |
std::set<Key> | An ordered collecion of items of type T. The std::set<Key> is based on a balanced binary (Red-Black) tree, so operations are logrithmic, O(ln(N)) insertion, search, and retrieval. Sets will not accept duplicate keys. |
std::multiset<Key> | An ordered collecion of items of type T. The std::multiset<Key> is based on a balanced binary (Red-Black) tree, so operations are logrithmic, O(ln(N)) insertion, search, and retrieval. multisets will accept duplicate keys but that makes search and retrieval more complex as you may get back a range of values for the duplicated key. |
std::map<Key, Value> | An colletion of key-value pairs of types Key and Value, respectively, ordered on the keys. The std::map<Key, Value> is based on a balanced binary (Red-Black) tree, so operations are logrithmic, O(ln(N)) insertion, search, and retrieval. Maps will not accept duplicate keys. If you iterate over the collection you will get a sequence of pairs with iter->first returning the key and iter->second returning its associated value. |
std::multimap<Key, Value> | An colletion of key-value pairs of types Key and Value, respectively, ordered on the keys. The std::map<Key, Value> is based on a balanced binary (Red-Black) tree, so operations are logrithmic, O(ln(N)) insertion, search, and retrieval. Multimaps will accept duplicate keys. The same comments we made about the std::map<Key, value> apply here. If you iterate over the collection you will get a sequence of pairs with iter->first returning the key and iter->second returning its associated value. |
std::unordered_set<Key> C++11 |
An unordered colletion of items of Key type. The std::unordered_set<Key> is based on a hash table, so operations are are constant time for insertion, search, and retrieval. Unordered_sets will not accept duplicate keys. If you iterate over the collection you will items of type Key. |
std::unordered_multiset<Key> C++11 |
An unordered colletion of keys. The std::unordered_multiset<Key> is based on a hash table, so operations are are constant time for insertion, search, and retrieval. Unordered_multisets will accept duplicate keys. If you iterate over the collection you will get a sequence of keys. |
std::unordered_map<Key, Value> C++11 |
An unordered colletion of key-value pairs of types Key and Value, respectively. The std::map<Key, Value> is based on a hash table, so operations are are constant time for insertion, search, and retrieval. Unordered_maps will not accept duplicate keys. If you iterate over the collection you will get a sequence of pairs with iter->first returning the key and iter->second returning its associated value. |
std::unordered_multimap<Key, Value> C++11 |
An unordered colletion of key-value pairs of types Key and Value, respectively. The std::map<Key, Value> is based on a hash table, so operations are are constant time for insertion, search, and retrieval. Unordered_maps will accept duplicate keys. If you iterate over the collection you will get a sequence of pairs with iter->first returning the key and iter->second returning its associated value. |
C++11 STL Container code examples, C++03 STL Container code examples
Declaration | Discussion |
---|---|
std::unique_ptr<T> uptr(new T); | std::unique_ptr is a smart pointer that represents sole ownership of a resource created on the heap. Its action is to provide access to its owned object through standard pointer syntax and to delete the object when it goes out of scope. You must be careful not to initialize two or more std::unique_ptrs with the same raw pointer as that will cause delete to be called more than once on the owned object with undefined behavior. |
std::shared_ptr<T> sptr1(new T); std::shared_ptr<T> sptr2(sptr1); |
std::shared_ptr is a smart reference counted pointer that represents shared ownership of a resource created on the heap. When a shared pointer goes out of scope it decrements its reference count. Only if the count goes to zero is the resource deleted. You must be careful not to create more than one std::shared_ptr from a raw pointer. After the first is created you must create the remainder from an already defined std::shared_ptr sharing the same resource. |
std::weak_ptr<T> wptr = sptr1; | std::weak_ptr allows you to refer to an object it doesn't own and may have been deleted. You check by calling wptr.lock() or wptr.expired(). |
The type_traits library lets you make queries about a given type, like is it a pointer or is it a class. here's a complete list.
The chrono library has facilities for constructing clocks and measuring times. You can get the system time with std::system_clock and construct a high resolution timer from std::high_resolution_clock. here's a list of all its facilities.
Chrono Timer code examplestd::initializer-list is a class that supports initializing declared primitives, structs, classes, and containers with the values they will hold on startup. Here are two samples:
You need to be careful when the type you are initializing has a constructor that takes a single parameter. If your syntax uses braces instead of parentheses, then the initializer list will be used rather than the constructor you intended.
std::tuple<T1, T2, ...> is a class that holds a fixed number of instances of arbitrary types. It is a generalization of std::pair<F, S>. the std::make_tuple(Args... args) is a variadic template function designed to make it easy to construct tuples. The non-member function std::get<n>(std::tuple) provides access to a tuple's nth member instance.
tuple syntax | Description |
---|---|
std::tuple<T1, T2, ...> t { t1, t2, ...} | Declare an instance of a tuple that holds instances of T1, T2, ... |
Tn tn = get<n>(t); | Retrieve the value of the nth element of the tuple t. |
std::tuple<T1, T2, ...> t = std::make_tuple<t1, t2, ...> | Alternate way to initialize a std::tuple. |
Regular Expressions are strings with a particular syntax that makes it easy to specify exactly what a search should match. There are special characters for the beginning and end of a search string, ways to specify that any of a set of characters are acceptable at some place in the string and so forth. The standard C++ library now has std::regex, a regular expression processor that you may find useful.
here's a tutorial. You will also find a nice expositions in "The C++ Programming Language" starting on page 1051 and in "The C++ Standard Library" starting on page 717. These are our two required texts.C++ now supports threads and synchronization constructs to support multi-threaded programming. Here are links to C++11concurrency examples and a blocking queue implementation that we will discuss later in the semester. I recommend that you wait awhile before looking at these. You will need to be quite comfortable with C++11 to get much out of the examples.
Algorithm | Discussion |
---|---|
bool std::all_of(start_iter, end_iter, PredicateFunctor()); C++11 |
Checks if a predicate functor returns true on all of the elements of a container. |
bool std::any_of(start_iter, end_iter, PredicateFunctor()); C++11 |
Checks if a predicate functor returns true on any of the elements of a container. |
bool std::none_of(start_iter, end_iter, PredicateFunctor()); C++11 |
Checks if a predicate functor returns false on all of the elements of a container. |
UnaryFunction std::for_each(start_iter, end_iter, UnaryFunction f) | Functior processing is called on each element of the container. |
found_iter std::find(start_iter, end_iter, T& t); | returns an iterator pointing to the found item or end_iter. |
Code example using for_each near the end of the source code.