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

What are the common algorithms of C++ STL

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

Share

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

This article introduces the relevant knowledge of "what are the common algorithms of C++ STL". In the operation process of actual cases, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

##Search algorithm

####adjacent_find()

Find a pair of adjacent repeating elements within the iterator pair identification element range, and return an iterator pointing to the first element of the pair if found. Otherwise, it returns to past-the-end.

vector vecInt; vecInt.push_back(1); vecInt.push_back(2); vecInt.push_back(3); vecInt.push_back(4); vecInt.push_back(5); vecInt.push_back(5); vector::iterator it = adjacent_find(vecInt.begin(), vecInt.end());

####binary_search

Find value in an ordered sequence and return true if found. Note: In unordered sequences, it cannot be used.

set setInt; setInt.insert(2); setInt.insert(1); setInt.insert(8); setInt.insert(5); setInt.insert(9); bool bFind = binary_search(setInt.begin(),setInt.end(),8);

####count()

Using the equals operator, compares the elements in the flag range with the input value and returns the number of equals.

vector vecInt; vecInt.push_back(8); vecInt.push_back(2); vecInt.push_back(1); vecInt.push_back(4); vecInt.push_back(8); vecInt.push_back(6); int iCount = count(vecInt.begin(),vecInt.end(),8); //iCount==2

####count_if()

The count_if algorithm calculates the range of elements in [first, last) and returns the number of elements that satisfy the condition.

vector vecInt; vecInt.push_back(3); vecInt.push_back(3); vecInt.push_back(1); vecInt.push_back(4); vecInt.push_back(8); vecInt.push_back(9); int count = count_if(vecInt.begin(), vecInt.end(), evenNumber);//even number is 2;

####find

Compares elements in a specified range with the input value using the equals operator of the underlying element. When there is a match, the search ends and the iterator for that element is returned.

####equal_range:

Returns a pair of iterators, the first representing lower_bound and the second representing upper_bound.

##Search algorithm

merge()

The following are sorting and general algorithms: Provide element sorting strategies

merge: Merge two ordered sequences into another sequence.

For example: vecIntA,vecIntB,vecIntC are containers declared with vectors, vecIntA already contains 1,3,5,7,9 elements, vecIntB already contains 2,4,6,8 elements

vecIntC.resize(9); //enlarge capacity

merge(vecIntA.begin(),vecIntA.end(),vecIntB.begin(),vecIntB.end(),vecIntC.begin());

vecIntC stores nine elements in sequence: 1, 2, 3, 4, 5, 6, 7, 8, 9

####sort()

sort: Rearrange elements in ascending order by default. To change the collation, enter a comparison function.

random_shuffle()

random_shuffle: Randomizes the order of elements within a specified range.

"C++ STL commonly used algorithms what" content is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

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

Internet Technology

Wechat

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

12
Report