【问题标题】:How to copy std::string to std::string with parameters?如何使用参数将 std::string 复制到 std::string?
【发布时间】:2023-03-26 22:40:02
【问题描述】:

我无法解决带有参数的复制字符串的问题。 我已经找到了使用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;
...

我只想使用没有char * 的std::string。在这种情况下可能吗?

【问题讨论】:

  • 什么是std::word?
  • 对不起,我修正了代码

标签: c++ string std


【解决方案1】:

尝试使用这个sn-p:

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

【讨论】:

  • 什么是变量s?
猜你喜欢
  • 2012-12-27
  • 1970-01-01
  • 2012-01-06
  • 2022-01-05
  • 2012-05-02
  • 2021-10-30
  • 1970-01-01
  • 1970-01-01
  • 2021-08-11
相关资源
最近更新 更多