Thread: vector to list?

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    104

    vector to list?

    Is there a convenient way to convert a vector to a list?
    If not, is there a convenient way to sort the items in a vector?
    Thanks in advance.

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Hmmm, not sure what convenient means. But you can use the STL algorithm sort() to sort a vector.

    [edit]
    Forget first question. Don't know if you mean this, but an easy algorithm: Take the first element of a vector and add it to a list, repeat this until all elements of the vector are processed.
    [/edit]

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    104

    vector to list

    Can you provide me with a short example?
    I've tried:
    Code:
    vector<int>v;
    v.push_back(7);
    v.push_back(5);
    v.push_back(6);
    v.sort();
    and I get an error stating
    "sort not a memeber of vector<int, allocater<int>>"
    or something like that.

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Algorithm sort() is not a member of the vector template class.

    Code:
    #include <vector>
    #include <algorithm>
    
    vector<int> v;
    v.push_back(7);
    v.push_back(5);
    v.push_back(6);
    
    sort(v.begin(), v.end());

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    104
    Is it possible to

    sort(v.end(), v.begin()); ?

    I get no compile error when I try it, but no results either.

  6. #6
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    You get no compiler error, the algorithm sort expects to iterators for template class vector and gets them, so that is not wrong. But the function requires an iterator to the first element in range as first argument and an iterator to the last element as second argument, so I think that is why it doesn't work.

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    For the sort(v.end(),v.begin()); question, I am guessing you want to sort in descending order rather than ascending order. The default sorting method used by the sort function is to go through the elements from start to finish comparing the elements using the '<' operator. If you wish to override this, you can supply an optional third argument to the sort function which is a function that will compare two elements of the vector according to whatever criteria you wish. For example, the following will sort the vector in reverse order:
    Code:
    #include <vector>
    #include <algorithm>
    
    using namespace std;
    
    bool MySort(const int& lhs,const int& rhs)
    {
        return lhs > rhs;  // By itself, the sort algorithm will effectively use
                           // lhs < rhs instead
    }
    
    int main()
    {
        vector<int> v;
    
        v.push_back(7);
        v.push_back(5);
        v.push_back(6);
    
        sort( v.begin(), v.end(), MySort );
    
        return 0;
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 09:01 AM
  3. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  4. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 05:20 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM