C++字符串操作,字符串下标超出范围

Jaa*_*nus 3 c++ string visual-studio visual-c++

基本上应该做的是:

1)获取字符串并找到其长度

2)遍历所有元素key并将所有独特成员放入start(playfair密码)

Table::Table(string key) {
    int i;
    for(i = 0; i < key.length(); i++) {
        if(start.find(key[i]) == string::npos) { //start is empty string
            start[start.length()] = key[i]; // this line gives error
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

错误:

在此输入图像描述

Arm*_*yan 6

因为有效指数范围从0高到length - 1包含.如果要在字符串中添加char,请使用push_back

start.push_back(key[i]); //this will increase the length by 1
Run Code Online (Sandbox Code Playgroud)