C++ part B 第一周 (上)
the enum of C++ 11 have some new features
enum class WierdLogic :unsigned{NO, MAYBE = 5, YES = 10};
<regex> - regular expression
<thread> - threading
<unordered_map> - hash based map
<array> - fixed size array
<forward_list> - singly linked list
And much more...
In C
C container is an array such as: int d[100]
C iterator is a pointer : int *p = d;
C algorithm is : rand(), sqrt() ...
Generics can be done clumsily with void*
We have seen input and output iterators
Must have a single pass algorithm such as find
Next up forward, bidirectional,random access
Search forward direction one-by-one.
The operator ++ is defined("advance the iterator") and dereferencing operators * and ->.
Iterators may be compared for equality == and != and may be constructed and copy constructed.A canonical algorithm using this iterator is the STL replace algorithm:
template<class T> void replace(ForwardIterator first, ForwardIterator last, const T&x, const T&y);
This algorithm replaces occurences of x by y in the specified iterator range.
#include <iostream> #include <iterator> #include <fstream> #include <vector> using namespace std; template<typename ForwardIterator> void square(ForwardIterator first, ForwardIterator last){ for(; first!=last; first++) *first = (*first)*(*first); } int main(){ vector<int> w = {1,2,3,4,5}; square(w.begin(), w.end()); for(auto i:w) cout << i << "\t"; cout << endl; }