关于算法分析与设计的思考

问题遇到的现象和发生背景:在课本的序章讲解STL string 类的例题中出现了如下伪代码:

    char cstr[] = "China!Great Wall";
    string s1(cstr);                //s1:China!Great Wall
    string s2(s1);                //s2:China!Great Wall
    string s3(cstr, 7, 11);    //s3:Great Wall
    string s4(cstr, 6);            //s4:China!I
    string s5(5, 'A');            //s5:AAAAA
}

然后我发现s4的结果 并不是明白,所以我去带到ide环境

#include<iostream>
#include<string>

using namespace std;

void main(){

    char cstr[] = "China!Great Wall";
    string s1(cstr);        //s1:China!Great Wall
    string s2(s1);            //s2:China!Great Wall
    string s3(cstr, 7, 11); //s3:Great Wall
    string s4(cstr, 6);        //s4:China!
    string s5(5, 'A');        //s5:AAAAA
}

运行结果及报错内容

img

我想要达到的结果:理解s4的结果以及解决代码问题

报什么错???

VS 2017,Clion 2021.3 + GCC 4.8.5,实测都没问题。你可能需要检查一下你的IDE安装、工程设置,或者新建一个简单项目看看。

#include <iostream>
#include <string>

using namespace std;

int main()
{
    char cstr[] = "China!Great Wall";
    string s1(cstr);                //s1:China!Great Wall
    string s2(s1);                //s2:China!Great Wall
    string s3(cstr, 7, 11);    //s3:Great Wall
    string s4(cstr, 6);            //s4:China!I
    string s5(5, 'A');            //s5:AAAAA

    cout << s1 << endl;
    cout << s2 << endl;
    cout << s3 << endl;
    cout << s4 << endl;
    cout << s5 << endl;
    return 0;
}