Thread: sort a vector

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    32

    sort a vector

    hi I nees create a vector and reserve it dimension
    Code:
    vector <string> lines (DIMENSION);
    lines[0] = "mi";
    lines[1] = "gi";
    sort(lines.begins(), lines.end());
    Problem: the sort put the strings at the end of vector because it match they with "" (blank). I'd like put it at begin...How do?
    thanks

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by mickey0 View Post
    Problem: the sort put the strings at the end of vector because it match they with "" (blank). I'd like put it at begin...How do?
    thanks
    sort the new strings only
    Code:
    sort(lines.begins(), lines.begin()+2);
    Kurt

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You mean, you've created a vector of strings with several elements filled with data strings (like "mi" and "gi"), and several strings are still empty. So when you go to sort it, you get something like "", "", "", "gi", "mi"? And you want "gi", "mi", without the empty strings?

    The simplest way that comes to mind is to remove the empty strings first, and then sort it. Or you could remove them afterwards, I suppose. Or you could just process the vector starting at the first non-blank element after it was sorted.
    Code:
    #include <iostream>
    #include <vector>
    #include <string>
    
    int main() {
        std::vector<std::string> data(5);
        std::vector<std::string>::iterator i;
        
        data[0] = "one";
        data[1] = "two";
        std::sort(data.begin(), data.end());
        
        for(i = data.begin(); i != data.end() && *i == ""; i ++);
        
        for( ; i != data.end(); i ++) {
            std::cout << '"' << *i << '"' << std::endl;
        }
        
        std::cin.get();
        return 0;
    }
    Output:
    Code:
    "one"
    "two"
    Or, even more simply (and this works for sorted and non-sorted vectors):
    Code:
    #include <iostream>
    #include <vector>
    #include <string>
    
    int main() {
        std::vector<std::string> data(5);
        std::vector<std::string>::iterator i;
        
        data[0] = "one";
        data[1] = "two";
        std::sort(data.begin(), data.end());
        
        for(i = data.begin(); i != data.end(); i ++) {
            if(*i != "") std::cout << '"' << *i << '"' << std::endl;
        }
        
        std::cin.get();
        return 0;
    }
    The output is the same as above.

    [edit] Or, even easier, see ZuK's reply above. [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If you dont actually need the unfilled slots of the vector right away, then perhaps it would be better to write:
    Code:
    vector <string> lines;
    lines.reserve(DIMENSION);
    lines.push_back("mi");
    lines.push_back("gi");
    sort(lines.begin(), lines.end());
    Otherwise, you have to keep track of the number of elements currently in use yourself.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You could also write your own comparator. It should return false if the left string is empty, then true if the right string is empty, then use the regular string less than function.

    That means that empty strings would always be last and the rest of the strings would be sorted normally. This is especially useful if you are providing a valid string at non-sequential indexes (although I'm not sure when you'd need to do that).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. syntax help?
    By scoobygoo in forum C++ Programming
    Replies: 1
    Last Post: 08-07-2007, 10:38 AM
  3. Sorting
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 11-10-2003, 05:21 PM
  4. my vector class..easy dynamic arrays
    By nextus in forum C++ Programming
    Replies: 5
    Last Post: 02-03-2003, 10:14 AM
  5. Operators for 3D Vector Mathematics
    By Anarchist in forum C++ Programming
    Replies: 10
    Last Post: 01-31-2003, 07:33 PM