How to use lambda functions to reverse a string in C++ 20

Lambda functions in C++20 provide a powerful and concise way to define small, inline functions for specific tasks to eliminate the need to create separate named functions. One intriguing application of lambda functions is their ability to reverse a string efficiently. Reversing a string involves changing the order of its characters. Lambda functions allow us to achieve this with minimal complexity and code.

Example of a reversed string
Example of a reversed string

Reversing a string can be a valuable operation in programming for several reasons:

  1. Text Manipulation: When working with textual data, reversing a string can help in tasks like palindromic checks, text analysis, and encryption.

  2. Algorithms and Data Structures: String reversal is fundamental to various algorithms and data structures, such as stack-based operations and linked lists.

  3. User Interface: In graphical applications, reversing strings may be needed for bidirectional text display or mirroring.

The potential of lambda functions

By harnessing the capabilities of lambda functions, we can effortlessly create a compact and readable solution to reverse strings, which makes our code both elegant and functional. Now, we will use a lambda function to reverse a string in C++20 and explore the syntax required to accomplish this task effectively.

Lambda Function Syntax

Lambda functions in C++20 have the following syntax.

[capture_clause] (parameter_list) {
//function body
}
  • Capture Clause: Specifies which variables from the surrounding scope are accessible inside the lambda function.

  • Parameter List: Lists the parameters the lambda function takes, if any.

  • Function Body: Contains the code to be executed when the lambda function is called.

Code example

Press the “Run” button to execute the following code.

#include <iostream>
#include <algorithm>

int main() {
    std::string str = "Hello, world!";
    
    auto reverseString = [](std::string& s) {
        std::reverse(s.begin(), s.end());
    };
    
    reverseString(str);
    std::cout << "Reversed string: " << str << std::endl;
    return 0;
}
Reversing a string using lambda functions

Code explanation

  • Lines 1–2: Include necessary header files, <iostream> and <algorithm>, which provide functionalities for input/output and various algorithms, respectively.

  • Line 4: The main() function is the starting point of the program's execution.

  • Line 5: Declares a std::string variable named str and assigns it "Hello, world!".

  • Line 7: Defines an anonymous lambda function using the auto keyword and assigns it to the reverseString variable. This lambda function takes a reference to a std::string parameter, s.

  • Line 8: Inside the lambda function, uses std::reverse() from the <algorithm> header to reverse the order of characters in the string. The s.begin() and s.end() iterators specify the range of characters to be reversed.

  • Line 11: Calls the reverseString lambda function and passes the str string by reference.

  • Line 12: Prints the reversed string to the console using std::cout.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved