Thread: STL vectors

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    224

    STL vectors

    is there a way to sort a vector of strings alphabeticly ? or do i got to make my own function to do it. and one more is there a way to compare a string with all of the strings in a vector... (is it done the same way as a array??)

    thanks

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    224
    i answered the first part of my question sort( , ) i thought it could only do int but i was wrong.

  3. #3
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    std::sort and std::find.
    Code:
    vector<string> vec;
    
    vec.push_back("BBB");
    vec.push_back("CCC");
    vec.push_back("AAA");
    
    cout << "Before:";
    copy(vec.begin(), vec.end(), ostream_iterator<string>(cout, " "));
    
    std::sort(vec.begin(), vec.end());
    
    cout << endl << "After:";
    copy(vec.begin(), vec.end(), ostream_iterator<string>(cout, " "));

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    By default, the sort STL function will use the less-than (<) operator defined for whatever type is stored in the vector. This means any type that has a less-than operator defined for it can be pushed onto a vector and sorted using sort. Since the string container has a less-than operator defined for it that works alphabetically, calling the sort function will sort the elements in the vector in an alphabetic fashion. This also means that you can come up with a user defined type (class or struct), define the less-than operator for it, push a bunch of these objects into a vector, and then call sort and the vector will end up sorted according to the rules of your less-than operator.
    "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

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    std::binary_search is more efficient than std::find if your vector is already sorted.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sets of Vectors in STL
    By Steff in forum C++ Programming
    Replies: 8
    Last Post: 06-18-2009, 12:03 AM
  2. STL Vectors of Vectors
    By Beamu in forum C++ Programming
    Replies: 2
    Last Post: 12-31-2008, 05:23 AM
  3. allocating structs within STL vectors
    By aoiOnline in forum C++ Programming
    Replies: 20
    Last Post: 12-05-2007, 02:49 PM
  4. Array of Vectors amd other STL questions
    By kolistivra in forum C++ Programming
    Replies: 16
    Last Post: 04-12-2007, 09:11 AM
  5. STL Vectors
    By Da-Nuka in forum C++ Programming
    Replies: 2
    Last Post: 02-25-2005, 08:35 PM