简体   繁体   English

矢量<bool>迭代器不可解引用

[英]vector <bool> iterator not dereferencable

I am trying to debug my program after writing it and I run into this error:我在编写程序后尝试调试我的程序,但遇到了这个错误:

在此处输入图片说明

Here is my code:这是我的代码:

#include<fstream>
#include <iostream>
#include <vector>
#include <string>
#define MAXINT 2147483647

using namespace std;

struct Edge
{
int id, weight;

Edge(int y, int w)
{
    id = y;
    weight = w;
}
};

struct Node
{
vector <Edge *> Edges;
};

struct Graph
{
vector < Node *> vertices;
vector < int > indices;

void Resize(int x)
{
    if (vertices.capacity() < x)
        vertices.resize(x);
}

void InsertEdge(int x, int y, int weight)
{
    Resize(((x > y) ? x : y) + 1);
    InsertVertex(x);
    InsertVertex(y);
    vertices[x]->Edges.push_back(new Edge(y, weight));
}

void InsertVertex(int x)
{
    if (vertices[x] == NULL)
    {
        Node *t = new Node;
        vertices[x] = t;
        indices.push_back(x);
    }
}
};



void Dij(Graph const &g, int start)
{
Node *temp;
vector<bool> check;
vector<int>  distance, prev;
int v, w, weight, dist;

for (int i = 0; i <= g.indices.size(); i++)
{
    check.push_back(false);
    distance.push_back(MAXINT);
    prev.push_back(-1);
}

v = start;
distance[v] = 0;

while (!check[v])
{
    check[v] = true;
    temp = g.vertices[v];


    for (int i = 0; i < temp->Edges.size(); i++)
    {
        w = temp->Edges[i]->id;
        weight = temp->Edges[i]->weight;

        if (distance[w] > (distance[v] + weight))
        {
            distance[w] = distance[v] + weight;
            prev[w] = v;
        }
    }

    v = 1;
    dist = MAXINT;

    for (int x = 0; x < g.indices.size(); x++)
    {
        int i = g.indices[x];

        if (!check[i] && dist > distance[i])
        {
            dist = distance[i];
            v = i;
        }
    }
}
}

int main()
{
int startNode, nodeOne, nodeTwo, number;
Graph g;
ifstream myReadFile;
myReadFile.open("P:\\Documents\\New Folder\\Test\\src\\Read.txt");
while (!myReadFile.eof()) 
{
    myReadFile >> nodeOne;
    myReadFile >> nodeTwo;
    myReadFile >> number;
    g.InsertEdge(nodeOne, nodeTwo, number);
}

cout<< "Enter the starting node: ";
cin >> startNode;

Dij(g, startNode);

return 0;
}

I apologize for the annoying formatting =/.我为烦人的格式道歉 =/。 It breaks during the last for loop in the dij method.它在 dij 方法的最后一个 for 循环中中断。 Anyone know what I might be omitting?有谁知道我可能会遗漏什么?

Paddy is right!稻田说得对!

But as a suggestion, don't use vector<bool> ...但作为建议,不要使用vector<bool> ...

See, the C++ gods wanted to create a space saving storage structure to store bools .看,C++ 众神想要创建一个节省空间的存储结构来存储bools To make it space efficient, they used bits .为了提高空间效率,他们使用了bits Because there is no bits unit in C++: they were forced to use chars .因为 C++ 中没有bits单位:他们被迫使用chars But a char is 8 bits!!但是一个字符是 8 位!! The C++ gods came up with a unique solution: they made a special member type: reference to access the bools. C++ 大神想出了一个独特的解决方案:他们制作了一个特殊的成员类型:访问 bool 的reference You CANNOT access the boolean values any other way.您不能以任何其他方式访问boolean值。 Technically, vector<bool> isn't even a container: due to the fact that the elements are chars , iterators can't be dereferenced.从技术上讲, vector<bool>甚至不是一个容器:由于元素是chars ,迭代器不能被取消引用。

A much better and cleaner way to store bits is to use the bitset class.一种更好、更bitset存储位的方法是使用bitset类。

I think you are pre-populating the wrong number of elements into those vectors.我认为您在这些向量中预填充了错误数量的元素。 You are iterating over g.indices.size() , where it should be g.vertices.size() .您正在迭代g.indices.size() ,它应该是g.vertices.size()

The rest of your code knows that indices can be shorter than vertices .您的其余代码知道indices可以比vertices短。 You index the check , distance , and prev vectors with values that you pull out of indices .使用从索引中提取的值对checkdistanceprev向量进行indices The runtime error you're getting is probably due to iterator bounds-checking in Debug mode.您得到的运行时错误可能是由于调试模式下的迭代器边界检查造成的。

This should fix your problem:这应该可以解决您的问题:

for (int i = 0; i <= g.vertices.size(); i++)  // <-- notice the change here
{
    check.push_back(false);
    distance.push_back(MAXINT);
    prev.push_back(-1);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM