无法在 c++ 中循环后执行代码
Cannot execute code after the loop in c++
我在 c++ 中遇到了一个我不明白的问题。
这是我的代码:
auto DataArray = jvalue.at(U("data")).as_array();
std::cout << "Outside the loop, first output" << std::endl;
for (int i = 0; i <= 10; i++)
{
auto data = DataArray[i];
auto dataObj = data.as_object();
std::wcout << "inside the loop" << std::endl;
}
std::cout << "Outside the loop, second output" << std::endl;
输出:
Outside the loop, first output
inside the loop
inside the loop
inside the loop
inside the loop
inside the loop
inside the loop
inside the loop
inside the loop
inside the loop
inside the loop
Press any key to continue . . .
似乎代码在循环结束时停止。 但是为什么?
但是如果我注释掉
//auto data = DataArray[i];
//auto dataObj = data.as_object();
它没有问题。
顺便说一下,我正在处理 cpprest 并从 API 获取 json 对象数据。jvalue
变量保存结果。
如果我尝试捕获代码:
try {
auto data = DataArray[i];
auto dataObj = data.as_object();
std::wcout << "inside the loop" << std::endl;
}
catch (const std::exception& e) {
std::wcout << e.what() << std::endl;
}
结果是无限循环,输出:not an object
。
请帮忙。谢谢。
我认为你应该使用i < 10
而不是在你的循环中i <= 10
:
for (int i = 0; i < 10; i++)
{
auto data = DataArray[i];
auto dataObj = data.as_object();
std::wcout << "inside the loop" << std::endl;
}
上次迭代未在循环内输出。它在那里失败,没有 10 个索引的DataArray[10]
更好的是使用DataArray.size()
而不是i < 10
for (int i = 0; i < DataArray.size(); i++)
{
auto data = DataArray[i];
auto dataObj = data.as_object();
std::wcout << "inside the loop" << std::endl;
}
相关文章:
- 需要将此代码更改为执行代码
- 当前不会命中断点。没有调试器目标代码类型的可执行代码与此文件关联
- 单步执行代码时重复上一行
- 如何使用介子在C++中执行代码覆盖?
- Visual Studio 2017,C++,在单步执行代码时指向错误的行
- 在 R 中执行C++代码
- 通过 dll 注入在主线程中执行代码
- 无法在 c++ 中循环后执行代码
- 执行C 代码时快速频繁的文件访问
- 执行 C++ 代码后出错
- 一个人如何从代表函数的字符串中执行运行时执行C 代码
- 是否可以在程序崩溃后执行代码?
- 第一次在 Linux 上执行 c++ 代码的时间非常慢
- 计算 JSON 中的条目数并相应地执行代码
- 将在 CATCH 块之后执行代码
- 分析执行C++代码的每一行所花费的确切时间
- 我在执行代码时不断得到"Bus Error"?
- c while()..执行代码行的条件
- 从并行线程在主 Maya 线程上执行代码
- 在调用GNUPLOT之后,如何继续执行C 代码