Home > Backend Development > C++ > body text

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

王林
Release: 2024-04-25 22:06:01
Original
394 people have browsed it

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)...; // 完美转发参数
  }
};
Copy after login
  • 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(); // 移出旧值
  }
};
Copy after login

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; });
Copy after login
  • 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; };
Copy after login

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; }); // 降序排序
Copy after login

C 14 Code:

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

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

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template