算法库C++ C++魔术师STL算法

对于那些渴望在竞争性编程中出类拔萃的人来说,只有了解STL容器的知识是没有用的,直到你不知道所有STL都能提供什么。 STL有一个算法海洋,对于所有 库函数:参考 在这里 . 下面介绍了一些最常用的向量算法和竞争编程中最有用的算法:

null

非操纵算法

  1. 分类 (第一个迭代器,最后一个迭代器) –对给定向量进行排序。
  2. 反向(第一个迭代器,最后一个迭代器) –反转向量。
  3. *最大元素(第一个迭代器,最后一个迭代器) –找到向量的最大元素。
  4. *最小元素(第一个迭代器,最后一个迭代器) –找到向量的最小元素。
  5. 累加(第一个迭代器、最后一个迭代器、和的初始值) –向量元素的求和

CPP

// 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]);
vector< int > vect(arr, arr+n);
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 << "Vector after sorting is: " ;
for ( int i=0; i<n; i++)
cout << vect[i] << " " ;
// Reversing the Vector
reverse(vect.begin(), vect.end());
cout << "Vector after reversing is: " ;
for ( int i=0; i<6; i++)
cout << vect[i] << " " ;
cout << "Maximum element of vector is: " ;
cout << *max_element(vect.begin(), vect.end());
cout << "Minimum element of vector is: " ;
cout << *min_element(vect.begin(), vect.end());
// Starting the summation from 0
cout << "The summation of vector elements is: " ;
cout << accumulate(vect.begin(), vect.end(), 0);
return 0;
}


输出

Vector is: 10 20 5 23 42 15 Vector after sorting is: 5 10 15 20 23 42 Vector after reversing is: 42 23 20 15 10 5 Maximum element of vector is: 42Minimum element of vector is: 5The summation of vector elements is: 115

6.计数(第一个迭代器,最后一个迭代器,x) –计算向量中x的出现次数。

7.查找(第一个迭代器,最后一个迭代器,x) –返回向量中第一个出现的x的迭代器,并指向向量的最后一个地址((向量的名称)。如果向量中不存在元素,则结束()。

CPP

// 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 << "Element found" :
cout << "Element not found" ;
return 0;
}


输出

Occurrences of 20 in vector : 2Element found

8. 二进制搜索 (第一个迭代器,最后一个迭代器,x) –测试x是否存在于排序向量中。

9.下界(第一个迭代器,最后一个迭代器,x) –返回一个迭代器,该迭代器指向[first,last]范围内的第一个元素,该元素的值不小于“x”。

10.上界(第一个迭代器,最后一个迭代器,x) –返回一个迭代器,该迭代器指向[first,last]范围内的第一个元素,该元素的值大于“x”。

C++

// 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;
}


输出

The lower bound is at position: 3The upper bound is at position: 5

一些操作算法

  1. arr.erase(要删除的位置) –这会删除向量中的选定元素,并相应地移动和调整向量元素的大小。
  2. arr.erase(唯一(arr.begin()、arr.end()、arr.end()) –这将删除单行中排序向量中的重复出现。

CPP

// C++ program to demonstrate working of erase()
#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 << "Vector is :" ;
for ( int i=0; i<6; i++)
cout << vect[i]<< " " ;
// Delete second element of vector
vect.erase(vect.begin()+1);
cout << "Vector after erasing the element: " ;
for ( int i=0; i<vect.size(); i++)
cout << vect[i] << " " ;
// sorting to enable use of unique()
sort(vect.begin(), vect.end());
cout << "Vector before removing duplicate "
" occurrences: " ;
for ( int i=0; i<vect.size(); i++)
cout << vect[i] << " " ;
// Deletes the duplicate occurrences
vect.erase(unique(vect.begin(),vect.end()),vect.end());
cout << "Vector after deleting duplicates: " ;
for ( int i=0; i< vect.size(); i++)
cout << vect[i] << " " ;
return 0;
}


输出

Vector is :5 10 15 20 20 23 Vector after erasing the element: 5 15 20 20 23 Vector before removing duplicate  occurrences: 5 15 20 20 23 Vector after deleting duplicates: 5 15 20 23 42 45 

3.下一个置换(第一个迭代器,最后一个迭代器) –这将向量修改为下一个排列。

4.上一个置换(第一个迭代器,最后一个迭代器) –这将向量修改为其先前的排列。

CPP

// 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:" ;
for ( int i=0; i<n; i++)
cout << vect[i] << " " ;
// modifies vector to its next permutation order
next_permutation(vect.begin(), vect.end());
cout << "Vector after performing
next permutation:";
for ( int i=0; i<n; i++)
cout << vect[i] << " " ;
prev_permutation(vect.begin(), vect.end());
cout << "Vector after performing prev
permutation:";
for ( int i=0; i<n; i++)
cout << vect[i] << " " ;
return 0;
}


输出

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.距离(第一个迭代器,所需的位置) –返回从第一个迭代器到所需位置的距离。该函数在查找索引时非常有用。

CPP

// 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;
}


输出

Distance between first to max element: 7

更多—— STL文章 本文由 曼吉特·辛格。 如果你喜欢GeekSforgeks,并且想贡献自己的力量,你也可以写一篇文章,然后把你的文章邮寄给评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写评论

© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享