打印出 std::array 字符串不起作用
Printing out std::array with strings not working
我有以下代码,它没有打印到控制台:
void generateRandomStringArray(array<string, N> &arrayRef)
{
cout << "Generating random string array..." << endl;
for (size_t i = 0; i < N; i++)
{
arrayRef[i] = randomString();
cout << "Value of i = " << i << ": " << stoi(arrayRef[i]) << endl;
cout << arrayRef[i] << endl;
}
cout << "Generating array successful." << endl;
};
问题出在第 cout << arrayRef[i] << endl;
行,我得到错误,存在未处理的异常,但问题是什么?为什么抛出异常,我该如何纠正?
异常跟踪:
First-chance exception at 0x76EFC42D in SortingAlgorithms.exe: Microsoft C++ exception: std::invalid_argument at memory location 0x0344F8EC.
If there is a handler for this exception, the program may be safely continued.
换行:
cout << "Value of i = " << i << ": " << stoi(arrayRef[i]) << endl;
cout << arrayRef[i] << endl;
到
cout << arrayRef[i] << endl;
cout << "Value of i = " << i << ": " << stoi(arrayRef[i]) << endl;
并且您会看到错误出现在 stoi
调用中,您在调用 stoi
时使用了一些损坏的字符串。
我有以下代码,它没有打印到控制台:
void generateRandomStringArray(array<string, N> &arrayRef)
{
cout << "Generating random string array..." << endl;
for (size_t i = 0; i < N; i++)
{
arrayRef[i] = randomString();
cout << "Value of i = " << i << ": " << stoi(arrayRef[i]) << endl;
cout << arrayRef[i] << endl;
}
cout << "Generating array successful." << endl;
};
问题出在第 cout << arrayRef[i] << endl;
行,我得到错误,存在未处理的异常,但问题是什么?为什么抛出异常,我该如何纠正?
异常跟踪:
First-chance exception at 0x76EFC42D in SortingAlgorithms.exe: Microsoft C++ exception: std::invalid_argument at memory location 0x0344F8EC.
If there is a handler for this exception, the program may be safely continued.
换行:
cout << "Value of i = " << i << ": " << stoi(arrayRef[i]) << endl;
cout << arrayRef[i] << endl;
到
cout << arrayRef[i] << endl;
cout << "Value of i = " << i << ": " << stoi(arrayRef[i]) << endl;
并且您会看到错误出现在 stoi
调用中,您在调用 stoi
时使用了一些损坏的字符串。