簡體   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