简体   繁体   English

如何传递向量<pair<int,int> &gt;v[] </pair<int,int>

[英]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.

相关问题 你怎么能传递一个向量<pair<int, int> &gt; 变成一个函数? - How can you pass a vector<pair<int, int> > into a function? 如何传递只读向量 <int> 成为对向量的一部分的函数 <int,int> 没有复制 - How to pass a read-only vector<int> to a function which is part of a vector of pair<int,int> without copying 矢量是怎样的<vector<int> > 比向量“更重” <pair<int,int> >? </pair<int,int></vector<int> - How is vector<vector<int>> "heavier" than vector<pair<int,int>>? 向量将如何进行排序 <pair<int,pair<int,int> &gt;&gt;? - How will the sorting be done with vector<pair<int,pair<int,int>>>? 如何转换向量 <pair<int,int> &gt;到多图 <int,int> 有效率的? - How to convert a vector<pair<int,int> > to multimap<int,int> efficiently? 如何对vector &lt;pair &lt;int,pair进行排序 <int , pair<string , pair<int , int > &gt;&gt;&gt;&gt;? - How to sort vector< pair< int , pair<int , pair<string , pair<int , int > > > > >? 向量 <pair<int,int> &gt; v(大小); 打印时显示0作为值 - vector <pair<int,int>>v(size); showing 0 as values when printed 如何对矢量进行排序 <pair<string , pair<int , int> &gt;&gt;? - How to sort a vector<pair<string , pair<int , int>>>? 打印一个`向量<pair<int, int> &gt;` - Printing a `vector<pair<int, int>>` 如何通过向量 <int> v [n]作为功能的性能指标? - How to pass vector<int>v[n] as perameter in function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM