Thread: Vector and priority queue

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    197

    Vector and priority queue

    i have a sort of this :
    Code:
    vector < vector <int > >a(100);
    say for eg: a[1] has has a list of elements...
    I have defined t priority queue stl to hold vector of elemnts..
    But on a single push i need all the elemts of 1..ie a[1] to be stored in the priority queue
    something like this
    Code:
    Q.push(a[1])
    i need all the elemts of 1 to be pushed at once...
    The above doesnt work...it says
    No matching.....bla bla bla
    any suggestions

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> i have a sort of this :

    Well it "sort of" matters what *exactly* you have.

    >> The above doesnt work...it says "No matching.....bla bla bla"

    That just means that you've passed it the wrong...whatchyamacallit.

    >> any suggestions

    Sure: Don't ask such vague questions.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    Quote Originally Posted by Sebastiani View Post
    >> i have a sort of this :

    Well it "sort of" matters what *exactly* you have.

    >> The above doesnt work...it says "No matching.....bla bla bla"

    That just means that you've passed it the wrong...whatchyamacallit.

    >> any suggestions

    Sure: Don't ask such vague questions.
    I did exactly what i asked..The question was not vague...i gave with an example. and snippet...
    My question is not answered

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    No matching.....bla bla bla
    That bla bla bla is pretty important to diagnose your problem. The compilers emit messages for a reason, so you read them, understand what's wrong and fix the error.

    Code:
    #include <vector>
    #include <queue>
    
    int main()
    {
        std::vector<std::vector<int> > v(100);
        std::priority_queue<std::vector<int> > pq;
        pq.push(v[1]);
    }
    Compiles for me. So you haven't provided enough context.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Jul 2009
    Location
    MD
    Posts
    10
    So do you want the vector stored in a[1] to get pushed into the priority queue to or do you want the elements in the vector to get pushed? Your question is very vague and I think you might not realize that you're dealing with a vector of a vector of ints, rather than a single vector. To visualize your:
    Code:
    vector < vector <int > >a(100);
    This means that you have a 2-dimensional matrix of integers. If you want to push the integers themselves you'll need to loop through each vector individually.

  6. #6
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Quote Originally Posted by anon View Post
    Code:
    #include <vector>
    #include <queue>
    
    int main()
    {
        std::vector<std::vector<int> > v(100);
        std::priority_queue<std::vector<int> > pq;
        pq.push(v[1]);
    }
    This code made me think. How does priority_queue compare elements?
    Code:
    #include <vector>
    #include <iostream>
    #include <queue>
    
    int main()
    {
        std::vector<std::vector<int> > v;
        std::vector<int> temp;
        temp.push_back(3);
        temp.push_back(7);
        temp.push_back(2);
        v.push_back(temp);
        temp.clear();
        temp.push_back(13);
        temp.push_back(17);
        temp.push_back(12);
        v.push_back(temp);
        std::priority_queue<std::vector<int> > pq;
        pq.push(v[0]);
        pq.push(v[1]);
        std::cout << pq.top()[0] << std::endl;
    }
    prints 13 for me. I was wondering if it just looked at the first element of the vector. What exactly is happening? I see it uses a template function
    Code:
    class Compare = less<typename Container::value_type>
    but what is Container::value_type?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by linuxdude View Post
    This code made me think. How does priority_queue compare elements?
    Code:
    #include <vector>
    #include <iostream>
    #include <queue>
    
    int main()
    {
        std::vector<std::vector<int> > v;
        std::vector<int> temp;
        temp.push_back(3);
        temp.push_back(7);
        temp.push_back(2);
        v.push_back(temp);
        temp.clear();
        temp.push_back(13);
        temp.push_back(17);
        temp.push_back(12);
        v.push_back(temp);
        std::priority_queue<std::vector<int> > pq;
        pq.push(v[0]);
        pq.push(v[1]);
        std::cout << pq.top()[0] << std::endl;
    }
    prints 13 for me. I was wondering if it just looked at the first element of the vector. What exactly is happening? I see it uses a template function
    Code:
    class Compare = less<typename Container::value_type>
    but what is Container::value_type?
    Container::value_type is pretty clearly std::vector<int>. Operator < is defined for any standard vector, using lexicographical_compare.

Popular pages Recent additions subscribe to a feed