Logo C++ Online Judge

C++

时间限制:1 s 空间限制:512 MB
TA:
Statistics

本题要求你自行实现一个 string 类,使以下示例代码能够正常运行。

#include <iostream>

class string;

int main()
{
    using namespace std::literals;

    // Creating a string from const char*
    string str1 = "hello";

    // Creating a string using string literal
    string str2 = "world"s;

    // Concatenating strings
    string str3 = str1 + " " + str2;

    // Print out the result
    std::cout << str3 << '\n';

    auto pos = str3.find(" ");
    str1 = str3.substr(pos + 1); // the part after the space
    str2 = str3.substr(0, pos);  // the part till the space

    std::cout << str1 << ' ' << str2 << '\n';

    // Accessing an element using subscript operator[]
    std::cout << str1[0] << '\n';
    str1[0] = 'W';
    std::cout << str1 << '\n';
}

正确的输出应为:

hello world
world hello
w
World