[英]How to pass vector<pair<int,int> >v[]
I'm trying to pass a vector<pair<long,long> > adj[n]
to another function called short but it gives me error but I've written code like this before so I don't why this is giving an error now我正在尝试将
vector<pair<long,long> > adj[n]
传递给另一个名为 short 的 function 但它给了我错误但我之前写过这样的代码所以我不知道为什么这会给出错误现在
error: invalid declarator before 'adj'错误:“adj”之前的声明符无效
error: expected ')' before 'adj'错误:在 'adj' 之前应为 ')'
#include <bits/stdc++.h>
using namespace std;
void short(vector<pair<long,long> >adj[], int n){
vector<long>dist(n+1,INT_MAX);
vector<long>parent(n+1, -1);
dist[1] = 0;
parent[1] = 1;
priority_queue<pair<long,long>,vector<pair<long,long> >,greater<pair<long,long> > > pq;
pq.push(mp(0,1));
vector<bool>flag(n+1, false);
while(!pq.empty()){
long u = pq.top().S;
pq.pop();
flag[u] = true;
for(auto it = adj[u].begin(); it!=adj[u].end(); it++){
long v = (*it).F;
long wt = (*it).S;
if(!flag[v] && dist[u] +wt < dist[v]){
dist[v] = dist[u]+wt;
pq.push(mp(dist[v],v));
parent[v] = u;
}
}
}
print(parent, 1);
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
long n, m;
cin >> n >> m;
vector<pair<long,long> > adj[n+1];
long u, v, w;
loop(i,m){
cin >> u >> v >> w;
adj[u].pb(mp(v,w));
adj[v].pb(mp(u,w));
}
short(adj,n);
}
void short(vector<pair<long,long> >adj[], int n){
Rename function short
to something else.将 function
short
重命名为其他名称。 short
is a C++ fundamental type. short
是 C++ 基本类型。 You cannot use it as the name of an identifier.您不能将其用作标识符的名称。
(Originally posted as comment under question. Adding it as answer as per this ) (最初发布为问题下的评论。根据此添加它作为答案)
actually when we define the vector with some size we use parenthesis instead of that square bracket...实际上,当我们定义具有一定大小的向量时,我们使用括号而不是方括号......
vector>adj(n)..this is the correct format.. vector>adj(n)..这是正确的格式..
and also change your function name to something else as short is a keyword in c++并将您的 function 名称更改为其他名称,因为短是 c++ 中的关键字
hope you get it:)希望你能明白:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.