Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Standard template Class in C++

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report