gpt4 book ai didi

c++ - 调试断言失败! (字符串下标超出范围)

转载 作者:行者123 更新时间:2023-11-28 06:48:26 25 4
gpt4 key购买 nike

<分区>

好的。所以我正在制作一个简单的程序来试验字符串,并且在程序的一部分中,一个 for循环按顺序垂直显示字符串的每个字母。我正常运行该程序以检查其在整个编码过程中的进度,但是当我将其放入时,发生了错误。这是我的代码:

int main()
{
std::string word1 = "Chicken";
std::string word2("Nuggets");
std::string word3(3, '!');

std::string phrase = word1 + " " + word2 + word3;

std::cout << "The phrase is: " << phrase << "\n\n";

std::cout << "This phrase has " << phrase.size() << " letters!" << "\n\n";

std::cout << "The character at position zero is " << phrase[0] << "\n\n";

phrase[0] = 'S';

std::cout << "The phrase is now... " << phrase << "\n\n";

for (unsigned int i = 0; i <= phrase.size(); ++i)
{
std::cout << "Position " << i << "'s character is " << phrase[i] << "\n";
}

}

非常感谢任何帮助,以及对我的代码的反馈(因为我是新手)。

附言<iostream><string>包括在内,我只是无法在
上向您展示它网站。对不起!

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com