C ++向量下标超出范围 [英] C++ vector subscript out of range

查看:232
本文介绍了C ++向量下标超出范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,我收到了标题中提到的错误。这是代码:



Hi, in the following code, I''m getting the error mentioned in the title. Here''s the code:

typedef struct node		// this acts as the vertex of graph
{
	string name;		// stores the unique name of the vertex
	int value;			// stores the integer data saved in the vertex
} node;

class graph
{
private:
	vector< vector<node> > adjList;		// 2D vector to store the adjencey list of the graph

public:
	graph()								// default constructor
	{
		adjList.resize(0);
	}void printBFSTrav()
	{
		stack<node> stck;
		vector<string> visited;
		vector<node> result;
		//result.resize(adjList.size());

		stck.push(adjList[0][0]);
		visited.push_back(adjList[0][0].name);

		node current;
		int i=0, vTrav=0;
		while(!stck.empty())
		{
			current = stck.top();
			result.push_back( current );
			stck.pop();

			i=0;
			for(int size=adjList.size(); i<size && adjList[0][i].name != current.name; ++i);

			for(int j=1, size=adjList[i].size(); j<size; ++j)
			{
				for(vTrav=0; (vTrav < visited.size()) && (visited[vTrav] != adjList[i][j].name); ++vTrav);

				if(vTrav!=visited.size())
					continue;

				stck.push( adjList[i][j] );
				visited.push_back( adjList[i][j].name );
			}
		}

		for(int i=0; i<result.size(); ++i)
			cout << endl << result[i].name << "(" << result[i].value << ")";  //************************
	}





我在这行时收到错误:



for(int size = adjList.size(); i< size mode =hold>

第三次执行。所有矢量索引都在AFAIK范围内。



I get the error when this line:

for(int size=adjList.size(); i<size mode="hold">
gets executed third time. All vector indexes are within the range AFAIK.

推荐答案

您正在重复使用变量有两个目的:

You are reusing a variable for 2 purposes:
for(int size=adjList.size(); i < size && adjList[0][i].name != current.name; ++i)
  for(int j=1, size=adjList[i].size(); j<size; ++j) ...



变量大小在外循环中定义(我可能会以不寻常的方式添加),显然是外循环大小...然后你重新定义它变量为in ner循环大小。当内循环完成时,你转到外循环的第二次迭代,但是大小值没有正确重置。



可能还有其他错误(我没有看看过去了,但你想在循环外移动int size = ...我会建议:




The variable size is defined in the outer loop (I might add, in an unusual fashion), to apparently be the outer loop size ... then you redefine the same variable as the inner loop size. When the inner loop completes, you go to the second iteration of the outer loop, but the size value is not reset properly.

There may be other errors (I didn''t look past this), but you want to move the "int size=..." outside the loops and I would suggest:

int isize = adjList.size();    // defined outside the outer loop
// ...
int jsize = adjList[i].size(); // defined inside the outer loop, outside the inner loop


在语句中

In the statement
for (int size=adjList.size();
            i < size && adjList[0][i].name != current.name;
                     ++i);



实际上是指: adjList [i] [0] 。 size是外部索引的维度,这是符号中的第一个。


you actually meant: adjList [i][0]. size is the dimension of the "outer" index, which is the first one in the notation.


这篇关于C ++向量下标超出范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