Home  >  Article  >  Backend Development  >  Improvements to STL function objects in C++ 11 and C++ 14?

Improvements to STL function objects in C++ 11 and C++ 14?

王林
王林Original
2024-04-25 22:06:01393browse

STL function objects have undergone major improvements, including perfect forwarding and move semantics in C 11, and function pointer encapsulation and generic lambdas in C 14. These improvements enhance usability, efficiency, and flexibility; for example, a generic lambda simplifies the writing of sorting function objects by simply using std::less{} to sort descendingly.

C++ 11 和 C++ 14 中 STL 函数对象的改进?

Improvements in STL function objects in C 11 and C 14

During the development of the C Standard Library (STL), Function objects have been significantly improved. These improvements are designed to enhance usability, efficiency and flexibility.

Improvements in C 11

  • Perfect forwarding: Perfect forwarding allows function objects to receive and forward function arguments without explicit Type conversion or copying, thereby improving efficiency.

Code example:

struct Forwarder {
  template <typename ...Args>
  void operator()(Args&&... args) const {
    std::forward<Args>(args)...; // 完美转发参数
  }
};
  • move semantics: move semantics allow a function object to move its internal state instead of replication, thereby further improving efficiency.

Code Example:

struct Mover {
  std::string value;

  Mover(Mover&& other) noexcept
    : value(std::move(other.value)) {
    other.value.clear(); // 移出旧值
  }
};

Improvements in C 14

  • Function Pointers Encapsulation: C 14 introduced the std::function type, which encapsulates function pointers, which makes it easier to use function pointers as objects.

Code example:

auto plus = std::function<int(int, int)>([](int a, int b) { return a + b; });
  • Generic lambda: Generic lambda allows the use of templates to specify the type of lambda expression, thereby Provides type safety and flexibility.

Code example:

auto sort_by = [](const auto& a, const auto& b) { return a < b; };

Practical case

Assume there is a data structure of student grades, now we To sort grades using STL function objects.

C 11 Code:

std::vector<int> grades = {90, 85, 95, 88, 92};

std::sort(grades.begin(), grades.end(),
  [](int a, int b) { return a > b; }); // 降序排序

C 14 Code:

std::vector<int> grades = {90, 85, 95, 88, 92};

std::ranges::sort(grades, std::less{}); // 降序排序

As you can see, C 14 The introduction of a generic lambda simplifies the writing of sorting function objects.

The above is the detailed content of Improvements to STL function objects in C++ 11 and C++ 14?. 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