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

📅  最后修改于: 2021-06-26 12:06:12             🧑  作者: Mango

对于所有渴望在竞争性编程中表现出色的人来说,只有不了解STL容器的知识才有用处,直到人们不知道所有STL所提供的内容。
对于所有库函数,STL都有大量的算法:请参阅此处
以下是一些关于向量的最常用算法和《竞争性编程》中最有用的算法:

非操纵算法

  1. sort (first_iterator,last_iterator) –对给定的向量进行排序。
  2. reverse(first_iterator,last_iterator) –反转向量。
  3. * max_element(first_iterator,last_iterator) –查找向量的最大元素。
  4. * min_element(first_iterator,last_iterator) –查找向量的最小元素。
  5. accumulate(first_iterator,last_iterator,求和的初始值) –对向量元素求和
CPP
// A C++ program to demonstrate working of sort(),
// reverse()
#include 
#include 
#include 
#include  //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 vect(arr, arr+n);
 
    cout << "Vector is: ";
    for (int i=0; i


CPP
// C++ program to demonstrate working of count()
// and find()
#include 
#include 
#include 
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 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;
}


C++
// C++ program to demonstrate working of lower_bound()
// and upper_bound().
#include 
#include 
#include 
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 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;
}


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


CPP
// C++ program to demonstrate working
// of next_permutation()
// and prev_permutation()
#include 
#include 
#include 
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 vect(arr, arr+n);
 
    cout << "Given Vector is:\n";
    for (int i=0; i


CPP
// C++ program to demonstrate working of distance()
#include 
#include 
#include 
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 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;
}


输出
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: 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) –如果向量中不存在element,则返回一个迭代器,该迭代器返回向量中x的第一个出现位置,并指向向量的最后一个地址((name_of_vector).end())。

CPP

// C++ program to demonstrate working of count()
// and find()
#include 
#include 
#include 
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 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;
}
输出
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) –返回一个迭代器,该迭代器指向[first,last)范围内第一个元素,该元素的值不小于’x’。

10. upper_bound(first_iterator,last_iterator,x) –返回一个迭代器,该迭代器指向[first,last)范围内的第一个元素,该元素的值大于’x’。

C++

// C++ program to demonstrate working of lower_bound()
// and upper_bound().
#include 
#include 
#include 
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 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: 3
The upper bound is at position: 5

一些操纵算法

  1. arr.erase(要删除的位置) –删除矢量中的选定元素,并相应地移动和调整矢量元素的大小。
  2. arr.erase(unique(arr.begin(),arr.end()),arr.end()) –擦除单行中排序向量中的重复项。

CPP

// C++ program to demonstrate working of erase()
#include 
#include 
#include 
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 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 << "\nVector after erasing the element: ";
    for (int i=0; i<5; i++)
        cout << vect[i] << " ";
 
    // sorting to enable use of unique()
    sort(vect.begin(), vect.end());
 
    cout << "\nVector before removing duplicate "
             " occurrences: ";
    for (int i=0; i<5; i++)
        cout << vect[i] << " ";
 
    // Deletes the duplicate occurrences
    vect.erase(unique(vect.begin(),vect.end()),vect.end());
 
    cout << "\nVector 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. next_permutation(first_iterator,last_iterator) –这将向量修改为其下一个排列。

4. prev_permutation(first_iterator,last_iterator) –这将向量修改为其先前的排列。

CPP

// C++ program to demonstrate working
// of next_permutation()
// and prev_permutation()
#include 
#include 
#include 
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 vect(arr, arr+n);
 
    cout << "Given Vector is:\n";
    for (int i=0; i
输出
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) –返回到第一个迭代器的期望位置的距离。此函数在查找索引时非常有用。

CPP

// C++ program to demonstrate working of distance()
#include 
#include 
#include 
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 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

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。