繁体   English   中英

为什么我不能重置矢量?

[英]Why can't i reset the vector?

我想将向量“is_node_visited”归零并将向量“node_parent”设置为负一,但我的 function 不起作用。 我收到以下错误:

error: no matching function for call to 'to_false'
    to_false(is_node_visited, vertex_count);
    ^~~~~~~~
note: candidate function not viable: no known conversion from 'vector<bool> [vertex_count]' to 'vector<bool> &' for 1st argument
void to_false(vector<bool>& T,int n)
     ^
error: no matching function for call to 'minus_one'
    minus_one(node_parent, vertex_count);
    ^~~~~~~~~
note: candidate function not viable: no known conversion from 'vector<int> [vertex_count]' to 'vector<int> &' for 1st argument
void minus_one(vector<int>& T, int n)
     ^

我的代码:

#include <iostream>
#include <vector>
#include <queue>

using namespace std;

void to_false(vector<bool>& T,int n)
{
    for(int i=0; i<n; i++) T[i] = false;
}

void minus_one(vector<int>& T, int n)
{
    for(int i=0; i<n; i++) T[i] = -1;
}

int main()
{
  int vertex_count, edge_count,first_vertex, second_vertex, i;


  cin>>vertex_count;
  cin>>edge_count;
  vector <int> tab[vertex_count];
  vector <bool> is_node_visited[vertex_count];
  vector <int> node_parent[vertex_count];
  queue<int> Q;

    for(i=0; i<edge_count; i++)
    {
        cin>>first_vertex;
        cin>>second_vertex;
        tab[first_vertex].push_back(second_vertex);
        tab[second_vertex].push_back(first_vertex);
    }

    to_false(is_node_visited, vertex_count);
    minus_one(node_parent, vertex_count);

    return 0;

}
vector <bool> is_node_visited[vertex_count];

这不是向量,它是向量数组, []表示数组。

我想你的意思是这个

vector <bool> is_node_visited(vertex_count);

这是一个初始大小为vertex_countvector<bool>

还有这段代码

int vertex_count;
cin >> vertex_count;
vector <int> tab[vertex_count];

我认为你确实意味着拥有一组向量,C++ 是不合法的。 在 C++ 中,数组大小必须是编译时常量,但vertex_count是一个变量。

你可以有一个向量的向量。

int vertex_count;
cin >> vertex_count;
vector <vector <int>> tab(vertex_count);

那将是合法的 C++。

暂无
暂无

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

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