Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of Standard template Class in C++

Shulou Source: shulou.com Published: 2022-06-03 14:31:21 11月14日 Update

Editor to share with you the C++ standard template class example analysis, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!

1 standard template library

STL provides templates that represent containers, iterators, function objects, and algorithms.

Containers: similar to arrays that store several values, which are actually homogeneous

Iterator: an object that traverses a container, similar to a pointer to a traversing array, a generalized pointer

Algorithm: complete specific tasks

Function object: class object or function pointer.

Template class vector

Erase () deletes the given interval element in the vector. Accept two iterator parameters (which define the interval to be deleted), iterator 1 points to the beginning of the interval, and iterator 2 points to the last position at the end of the interval.

/ / delete first and second elementsdata.erase (data.begin (), data.begin () + 2)

Insert () has the opposite function as erase (). Accept three iterator parameters. Param1 specifies where the new element is inserted, and param2 and param3 define the interval for insertion (usually other objects).

/ / insert [old.begin + 1, old.end) in the front of datadata.insert (data.begin (), old.begin () + 1, old.end ()); / / at this time, the overtailed element is very convenient / / the element data.insert (data.end (), old.begin () + 1, old.end ()) is inserted at the end.

For_each () accepts three parameters. Param1 and param2 are iterators that define intervals in the container, and param3 points to the pointer to the function (function object). The pointed function (param3) is used for each element of the interval [param1, param2). However, the pointed function param3 cannot modify the value of the interval.

/ / instead of for loop for (auto iter = data.begin (); iter! = data.end (); iter++) {showData (* iter);} / replace for with for_each// Note that the function showData here cannot modify the value of data. For_each (data.begin (), data.end (), showData)

The sort () sort function.

1) accept the iterator parameters of 2 intervals, and use the

Tags: Functions intervals iterations objects templates elements parameters points containers pointers standards content articles loops examples examples analysis three two code Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology MySQL MariaDB Apple Docker