算法库 | C++魔术师般的STL算法
对于所有渴望在竞争激烈的编程中出类拔萃的人来说,如果不知道STL能提供什么,只知道STL容器的知识是没有用的。
STL有一个算法的海洋,对于所有库函数。请参考这里。
在竞争性编程中,一些最常用的向量算法和最有用的算法被提到如下。
Non-Manipulating Algorithms
1. sort(first_iterator, last_iterator)
//对给定向量进行**(升序)**排序。
2. sort(first_iterator, last_iterator, greater<int>())
//对给定的容器/向量进行降序排序。
3. reverse(first_iterator, last_iterator)
//对一个向量进行翻转
4. max_element(first_iterator, last_iterator)
//找到一个向量的最大元素,返回的是一个指针,可以通过提领*max_element()的方式来获取最大值
5. min_element(first_iterator, last_iterator)
//找到一个向量的最小元素,返回的是一个指针,可以通过提领*min_element()的方式来获取最小值
6. accumulate(first_iterator, last_iterator, initial value of sum)
返回的是一个数值
函数的头文件在#include <numeric>
中
//对向量的元素进行求和。
例子
// A C++ program to demonstrate working of sort(),
// reverse()
#include <algorithm>
#include <iostream>
#include <vector>
#include <numeric> //For accumulate operation, 函数的头文件
using namespace std;
int main()
{
// Initializing vector with array values
int arr[] = {10, 20, 5, 23 ,42 , 15};
int n = sizeof(arr)/sizeof(arr[0]);//数组长度计算 = sizeof(数组名)/sizeof(数组首元素)
vector<int> vect(arr, arr+n); //利用数组来初始化vector
cout << "Vector is: ";
for (int i=0; i<n; i++)
cout << vect[i] << " ";
// Sorting the Vector in Ascending order
sort(vect.begin(), vect.end());
cout << "\nVector after sorting is: ";
for (int i=0; i<n; i++)
cout << vect[i] << " ";
// Sorting the Vector in Descending order
sort(vect.begin(),vect.end(), greater<int>());
cout << "\nVector after sorting in Descending order is: ";
for (int i=0; i<n; i++)
cout << vect[i] << " ";
// Reversing the Vector (descending to ascending , ascending to descending)
reverse(vect.begin(), vect.end());
cout << "\nVector after reversing is: ";
for (int i=0; i<n; i++)
cout << vect[i] << " ";
cout << "\nMaximum element of vector is: ";
cout << *max_element(vect.begin(), vect.end()); //提领*
cout << "\nMinimum element of vector is: ";
cout << *min_element(vect.begin(), vect.end()); //提领*
// Starting the summation from 0
cout << "\nThe summation of vector elements is: ";
cout << accumulate(vect.begin(), vect.end(), 0);
return 0;
}
Output
Vector is: 10 20 5 23 42 15
Vector after sorting is: 5 10 15 20 23 42
Vector after sorting in Descending order is: 42 23 20 15 10 5
Vector after reversing is: 5 10 15 20 23 42
Maximum element of vector is: 42
Minimum element of vector is: 5
The summation of vector elements is: 115
6. count(first_iterator, last_iterator, x)
计算x在向量中出现的次数
7. find(first_iterator, last_iterator, x)
返回x在向量中首次出现的位置,如果x未在向量中,返回向量元素的最后一个位置的下一个位置(例如nums.end())
例子
// C++ program to demonstrate working of count()
// and find()
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main()
{
// Initializing vector with array values
int arr[] = {10, 20, 5, 23 ,42, 20, 15};
int n = sizeof(arr)/sizeof(arr[0]);
vector<int> vect(arr, arr+n);
cout << "Occurrences of 20 in vector : ";
// Counts the occurrences of 20 from 1st to
// last element
cout << count(vect.begin(), vect.end(), 20);
// find() returns iterator to last address if
// element not present
find(vect.begin(), vect.end(),5) != vect.end()?
cout << "\nElement found":
cout << "\nElement not found";
return 0;
}
Output
Occurrences of 20 in vector : 2
Element found
8. binary_search(first_iterator, last_iterator, x)
判断x是否出现在向量中
9. lower_bound(first_iterator, last_iterator, x)
返回>=x的迭代器
10. upper_bound(first_iterator, last_iterator, x)
返回>x的迭代器
// C++ program to demonstrate working of lower_bound()
// and upper_bound().
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main()
{
// Initializing vector with array values
int arr[] = {5, 10, 15, 20, 20, 23, 42, 45};
int n = sizeof(arr)/sizeof(arr[0]);
vector<int> vect(arr, arr+n);
// Sort the array to make sure that lower_bound()
// and upper_bound() work.
sort(vect.begin(), vect.end());
// Returns the first occurrence of 20
auto q = lower_bound(vect.begin(), vect.end(), 20);
// Returns the last occurrence of 20
auto p = upper_bound(vect.begin(), vect.end(), 20);
cout << "The lower bound is at position: ";
cout << q-vect.begin() << endl;
cout << "The upper bound is at position: ";
cout << p-vect.begin() << endl;
return 0;
}
Output
The lower bound is at position: 3
The upper bound is at position: 5
Some Manipulating Algorithms
1. arr.erase(position to be deleted)
删除中的选定元素,并相应地移动向量中的元素并调整向量的大小
2. arr.erase(unique(arr.begin(), arr.end()), arr.end())
删除arr中重复出现的元素
对数组排序并去重(使得每个元素只出现一次)
sort(nums.begin(), nums.end())
nums.earse(unique(nums.beign(), nums.end()), nums.end())
例子
// C++ program to demonstrate working
// of erase
#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <vector>
using namespace std;
int main()
{
// Initializing vector with array values
int arr[] = { 5, 10, 15, 20, 20, 23, 42, 45 };
int n = sizeof(arr) / sizeof(arr[0]);
vector<int> vect(arr, arr + n);
cout << "Given Vector is:\n";
for (int i = 0; i < n; i++)
cout << vect[i] << " ";
vect.erase(find(vect.begin(),vect.end(),10));
cout << "\nVector after erasing element:\n";
for (int i = 0; i < vect.size(); i++)
cout << vect[i] << " ";
vect.erase(unique(vect.begin(), vect.end()),
vect.end());
cout << "\nVector after removing duplicates:\n";
for (int i = 0; i < vect.size(); i++)
cout << vect[i] << " ";
return 0;
}
Output
Given Vector is:
5 10 15 20 20 23 42 45
Vector after erasing element:
5 15 20 20 23 42 45
Vector after removing duplicates:
5 15 20 23 42 45
3. next_permutation(first_iterator, last_iterator)
向量的下一个排列(返回值是void,直接对向量内部进行修改)
4. prev_permutation(first_iterator, last_iterator)
向量的上一个排列
例子
// C++ program to demonstrate working
// of next_permutation()
// and prev_permutation()
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main()
{
// Initializing vector with array values
int arr[] = {5, 10, 15, 20, 20, 23, 42, 45};
int n = sizeof(arr)/sizeof(arr[0]);
vector<int> vect(arr, arr+n);
cout << "Given Vector is:\n";
for (int i=0; i<n; i++)
cout << vect[i] << " ";
// modifies vector to its next permutation order
next_permutation(vect.begin(), vect.end());
cout << "\nVector after performing next permutation:\n";
for (int i=0; i<n; i++)
cout << vect[i] << " ";
prev_permutation(vect.begin(), vect.end());
cout << "\nVector after performing prev permutation:\n";
for (int i=0; i<n; i++)
cout << vect[i] << " ";
return 0;
}
Output
Given Vector is:
5 10 15 20 20 23 42 45
Vector after performing next permutation:
5 10 15 20 20 23 45 42
Vector after performing prev permutation:
5 10 15 20 20 23 42 45
5. distance(first_iterator,desired_position)
它返回所需位置与第一个迭代器的距离。此函数在查找索引时非常有用。
例子
// C++ program to demonstrate working of distance()
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main()
{
// Initializing vector with array values
int arr[] = {5, 10, 15, 20, 20, 23, 42, 45};
int n = sizeof(arr)/sizeof(arr[0]);
vector<int> vect(arr, arr+n);
// Return distance of first to maximum element
cout << "Distance between first to max element: ";
cout << distance(vect.begin(),
max_element(vect.begin(), vect.end()));
return 0;
}