Thread: Vector of a struct containing another vector

  1. #31
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by R.Stiltskin View Post
    I didn't know that sort() is built-in to vector. It isn't mentioned here:
    http://www.cppreference.com/wiki/stl/vector/start

    If that reference is incomplete, do you have a link to a better one?
    sort() is built-in to C++, not to vector. (It requires a random access iterator, so you need either arrays, or vector, or some custom-built random access object, to use it.)

  2. #32
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by tabstop
    sort() is built-in to C++
    Not really, since it is part of the standard library, not the core language, but admittedly that is being a little pedantic.
    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

  3. #33
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    Quote Originally Posted by laserlight View Post
    It is not. However, like std::vector, it is part of the standard library, available by including <algorithm>.
    Yeah, thanks for pointing that out. It really is time for me to start using those.

  4. #34
    Registered User
    Join Date
    Apr 2008
    Posts
    122
    Alright I just need an addInOrder and binarySearch algorithm for the index vector. That's it and I'm done!!! But yet again...I'm lost.

  5. #35
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    That would be lower_bound() in both cases. You can use binary_search if you're only interested in existence, though.
    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

  6. #36
    Registered User
    Join Date
    Apr 2008
    Posts
    122
    I need to order if first before using lower_bound(). I need to order it by the string word.

  7. #37
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Well, that's what sort() is for, isn't it?

    Actually, you'd use upper_bound() for insertion, though. And if you always do this, you never need to sort(), because it's always 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

  8. #38
    Registered User
    Join Date
    Apr 2008
    Posts
    122
    So this should work right?

    Code:
    void Index::addWord(string word, int pageNumber)
    {
        indexWord index;
        if(word.length() > 3)
        {
            index.value = word;
            index.locations.push_back(pageNumber);
            data.push_back(index);
            vector<int>::iterator current;
            sort (index.value.begin(), index.value.end());
        }
    }
    Wow..this actually sorts the words characters haha. So the string adventure would be adeenrstuv...
    Last edited by Todd88; 12-05-2008 at 12:49 PM.

  9. #39
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Todd88
    So this should work right?
    I have not read through this thread carefully, but at a glance I suspect you want to write it like this instead:
    Code:
    void Index::addWord(string word, int pageNumber)
    {
        if(word.length() > 3)
        {
            indexWord index;
            index.value = word;
            index.locations.push_back(pageNumber);
            sort(index.value.begin(), index.value.end());
            data.push_back(index);
        }
    }
    I moved the declaration of index into the body of the if statement since there is no point creating it if it will not be used. I re-ordered the statements according to how I think they should be ordered, but you'll have to check. I removed the declaration of current since it appears to be unused.
    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

  10. #40
    Registered User
    Join Date
    Apr 2008
    Posts
    122
    Quote Originally Posted by laserlight View Post
    I have not read through this thread carefully, but at a glance I suspect you want to write it like this instead:
    Code:
    void Index::addWord(string word, int pageNumber)
    {
        if(word.length() > 3)
        {
            indexWord index;
            index.value = word;
            index.locations.push_back(pageNumber);
            sort(index.value.begin(), index.value.end());
            data.push_back(index);
        }
    }
    I moved the declaration of index into the body of the if statement since there is no point creating it if it will not be used. I re-ordered the statements according to how I think they should be ordered, but you'll have to check. I removed the declaration of current since it appears to be unused.
    This still sorts the characters of the string value instead of sorting the "data" vector by the string value.

  11. #41
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Todd88
    This still sorts the characters of the string value instead of sorting the "data" vector by the string value.
    hmm... but if you want to sort the data vector, then I would expect something like:
    Code:
    sort(data.begin(), data.end());
    If indexWord is not already less than comparable with an overloaded operator< then you should either overload operator< for indexWord such that it sorts by the string value, or you provide a function object to serve as a predicate for sorting.
    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

  12. #42
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So if you don't want to rearrange the letters in the word, then don't sort index.value. If you want to sort the index, then just sort index. Of course, since index is local to the if-statement, and will cease to exist once that first curly brace is hit, that seems a little extreme.

  13. #43
    Registered User
    Join Date
    Apr 2008
    Posts
    122
    I overloaded the operator < using:

    Code:
    bool indexWord::operator< (indexWord& string)
    {
        return value < string.value;
    }
    It's still telling me that there is no match for operator <

  14. #44
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It needs to be const correct:
    Code:
    bool indexWord::operator< (const indexWord& string) const
    {
        return value < string.value;
    }
    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

  15. #45
    Registered User
    Join Date
    Apr 2008
    Posts
    122
    and the binary_search algorithm should work too correct? It does work for me though.

    Code:
    binary_search (data.begin(), data.end(), word)
    right?

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. Vector class
    By Desolation in forum C++ Programming
    Replies: 2
    Last Post: 05-12-2007, 05:44 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  5. Operators for 3D Vector Mathematics
    By Anarchist in forum C++ Programming
    Replies: 10
    Last Post: 01-31-2003, 07:33 PM