c++ [错误] '__gnu_cxx::__alloc_traits< std::allocator< std::pair< int,int> >

axzmvihb  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(137)

按照代码:

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

using namespace std;

int n[20];

class State{
    public:
    int pos,val;
    State(int pos, int val){
        this->pos = pos;
        this->val = val;
    }
    bool operator<(State const &s) &{
        return this->val > s.val;
    }
};

void dijstra(vector<pair<int,int> > &v){
    
    priority_queue<State> pq;
    pq.push(State(1,0));
    
        while(!pq.empty()){
            State tmp = pq.top();
            pq.pop();
            int now = tmp.pos;
            int val = tmp.val;
            for(int i = 0; i < v[now].size(); i++){ // this error point
                int next = v[now][i].fisrt;
                int nextval = v[now][i].second;
                if(nextval + value < n[next]){
                    n[next] = nextval + val;
                    pq.push(State(next,nextval+val));
                }
            }
        }
    
}

int main() {
    
    int n,m,a,b,c;
    scanf("%d %d",&n,&m);
    vector<pair<int,int> > v[n];

    
    for(int i = 0; i < m; i++){
        scanf("%d %d %d",&a,&b,&c);
        v[a].push_back({b,c});
    }
    
    for(int i = 1; i <= n; i++){
        n[i] = 2147000000;
    }
    
    n[0] = 0;
    DFS(v);
    
    for(int i = 2; i <= n; i++){
        printf("%d : %d\n",i,arr[i]);
    }
    
    return 0;
}

字符串
这段代码是dijstra算法。这一点在dijstra函数中有错误。[错误] '__gnu_cxx::__alloc_traits<std::allocator<std::pair<int,int> >::value_type'没有名为'size'的成员我不确定是什么错误。为什么我不能使用v[now].size()?如果我在vector中使用pair,size()函数不起作用吗?

1l5u6lss

1l5u6lss1#

v代替v[now],因为v是pair的一维向量,所以用v[i].second代替v[now][i].second

相关问题