如何解决矢量下标超出范围线1501
我正在尝试从文件中读取数据,并从中制作图形。我在常规函数式编程中编写的代码很好。当我尝试将其转换为面向对象的编程方法时,它给出了错误。这是我的函数式编程代码。
struct Edge {
int src,dest,weight;
};
vector<vector<pair<int,int>>> MakeGraph(int noOfVerticies,vector<Edge> const& edges)
{
vector<vector<pair<int,int>>> adjList;
adjList.resize(noOfVerticies);
for (auto& edge : edges)
{
int src = edge.src;
int dest = edge.dest;
int weight = edge.weight;
// insert at the end
adjList[src].push_back(make_pair(dest,weight));
}
return adjList;
}
void printGraph(vector<vector<pair<int,int>>> adjList,int N)
{
//cout << N;
for (int i = 0; i < N; i++)
{
// print all neighboring vertices of given vertex
for (pair<int,int> v : adjList[i])
cout << "(" << i << "," << v.first <<
"," << v.second << ") ";
cout << endl;
}
}
vector<vector<pair<int,int>>> readAndMakeGraph()
{
Edge e1;
int noOfvertices;
bool isFirst = false;
ifstream inFile("a1.txt");
char ch;
vector <Edge> v1;
while (!inFile.eof())
{
if (isFirst == false)
{
inFile >> ch;
noOfvertices = static_cast<int>(ch)-48;
isFirst = true;
//cout << noOfvertices;
}
else
{
inFile >> ch;
e1.src = static_cast<int>(ch) - 48;
inFile >> ch;
e1.dest = static_cast<int>(ch) - 48;
inFile >> ch;
e1.weight = static_cast<int>(ch) - 48;
v1.push_back({ e1.src,e1.dest,e1.weight });
}
}
inFile.close();
vector<vector<pair<int,int>>> a1 = MakeGraph(noOfvertices,v1);
printGraph(a1,noOfvertices);
return a1;
}
int main()
{
vector<vector<pair<int,int>>> a1;
a1 = readAndMakeGraph();
}
文件格式为
noOfVertices
src,weight
src,weight
现在我的面向对象代码是
Graph.cpp
#include "Graph.h"
void Graph::MakeGraph(vector<Edge> const& edges)
{
vector<vector<pair<int,int>>> adjList;
adjList.resize(noOfVertices);
for (auto& edge : edges)
{
int src = edge.src;
int dest = edge.dest;
int weight = edge.weight;
// insert at the end
adjList[src].push_back(make_pair(dest,weight));
}
}
void Graph::printGraph()
{
for (int i = 0; i < noOfVertices; i++)
{
// print all neighboring vertices of given vertex
for (pair<int,int> v : adjList[i])
cout << "[" << i << "," << v.second << "] ";
cout << endl;
}
}
void Graph:: readAndMakeGraph()
{
Edge e1;
int noOfvertices;
bool isFirst = false;
ifstream inFile("a1.txt");
char ch;
vector <Edge> v1;
while (!inFile.eof())
{
if (isFirst == false)
{
inFile >> ch;
noOfvertices = static_cast<int>(ch) - 48;
isFirst = true;
//cout << noOfvertices;
}
else
{
inFile >> ch;
e1.src = static_cast<int>(ch) - 48;
inFile >> ch;
e1.dest = static_cast<int>(ch) - 48;
inFile >> ch;
e1.weight = static_cast<int>(ch) - 48;
v1.push_back({ e1.src,e1.weight });
}
}
inFile.close();
MakeGraph(v1);
}
Graph.h
#pragma once
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
struct Edge {
int src,weight;
};
class Graph
{
vector<vector<pair<int,int>>> adjList;
int noOfVertices;
public:
void MakeGraph(vector<Edge> const& edges);
void printGraph();
void readAndMakeGraph();
};
我的主要是
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include "Graph.h"
using namespace std;
int main()
{
Graph h1;
h1.readAndMakeGraph();
h1.printGraph();
}
解决方法
在函数def random_list(amount,minval,maxval,none_rate):
from random import choice
from numpy import linspace
arr = linspace(minval,amount)
none_count = round(float(amount * none_rate))
rep = []
col = [d for d in range(amount)]
while none_count > 0:
gen = choice(col)
if gen not in rep:
arr[gen] = None
none_count -= 1
rep.append(gen)
return arr
print(random_list(10,100,0.3))
中,定义一个函数局部变量Graph::readAndMakeGraph
,它与Graph成员变量int noOfVertices
不同。稍后,当您调用Graph::noOfVertices
时,它正在查看从未初始化的后者。
如果仅从该函数中删除行printGraph
,它应该解决当前的问题。
int noOfVertices;
中发生相同的问题,它声明了函数局部变量Graph::MakeGraph
。然后,adjList
看着printGraph
,但从未设置。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。