Home  >  Article  >  Backend Development  >  How to use STL algorithms to operate on C++ STL containers?

How to use STL algorithms to operate on C++ STL containers?

WBOY
WBOYOriginal
2024-06-03 11:30:04378browse

STL algorithm Process for operating C++ STL containers: Choose the appropriate algorithm: Choose the STL algorithm based on the required operation, such as finding the maximum value, copying elements, or sorting. Determine input and output iterators: Specify the iterator ranges of the input and output containers. Provide a binary function object: define a functor to perform the desired element-wise operation. Calling an algorithm: Use the algorithm() function to call the selected algorithm, passing the iterator range and functor.

如何使用STL算法对C++ STL容器进行操作?

How to use STL algorithms to operate C++ STL containers

The Standard Template Library (STL) provides a powerful collection of algorithms in C++ for operating sequence containers (such as vector, list and map). These algorithms are designed to provide an efficient and reusable mechanism for performing common data processing tasks.

Basic Syntax

The STL algorithm follows the following syntax:

template<typename InputIterator, typename OutputIterator, typename Function>
OutputIterator algorithm(InputIterator first, InputIterator last, OutputIterator result, Function op);

Where:

  • InputIterator and OutputIterator Specifies the iterator type of the input and output containers.
  • first and last are the iterator ranges of the input container.
  • result is the iterator of the output container.
  • op is a binary function object (functor) used to perform operations on input elements.

Practical case

1. Find the maximum value

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  vector<int> numbers = {1, 3, 5, 7, 9};

  auto max_value = *max_element(numbers.begin(), numbers.end());

  cout << "最大值:" << max_value << endl;

  return 0;
}

输出:

Maximum value: 9

**2. 复制元素**

include 9812357ada2ea3af4b95993351035df6

include 7d10b7d419803d4062679b4587905232

using namespace std;

int main() {
vectora549f44e00b17612193f9ef5739df6e9 copy;

copy.reserve(numbers.size()); // Reserve space to improve efficiency

copy_n(numbers.begin(), numbers.size(), back_inserter(copy));

for (int num : copy) {

cout << num << " ";

}

cout << endl;

return 0;
}

Output:

1 3 5 7 9

3. Sorting

#include 
#include 
#include 

using namespace std;

int main() {
  vector numbers = {5, 1, 3, 7, 2};

  sort(numbers.begin(), numbers.end());

  for (int num : numbers) {
    cout << num << " ";
  }

  cout << endl;

  return 0;
}

输出:

1 2 3 5 7

The above is the detailed content of How to use STL algorithms to operate on C++ STL containers?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn