简体   繁体   English

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

[英]How to copy std::string to std::string with parameters?

I can't solve the problem with the copying strings with parameters.我无法解决带参数复制字符串的问题。 I have found the way to do it with char * pointers, but they are actually wasting extra resources:我已经找到了使用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;
...

I want to work only with std::string without char * .我只想使用没有char * std::string 。 Is this possible in this case?在这种情况下这可能吗?

Try to use this snippet:尝试使用这个片段:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM