[CPP] Some useful STL
In CPP, we will use a lot of useful standard template library, and from CPP lab, i have learned some. Here is a brief summary.
There are a lot of useful container classes, for example, vector, we have introduced vector before. In vector, we also know how to use iterator. Similarly, iterator is useful in a lot of container, for example, array
The following code will be a example of:
Therefore, you can transfer them or copy them just using method copy
copy (InputIterator first, InputIterator last, OutputIterator result);
Using some container classes
There are a lot of useful container classes, for example, vector, we have introduced vector before. In vector, we also know how to use iterator. Similarly, iterator is useful in a lot of container, for example, array
The following code will be a example of:
- how to new an array
- how to traversal array in two ways(iterator and for each loop)
- how to sort the array using existing algorithm
- how to reverse an array
- how to reverse the array and output it using output stream
list
You can use list.insert to insert elements, but there is a method called push_back, as the same name in the vector, will insert element at the back of the list.transfer between list, array, vector
As we all know, list, array, vector are similar.Therefore, you can transfer them or copy them just using method copy
copy (InputIterator first, InputIterator last, OutputIterator result);
- The first and second parameter will be the begin() and end() iterator of the input (the one you copy from)
- The last parameter is the result begin() iterator (the one you want to store or transferred to)
评论
发表评论