Thread: Vector of a struct containing another vector

  1. #16
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Make it std::vector<indexWord>::const_iterator if you only want to read anyway.
    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

  2. #17
    Registered User
    Join Date
    Apr 2008
    Posts
    122
    Alright I got it working. Now how to a output these values in the array according to their first letter?

  3. #18
    Registered User
    Join Date
    Apr 2008
    Posts
    122
    Why is this not getting every single line of the book, the page number, and storing it in the vector?? This is the driver file that runs through the file, gets the line, strips the line into words, then passes those words to the addWord function:

    Code:
        Index create;
    
        int thePageNum = 1;
        while(book.fileOfBook)
        {
            for(int lineNumber = 0; lineNumber < MAX_LINES_PER_PAGE; lineNumber++)
            {
                string theLine = book.readLine();
                char * pch;
                pch = &theLine[0];
                pch = strtok (pch," :#;[]""\"()!*`1234567890'?,.-");
    
                while (pch != NULL)
                {
                    for(int i = 0; pch[i] != '\0'; i++)
                        pch[i] = tolower(pch[i]);
                    string words(pch);
                    create.addWord(words, PageNo);
                    pch = strtok (NULL, " :#;[]""\"()!*`1234567890'?,.-");
                }
            }
            create.output(indexOut, book.theTitle);
            thePageNum++;
        }
    This is the addLine function:

    Code:
    void Index::addWord(string word, int pageNumber)
    {
        indexWord index;
        index.value = word;
        index.locations.push_back(pageNumber);
        data.push_back(index);
    }
    I try to cout the variable "value" from the struct indexWord and it only goes to line MAX_LINES_PER_PAGE. I need it to run through the whole book. Is there something I am doing wrong?

  4. #19
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    " :#;[]""\"()!*`1234567890'?,.-"
    Surely, if this is indeed the same value in both strtok calls, then you probably should have a single declaration of a
    Code:
    const char *separators = " :#;[]""\"()!*`1234567890'?,.-";
    so that it's easy to see that it IS the same thing.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #20
    Registered User
    Join Date
    Apr 2008
    Posts
    122
    Alright I finally got it working. Now how to I look through the vector and output it according to first word?

  6. #21
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    (1) Your driver file is incrementing a variable called "thePageNum" but when it calls the addWord method it sends pageNo as the 2nd parameter.

    (2) It seems that every time strtok returns a word, addWord pushes a new indexWord onto the index vector. Instead, if a word already exists in the vector, don't you want to find it and just add another page number to its vector?

    (3) But before you add another page number, don't you also want to check to see if that page number is already there?

    (4) To print the index in alphabetical order you need to sort it alphabetically. But you might want to consider using a sorted list or a map instead of vector; otherwise to accomplish (2) and (3) you will have to search linearly through the indexWord and pageNo vectors every time.

    (5) Do you really want to index words like "the", "and", ...?

  7. #22
    Registered User
    Join Date
    Apr 2008
    Posts
    122
    Quote Originally Posted by R.Stiltskin View Post
    (1) Your driver file is incrementing a variable called "thePageNum" but when it calls the addWord method it sends pageNo as the 2nd parameter.

    (2) It seems that every time strtok returns a word, addWord pushes a new indexWord onto the index vector. Instead, if a word already exists in the vector, don't you want to find it and just add another page number to its vector?

    (3) But before you add another page number, don't you also want to check to see if that page number is already there?

    (4) To print the index in alphabetical order you need to sort it alphabetically. But you might want to consider using a sorted list or a map instead of vector; otherwise to accomplish (2) and (3) you will have to search linearly through the indexWord and pageNo vectors every time.

    (5) Do you really want to index words like "the", "and", ...?
    I have a condition in my addWord function that doesn't add the words if they are 3 letters or less. Plus this sounds really complicated sorting the vector even though I need to. How do I do this? I haven't learned maps.
    Last edited by Todd88; 12-05-2008 at 09:03 AM.

  8. #23
    Registered User
    Join Date
    Apr 2008
    Posts
    122
    I don't know how to search through the vector of a vector and a string. I also don't know how to output the vector containing the page numbers that's in the vector.

  9. #24
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    I assume you know how to sort the locations vector, since its elements are just ints. Use any sorting algorithm you know. You can access the elements of the vector the same as an ordinary array of ints. Alternatively, use an STL list instead of vector & it comes with a sort method built-in.

    To sort the indexWord vector, again use any sorting algorithm you like. In this case, when you look at two elements to see which comes first, compare the value members only, but when you swap, swap the entire indexWord elements. In order to use list to do this automatically for you, you would first have to give it a comparison function showing it how to order two elements. For example:
    http://www.codeguru.com/forum/showthread.php?t=366063

    Otherwise, try to figure out how to use a map, which will keep things sorted and let you find elements easily. Maybe this will help:
    http://www.cppreference.com/wiki/

  10. #25
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Todd88 View Post
    I don't know how to search through the vector of a vector and a string. I also don't know how to output the vector containing the page numbers that's in the vector.
    I don't know why you think outputting the vector of page numbers is different than outputting the big(ger) vector -- loop through all the elements and print.

    And sort() is built-in -- you just need to define a function that determines when "this" is less than "that".

  11. #26
    Registered User
    Join Date
    Apr 2008
    Posts
    122
    The pages vector is inside the struct indexWord and the indexWord vector is inside the class Index though. I am really sorry but I just don't get it! I am sure this is simple stuff but I don't get it!!

  12. #27
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Todd88 View Post
    The pages vector is inside the struct indexWord and the indexWord vector is inside the class Index though.
    Why do you think that makes a difference? Once you have a hold of the vector, it doesn't matter where it came from, just loop through it.

  13. #28
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    Quote Originally Posted by tabstop View Post
    And sort() is built-in -- you just need to define a function that determines when "this" is less than "that".
    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?

  14. #29
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    sort() is a free function, not a member of vector.
    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

  15. #30
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by R.Stiltskin
    I didn't know that sort() is built-in to vector.
    It is not. However, like std::vector, it is part of the standard library, available by including <algorithm>.
    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

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