1

following the code:

#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;
}

This code is dijstra algorithm. this point has error in dijstra function. [Error] '__gnu_cxx::__alloc_traits<std::allocator<std::pair<int, int> > >::value_type' has no member named 'size' I'm not sure what is the error. Why can't I use v[now].size()? If I use a pair in a vector, does size() function not work?

David Kim
  • 39
  • 6
  • 1
    That's the compiler's very roundabout and annoying way of saying that `pair` doesn't have a `size`. (`v[now]` is such a pair.) – molbdnilo Aug 19 '21 at 09:10
  • 2
    There should be a whole load of other error messages, too. – molbdnilo Aug 19 '21 at 09:12
  • 2
    btw `vector > v[n];` is a little odd. It is mixing `std::vector` with a variable length array that you don't need because there is `std::vector`. It doesnt make much sense to mix them. See [Why aren't variable-length arrays part of the C++ standard?](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) – 463035818_is_not_an_ai Aug 19 '21 at 09:14
  • 2
    actually the vla is also the cause of the error you are asking about. Variable lenght arrays have no `size` member. You should read the link above, and then you can forget that VLA exist. Their main use case in C++ seems to be in poor tutorial to confuse and mislead beginners – 463035818_is_not_an_ai Aug 19 '21 at 09:19

1 Answers1

0

use v instead of v[now] because v is 1D vector of pair so instead of v[now][i].second write v[i].second