当前位置:网站首页>STL summary
STL summary
2022-06-21 10:48:00 【Longbow dog learning C】
Catalog
One .STL The essence of
STL Is the standard template library , It's efficient C++ library , Increased code reuse rate , Make development more efficient .
Two .STL Six components of
Containers
STL The container of , It can be divided into sequential containers and associative containers .

Algorithm
Algorithm : Steps to solve the problem , In limited steps , Solve problems in mathematics or logic .STL There are a total of 70 Multiple , It mainly includes : Sort , lookup , Permutation and combination , Data mobility , Copy , Delete , Compare combinations , Operations, etc .
①accumulate: Accumulate the elements in the interval
// Yes [first, last) The elements in the interval are init Accumulate on the basis of
template <class InputIterator, class T>
T accumulate ( InputIterator first, InputIterator last, T init );
// Yes [fist, last) The elements in the interval are init On the basis of binary_op The specified operation is accumulated
template <class InputIterator, class T, class BinaryOperation>
T accumulate ( InputIterator first, InputIterator last, T init,
BinaryOperation binary_op );#include <numeric>
#include <vector>
struct Mul2
{
int operator()(int x, int y) { return x + 2 * y; }
};
int main()
{
// Accumulate the elements in the interval
vector<int> v{ 10, 20, 30 };
cout << accumulate(v.begin(), v.end(), 0)<<endl;
// Multiply each element in the interval by 2, And then add up
cout << accumulate(v.begin(), v.end(), 0, Mul2()) << endl;
return 0;
}②count And count_if: The function of the algorithm is to count the number of occurrences of an element in the interval
// Statistics value In the interval [first,last) Is the number of times
template <class InputIterator, class T>
ptrdiff_t count ( InputIterator first, InputIterator last, const T& value )
{
ptrdiff_t ret=0;
while (first != last) if (*first++ == value) ++ret;
return ret;
}
// Statistics satisfy pred The element of the condition is in [first, last) Number of in
template <class InputIterator, class Predicate>
ptrdiff_t count_if ( InputIterator first, InputIterator last, Predicate pred )
{
ptrdiff_t ret=0;
while (first != last) if (pred(*first++)) ++ret;
return ret;
}③find,find_if: The function of this algorithm is to find the position where the element first appears in the interval
// stay [first, last) Search for value First occurrence , Find the location that returns the element , Otherwise return to last
// Time complexity O(N)
template<class InputIterator, class T>
InputIterator find ( InputIterator first, InputIterator last, const T& value )
{
for ( ;first!=last; first++) if ( *first==value ) break;
return first;
}
// stay [first, last) To find a solution that satisfies pred Where the element of the condition first appears , Find and return to that location , Otherwise return to last
// Time complexity O(N)
template<class InputIterator, class Predicate>
InputIterator find_if ( InputIterator first, InputIterator last, Predicate pred )
{
for ( ; first!=last ; first++ ) if ( pred(*first) ) break;
return first;
}④max and min:max Returns the larger of the two elements ,min Returns the smaller of the two elements .
template <class T>
const T& max(const T& a, const T& b)
{
return (a<b)?b:a;
}
template <class T>
const T& min(const T& a, const T& b)
{
return !(b<a)?a:b;
}⑤merge: This algorithm combines two ordered sequences into one , Must include header file when using .
int main()
{
vector<int> v{ 2, 6, 5, 8 };
list<int> L{ 9, 3, 0, 5, 7 };
sort(v.begin(), v.end());
L.sort();
vector<int> vRet(v.size() + L.size());
merge(v.begin(), v.end(), L.begin(), L.end(), vRet.begin());
for (auto e : vRet)
cout << e << " ";
cout << endl;
return 0;
}⑥ partial_sort: The function of this algorithm is : look for TOPK
// In the interval [first, last) Before you find it middle-first The smallest element , And stored in [first, middle) in
template <class RandomAccessIterator>
void partial_sort(RandomAccessIterator first, RandomAccessIterator middle,
RandomAccessIterator last);
// stay [first, last) Before you find it middle-first The largest or smallest element , And stored in [first, middle) in
template <class RandomAccessIterator, class Compare>
void partial_sort(RandomAccessIterator first, RandomAccessIterator middle,
RandomAccessIterator last, Compare comp);⑦ partition: The function of the algorithm is to divide the elements in the interval according to the conditions , Must include header file when using .
template <class BidirectionalIterator, class Predicate>
BidirectionalIterator partition(BidirectionalIterator first,
BidirectionalIterator last, Predicate pred)
{
while (true)
{
while (first!=last && pred(*first)) ++first;
if (first==last--) break;
while (first!=last && !pred(*last)) --last;
if (first==last) break;
swap (*first++,*last);
}
return first;
}⑧reverse: The function of the algorithm is to reverse the elements in the interval , Must include header file when using .
template <class BidirectionalIterator>
void reverse ( BidirectionalIterator first, BidirectionalIterator last)
{
while ((first!=last)&&(first!=--last))
swap (*first++,*last);
}⑨sort: Multiple sorting algorithms are mixed .
⑩ unique: This function is used to delete adjacent repetitive elements in the interval , Ensure element uniqueness , Pay attention to ensure that the elements in the interval are orderly before use , To achieve real weight removal .
iterator
What is iterator ? An iterator is a design pattern , Let the user access the container's data through a specific interface . An iterator is essentially a pointer .
Why iterators are needed ? It can better realize the general algorithm .
Who should be responsible for providing iterators ? The underlying structure of each container is different , The iterator acts as a conversion layer between the algorithm and the container . therefore : Iterators for each container should be provided by the container designer , Then the container provides a unified interface according to the Convention .
Adapter
Adapter : Then the adapter , It's a design pattern , To put it simply : What you need is right in front of you , But it cannot be used because of the wrong interface , Its interface needs to be transformed for ease of use . namely : Convert the interface of one class into the interface of another class desired by the user , Make classes with incompatible interfaces work together .
functor
functor : An object with functional characteristics , The caller can use the object like a function , To be able to “ It behaves like a function ”, The class of this object must customize the function call operator operator(), After overloading the operator , You can add a pair of parentheses to the end of the functor object , To call the function defined by the function operator() operation , In terms of its behavior ,“ functor ” One more cut paste . General matching algorithm of imitative function , Role is : Improve the flexibility of the algorithm .
边栏推荐
- The bilingual live broadcast of Oriental selection is popular, and the transformation of New Oriental is beginning to take shape
- TensorFlow,危!抛弃者正是谷歌自己
- Odd number of characters异常
- leetcode:715. Range 模块【无脑segmentTree】
- 香农的信息论究竟牛在哪里?
- Ccs7.3 how to erase only part of the flash sector when burning DSP on-chip flash (two projects of on-chip flash burning of a DSP chip)
- Introduction to ground plane in unity
- 西电AI专业排名超清北,南大蝉联全国第一 | 2022软科中国大学专业排名
- ThreadLocal
- The delimiter connects the list string without secondary processing joiner on(). join()&&String. join(“,“, list)
猜你喜欢

