Vector of a struct containing another vector

This is a discussion on Vector of a struct containing another vector within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by R.Stiltskin 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 ...

  1. #31
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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
    21,414
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #33
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    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,893
    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,893
    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 11:49 AM.

  9. #39
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,414
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,414
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,185
    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
    21,414
    It needs to be const correct:
    Code:
    bool indexWord::operator< (const indexWord& string) const
    {
        return value < string.value;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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?

Page 3 of 5 FirstFirst 12345 LastLast
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, 06:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21