【发布时间】:2016-05-01 01:17:32
【问题描述】:
这是我正在编写的一个函数,它接收由空格分隔的一串单词并将每个单词添加到一个数组中。我不断收到一条错误消息,提示“libc++abi.dylib:以 std::out_of_range:basic_string 类型的未捕获异常终止。”我似乎找不到错误所在。
void lineParser(string line, string words[])
{
string word = "";
int array_index = 0;
int number_of_words = 1;
int string_index = 0;
while (string_index < line.length())
{
if (line.substr(string_index,1) != " ")
{
int j = string_index;
while (line.substr(j,1) != " ")
{
word += line.substr(j,1);
j++;
}
words[array_index] = word;
array_index++;
word = "";
number_of_words++;
string_index = j;
}
else
{
string_index++;
}
}
}
【问题讨论】:
-
您应该将一个可调整大小的容器(如 std::vector)的引用传递给您的函数,而不是数组指针。
标签: c++