小白还在写代码 2022-06-19 16:00 采纳率: 100%
浏览 21
已结题

string中assign函数赋值

 string str4;
    str4.assign("Hello C++");
    cout << "str4 = " << str4 << endl;

 string str5("Hello C++", 5);
    cout << "str5 = " << str5 << endl;

这段运行的结果str5是Hello
而把str5改成string str5(str4, 5);为什么输出的是 “C++”?就相当于功能反过来了?

  • 写回答

1条回答 默认 最新

  • 真相重于对错 2022-06-19 16:11
    关注

    因为它调用的是
    basic_string( const basic_string& other,
    size_type pos,
    const Allocator& alloc = Allocator() );
    这个构造函数第二个参数指的是从何处复制

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 6月27日
  • 已采纳回答 6月19日
  • 创建了问题 6月19日