“String subscript out of range” error when changing from character array to string?

后端 未结 2 2039
孤街浪徒
孤街浪徒 2021-01-16 14:56

I\'m starting to use strings in place of character arrays and am encountering an error when I change one of my character arrays defined with a size of 5 to a string. The err

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-16 15:26

    newWord[k]
    

    The size of the newWord string is zero. The behavior of indexing beyond the end of a std::string is not defined.

    You may need to resize the string, thus:

    newWord.resize(5);
    

提交回复
热议问题