The "first city" in Central China. How can Changsha be built?

AI越进化越跟人类大脑像!Meta找到了机器的“前额叶皮层”,AI学者和神经科学家都惊了...

《Feature-metric Loss for Self-supervised Learning of Depth and Egomotion》论文笔记

How to learn function test? Ali engineer teaches 4 steps

为什么 C# 访问 null 字段会抛异常?

基因型填充前的质控条件简介

Software architecture discussion

Application configuration management, basic principle analysis
![Fastapi web framework [pydantic]](/img/e1/290a8a6a978b9fb56a9c86f1734c45.png)
Fastapi web framework [pydantic]

中国国际电子商务中心与易观分析联合发布:2021年4季度全国网络零售发展指数同比增长0.6%
随机推荐
Concept of naive Bayes
国金证券开户安全吗?
FastAPI Web框架 [Pydantic]
POI implements operation to generate word tables and operate chart data in word
Vscode high-speed download address -- solve the problem of slow vscode Download
功能测试怎么学?阿里工程师教4个步骤
Do website from scratch 11- blog development
Start from scratch 10- background management system development
开课报名|「Takin开源特训营」第一期来啦!手把手教你搞定全链路压测!
Xidian AI ranked higher than Qingbei in terms of AI major, and Nantah ranked first in China in terms of Software Science in 2022
Mythical Games宣布与韩国领先游戏发行商Kakao Games合作,推动亚太区业务扩张
Underlying principle of Concurrency: thread, resource sharing, volatile keyword
The execution process before executing the main function after the DSP chip is powered on
Versions supported by vuforia engine
WCF restful+jwt authentication
The more AI evolves, the more it resembles the human brain! Meta found the "prefrontal cortex" of the machine. AI scholars and neuroscientists were surprised
One line of code accelerates sklearn operations thousands of times
Add solid state to the computer
Celsius 的暴雷,会是加密领域的“雷曼时刻”吗?
Is it safe for Guojin securities to open an account?