所以,我有以下情况:如何检查一个向量是否在C++中的2d向量中指向空向量?
我宣布整数向量的向量为vector < vector<int> > edges
。基本上,我试图使用其中图被定义如下上述实施的曲线图:
class graph
{
public:
int vertices;
vector < vector<int> > edges;
};
现在,一个边缘的插入过程中,我采取输入作为起始顶点和结束顶点。现在,我想要做这样的事情:
void insert(int a, int b, graph *mygraph) // a is starting vertex and b is ending vertex
{
auto it = mygraph->edges.begin();
//int v = 1;
vector<int> foo;
foo.push_back(b);
if (mygraph->edges[a].size() != 0) // Question here?
mygraph->edges[a].push_back(b);
else
mygraph->edges.push_back(foo);
return;
}
现在,在标有Question here
行了,基本上,我想检查是否为特定条目的载体存在与否? size
实际上是错误的,因为我试图在不存在的向量上调用大小操作。换句话说,我想检查一下,如果有矢量存在于矢量矢量中的特定位置。我该怎么做?例如,mygraph->edges[a] != NULL
?
它会存在当且仅当'一< mygraph-> edges.size()'。 – molbdnilo