gpt4 book ai didi

C++ 数组迭代

转载 作者:太空宇宙 更新时间:2023-11-03 10:34:32 24 4
gpt4 key购买 nike

谁能找出使用以下 C++ 代码没有任何内容打印到控制台的原因;

string array[] = { "a", "b", "c", "d" };

int length = sizeof(array);
try
{
for (int i = 0; i < length; i++)
{
if (array[i] != "") cout << array[i];
}
}
catch (exception &e)
{
e.what();
}

最佳答案

你使用了错误的长度:

int length = sizeof(array)/sizeof(array[0])

你在控制台上看不到任何东西的真正原因是输出被缓冲了,而且因为你没有写一个换行符所以它没有被刷新。与此同时,您的应用程序崩溃了。

关于C++ 数组迭代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6634849/

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