Make it std::vector<indexWord>::const_iterator if you only want to read anyway.
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
Alright I got it working. Now how to a output these values in the array according to their first letter?
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:
This is the addLine 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++; }
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?Code:void Index::addWord(string word, int pageNumber) { indexWord index; index.value = word; index.locations.push_back(pageNumber); data.push_back(index); }
Surely, if this is indeed the same value in both strtok calls, then you probably should have a single declaration of aCode:" :#;[]""\"()!*`1234567890'?,.-"so that it's easy to see that it IS the same thing.Code:const char *separators = " :#;[]""\"()!*`1234567890'?,.-";
--
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.
Alright I finally got it working. Now how to I look through the vector and output it according to first word?
(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 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 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/
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!!
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 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
It is not. However, like std::vector, it is part of the standard library, available by including <algorithm>.Originally Posted by R.Stiltskin
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)