使用不起作用的字符串打印出std :: array

时间:2015-03-30 18:38:20

标签: c++ arrays string cout

我有以下代码,但它没有打印到控制台:

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;行,我得到错误,有一个unhandeld异常,但问题是什么?为什么抛出异常,我该如何纠正?

异常追踪:

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.

1 个答案:

答案 0 :(得分:3)

交换线条:

    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