如何用参数将std::string复制到std::string?

How to copy std::string to std::string with parameters?

我无法解决带参数复制字符串的问题。 我找到了使用 char * 指针的方法,但它们实际上是在浪费额外的资源:

using namespace std;
...
string word, halfWord_0S, halfWord_1S;
char halfWord_0[100], halfWord_1[100];
...
if ((i % 2) == 0)
    {
        word.copy(halfWord_0, (i / 2), 0);
        word.copy(halfWord_1, (i / 2), ((i / 2)));
    }   else    {
        word.copy(halfWord_0, ((i / 2) + 1), 0);
        word.copy(halfWord_1, ((i / 2) + 1), (i / 2));
    }
    halfWord_0S = halfWord_0;
    halfWord_1S = halfWord_1;
...

我只想与 std::string 一起工作而没有 char *。在这种情况下这可能吗?

尝试使用此代码段:

std::string base = "some text";
std::string half = base.substr(0, base.length()/2);
std::string secondHalf = base.substr(base.length()/2);