Not really, since it is part of the standard library, not the core language, but admittedly that is being a little pedantic.Originally Posted by tabstop
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
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
I need to order if first before using lower_bound(). I need to order it by the string word.
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
So this should work right?
Wow..this actually sorts the words characters haha. So the string adventure would be adeenrstuv...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()); } }
Last edited by Todd88; 12-05-2008 at 12:49 PM.
I have not read through this thread carefully, but at a glance I suspect you want to write it like this instead:Originally Posted by Todd88
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.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); } }
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
hmm... but if you want to sort the data vector, then I would expect something like:Originally Posted by Todd88
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.Code:sort(data.begin(), data.end());
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
I overloaded the operator < using:
It's still telling me that there is no match for operator <Code:bool indexWord::operator< (indexWord& string) { return value < string.value; }
It needs to be const correct:
Code:bool indexWord::operator< (const indexWord& string) const { return value < string.value; }
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
and the binary_search algorithm should work too correct? It does work for me though.
right?Code:binary_search (data.begin(), data.end(), word)