Using the standard library

文字列の反転
文字列の反転 C++にて

reverse_string.h

#pragma once

#include <string>

namespace reverse_string
{
    std::string reverse_string(std::string str);
}

reverse_string.cpp

#include "reverse_string.h"

#include <algorithm>  // (1) for std::reverse()

namespace reverse_string
{

// (2) the function takes its argument "by value"
std::string reverse_string(std::string str)
{
    // (3) call a function from the standard library that performs the reversal
    std::reverse(begin(str), end(str));

    // str is now reversed
    return str;
}

} // namespace reverse_string

Including the header

The header <algorithm> is part of the standard library, it contains lots of useful functions (see cppreference.com). After including this header we can call the function std::reverse later in the file.

The type of the parameter

The function takes its argument "by value" (std::string). That means the parameter str gets constructed from the argument that the caller passes to the function, and it gets destructed at the end of the function. Any modifications of the parameter will not be visible outside of the function and will not affect the argument of the caller. We do that because the function needs a "working copy" of the string, it will reverse and return that parameter without creating an additional instance of std::string.

Calling std::reverse().

std::reverse() is a function template (in other languages known as "generics") that takes two "bidirectional iterators". These two iterators define a half-open interval [begin, end). std::reverse() reverses the order of the elements in the interval by swapping (see cppreference.com.)

Conclusion

This is a great approach; it is concise, easy to read, idiomatic, and very efficient.

2026年11月04日(水) · 役に立ちましたか?

C++の文字列の反転に対する他のアプローチ

コミュニティがこの演習を解いた他の方法
std::string result;
for (std::size_t i = 0; i < size(original); ++i)
{
    std::size_t rindex = size(original) - i - 1;
    result.push_back(original[rindex]);
}
return result;
Iterative

Build the reversed string iteratively.

std::size_t a = 0;
std::size_t b = size(str) - 1;
while (a < b)
{
    std::swap(str[a], str[b]);
    ++a; --b;
}
return str;
Swapping characters

Modify the parameter, swapping characters manually.

return {rbegin(original), rend(original)};
Direct construction

Construct the reversed string directly.

std::string result;
while (!original.empty())
{
    char c = original.back();
    original.remove_suffix(1);
    result.push_back(c);
}
return result;
Stack operations

Build the reversed string with stack operations.

void recursive_helper(std::string_view input, std::string& output)
{
    if (input.empty())
        return;
    output.push_back(input.back());
    input.remove_suffix(1);
    recursive_helper(input, output);
}
Linear recursion

Build the reversed string with linear recursion.

if (size(original) <= 1)
    return std::string{original};
auto half_size = size(original) / 2;
return reverse_string(original.substr(half_size)) +
       reverse_string(original.substr(0, half_size));
Divide and conquer

Build the reversed string recursively, with divide and conquer.

[[nodiscard]] std::string reverse_string(std::string&& str) noexcept;
[[nodiscard]] std::string reverse_string(std::string_view str);
[[nodiscard]] std::string reverse_string(const char* str);
Over-Engineered

Try to achieve peak performance with function overloading.