EXEC BAD ACCESS Итерация по вектору - PullRequest
0 голосов
/ 25 сентября 2018

У меня есть следующий код, который пытается перебрать вектор.Он принимает в качестве параметров значение и два итератора: начало и конец.Он не работает именно при while (start_iter != end_iter), генерирующем код ошибки EXC_BAD_ACCESS 1.

Вот код, где он взрывается:

list<int>::iterator const 
find_gt(
    vector<list<int> >::iterator start_iter, 
    vector<list<int> >::iterator end_iter, 
    int value)
{
    while (start_iter != end_iter)
    {
        if (start_iter->front() < value)
        {
            break;
        }
        ++start_iter;
    }
    return start_iter->begin();
}

А вот код, который его вызывает:

// Reads a file into the adjacency list
void readfile(string const filename, vector <list<int> > adjList) 
{
    std::ifstream file(filename);
    if (!file.fail())
    {
        string line;
        int i = 0;
        int valueToInsert;
        while (file >> valueToInsert) 
        {
            auto it = find_gt(adjList.begin(), adjList.end(), valueToInsert);
            adjList[i].insert(it, valueToInsert);
            i++;
        }
        file.close();
    }
    else
    {
        cout << "Could not open file!\n";
        throw std::runtime_error("Could not open file!");
    }
}


int main()
{
    // Vector of integer lists called adjList for adjacency list
    vector <list<int> > adjList;

    // Read the file contents into the adjacency list
    readfile("input.txt", adjList);

    return 0;
}

1 Ответ

0 голосов
/ 25 сентября 2018

В вашей функции вы возвращаете start_iter->begin(), даже когда start_iter равно end_iter.

Это вне пределов доступа к памяти.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...