I'm not quite sure why this code doesn't works:
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
class word
{
string letters;
string sorted;
public:
word(string&s)
{
letters=s;
sort(s.begin(),s.end());
sorted=s;
}
friend bool lt(word& a, word& b)
{
return (a.sorted < b.sorted) < 0 ? true: false;
}
friend ostream& operator<<(ostream& out, vector<word>& v)
{
for(int i=0; i<v.size(); ++i)
cout << v[i].letters << endl;
return out;
}
};
int main()
{
vector<word>dict;
string aux;
cin >> aux;
while(aux!="#")
{
word w(aux);
dict.push_back(w);
cin >> aux;
}
sort(dict.begin(),dict.end(),lt);
cout << dict << endl;
return 0;
}
There's something wrong with the function "lt" but i'm not sure what.
In the prototype it says:
StrictWeakOrdering cmp but I'm not sure what does that mean, maybe
something about the kind of iterators I need to use?
Thanks! 4 2265
Gaijinco wrote: I'm not quite sure why this code doesn't works:
#include <iostream> #include <algorithm> #include <vector> #include <string> using namespace std;
class word { string letters; string sorted; public: word(string&s)
You should probably use a const reference here. Compare: http://www.parashift.com/c++-faq-lit....html#faq-18.6
{ letters=s;
Use initialization lists. See this FAQ: http://www.parashift.com/c++-faq-lit....html#faq-10.6
sort(s.begin(),s.end()); sorted=s;
If you switch to a const reference (which you probably should), you'll
need to copy the unsorted parameter into "sorted" first, and then sort
it. But consider what this does: it sorts the letters within a word. So
"august" becomes "agstuu", which you then use in your compare function.
That's probably not what you want to do.
} friend bool lt(word& a, word& b) { return (a.sorted < b.sorted) < 0 ? true: false;
When comparing strings, you could just do:
return a.letter < b.letter;
The other business there is unecessary and almost certainly wrong. The
< operator returns a bool, so checking if it is less than zero is
non-sensical. It's either false (zero) or true (not zero).
} friend ostream& operator<<(ostream& out, vector<word>& v) { for(int i=0; i<v.size(); ++i) cout << v[i].letters << endl; return out; }
First, you output to "cout" but then return "out", which you didn't
actually use or modify. Second, it is more customary to provide a
friend output function just for your class and then provide another
non-friend function to handle vectors of any type. In that case, your
prototype for this function would be:
friend ostream& operator<<( ostream& out, const word& w );
and the prototype for the non-friend would be:
template<class T>
ostream& operator<<( ostream& out, const vector<T>& v );
Note the const reference for v, which you don't modify.
};
int main() { vector<word>dict; string aux; cin >> aux;
while(aux!="#") { word w(aux); dict.push_back(w);
cin >> aux; }
This is the wrong way to use cin. See the example in this FAQ: http://www.parashift.com/c++-faq-lit....html#faq-15.5 sort(dict.begin(),dict.end(),lt); cout << dict << endl;
return 0; }
There's something wrong with the function "lt" but i'm not sure what.
See above. The bottom line here is that you don't even need the word
class (unless you really do want to sort by "agstuu" instead of
"august"). Just make a vector of strings and sort that with the default
comparison function.
In the prototype it says: StrictWeakOrdering cmp but I'm not sure what does that mean, maybe something about the kind of iterators I need to use?
No. See this definition: http://www.sgi.com/tech/stl/StrictWeakOrdering.html
Cheers! --M
Gaijinco wrote: I'm not quite sure why this code doesn't works:
#include <iostream> #include <algorithm> #include <vector> #include <string> using namespace std;
class word { string letters; string sorted; public: word(string&s) { letters=s; sort(s.begin(),s.end()); sorted=s; }
Are you sure, you want this constructor to have the side-effect of sorting
the parameter passed? What about:
word ( string const & s )
: letters ( s )
, sorted ( s )
{
sort( sorted.begin(), sorted.end() );
}
friend bool lt(word& a, word& b) { return (a.sorted < b.sorted) < 0 ? true: false;
Huh? operator< returns a bool, it is not a three-way comparison yielding an
int. What about:
return ( a.sorted < b.sorted );
} friend ostream& operator<<(ostream& out, vector<word>& v)
This should be:
friend ostream& operator<< ( ostream& out, vector<word> const & v )
unless you want the output operator to modify its argument.
{ for(int i=0; i<v.size(); ++i) cout << v[i].letters << endl; return out; } };
int main() { vector<word>dict; string aux; cin >> aux;
while(aux!="#") { word w(aux); dict.push_back(w);
cin >> aux; }
sort(dict.begin(),dict.end(),lt); cout << dict << endl;
return 0; }
There's something wrong with the function "lt" but i'm not sure what. In the prototype it says: StrictWeakOrdering cmp but I'm not sure what does that mean, maybe something about the kind of iterators I need to use?
Soring a dictionary according to the lexicographic order of internally
rearranged words seems to be a rather strange requirement. Are you sure,
your want that?
Best
Kai-Uwe Bux
On Wed, 14 Jun 2006 05:31:54 -0700, Gaijinco wrote: I'm not quite sure why this code doesn't works:
[snip other reasonable code]
friend bool lt(word& a, word& b) { return (a.sorted < b.sorted) < 0 ? true: false; }
What's the "< 0" doing in that statement? Why isn't it just:
return a.sorted < b.sorted;
? Operator< returns a bool. As it is now, the result of operator< will
never be less than 0, so lt will always return false.
[snip other reasonable code]
There's something wrong with the function "lt" but i'm not sure what. In the prototype it says: StrictWeakOrdering cmp but I'm not sure what does that mean, maybe something about the kind of iterators I need to use?
Strict Weak Ordering. Your function needs to provide "less-than"
semantics (which yours attempts to do). Google for it, you'll find a
definition much better than I can give.
Although you are sorting your strings in a rather novel way... (sorting
the letters in the word first, and comparing that to order the words...
A little explanation may clarify somethings:
The code was to solve a problem from acm.uva.es called Ananagrams. The
problem stated that given a text you were to find the words that didn't
have anagrams within the text (called ananagrams in the problem)
That's why there was comparations between the sorted strings, to know
if there were anagrams.
Thank you all for your opinions, they were really helpful and
insightful! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: jwedel_stolo |
last post by:
Hi
I'm creating a dataview "on the fly" in order to sort some data prior to writing out the information to a MS SQL table
I have used two methods...
|
by: Joel |
last post by:
I am having some problems compiling my code on Mandrake 10 with g++
(GCC 3.3.2). The problem seems to be in that I try to define a functor
that...
|
by: Johan |
last post by:
Hi,
Why does my vector not sort. What I understand is you have to overload
the < operator, but that does not work.
see code below
Thanks
...
|
by: Xah Lee |
last post by:
Sort a List
Xah Lee, 200510
In this page, we show how to sort a list in Python & Perl and also
discuss some math of sort.
To sort a list in...
|
by: ritchie |
last post by:
Hi all!
Still working on this program!
Just to recap, I am writing a program to sort an array with four
different sort algorythms.
I am...
|
by: Development - multi.art.studio |
last post by:
Hello everyone,
i just upgraded my old postgres-database from version 7.1 to 7.4.2.
i dumped out my 7.1 database (with pg_dump from 7.1) as an...
|
by: thebison |
last post by:
Hi all,
I hope someone can help with this relatively simple problem.
I am building a timesheet application using ASP.NET C# with Visual
Studio...
|
by: SimeonArgus |
last post by:
I need to sort a list of points, so I've considered using an
IComparable implementation. Should be easy, right? But I need to know
two things in...
|
by: shubha.sunkada |
last post by:
Hi,
I have a recordset connection in asp that I am using to search
records.If I use the client side cursorlocation (rs.cursorlocation=3)
then it...
|
by: pradeepjain |
last post by:
i had posted the same code in javascript area !! this i am posting bcos of different problem. as you can see in ma code there is a dropdown called...
|
by: concettolabs |
last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
|
by: better678 |
last post by:
Question:
Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct?
Answer:
Java is an object-oriented...
|
by: teenabhardwaj |
last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
|
by: CD Tom |
last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
|
by: jalbright99669 |
last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was...
|
by: Arjunsri |
last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
| |