[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.

Using some container classes

enter image description here
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
    enter image description here
reverse_copy can also copy the reversed array to another array(container) to avoid the destroy of original copy.

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)

Sort

sort is useful, while using sort, list can use list.sort, and vectors and arrays should use sort(begin() iterator, end() iterator).

评论

此博客中的热门博文

[MLE] Linear Classification

[AIM] MetaHeuristics

[CS231] Neural Networks