473,656 Members | 2,983 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem with sort

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<<(ostr eam& out, vector<word>& v)
{
for(int i=0; i<v.size(); ++i)
cout << v[i].letters << endl;
return out;
}
};

int main()
{
vector<word>dic t;
string aux;
cin >> aux;

while(aux!="#")
{
word w(aux);
dict.push_back( w);

cin >> aux;
}

sort(dict.begin (),dict.end(),l t);
cout << dict << endl;

return 0;
}

There's something wrong with the function "lt" but i'm not sure what.
In the prototype it says:
StrictWeakOrder ing cmp but I'm not sure what does that mean, maybe
something about the kind of iterators I need to use?

Thanks!

Jun 14 '06 #1
4 2375
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<<(ostr eam& 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>dic t;
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(),l t);
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:
StrictWeakOrder ing 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

Jun 14 '06 #2
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<<(ostr eam& 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>dic t;
string aux;
cin >> aux;

while(aux!="#")
{
word w(aux);
dict.push_back( w);

cin >> aux;
}

sort(dict.begin (),dict.end(),l t);
cout << dict << endl;

return 0;
}

There's something wrong with the function "lt" but i'm not sure what.
In the prototype it says:
StrictWeakOrder ing 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
Jun 14 '06 #3
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:
StrictWeakOrder ing 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...
Jun 14 '06 #4
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!

Jun 15 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
2575
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 in order to determine the sort order of the DataView. (I'm writing in C# with the v1.1.4322 of the .NET Framework, in Window2K server"). First of all, here are the two methods I have used in order to apply the sorting property to the DataView 1. Simply defining the sort order and colum DataView...
2
1840
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 compares two pointer objects, and use that functor to sort a list of pointers. It seems correct to me, and when I read on these boards I could not find any one complaining about the same problem. But my compiler is giving me error messages. I am hoping that someone can help to point out where my...
4
1905
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 Johan
20
4054
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 Python, use the “sort” method. For example: li=;
3
2474
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 having a little trouble at the moment though! Now, I am trying to calculate, with each sort, how many times during the sort the array elements are compared and swapped.
20
2493
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 sql-file with copy-commands and to one file using insert-statements. after initalizing and starting postgres 7.4 on a different port and datadirectory, i tried to import the sql-dump with the copy statements. this import fails, but importing the dump-file with inserts took a long time but was...
1
1398
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 2003.As it is only a protoype application, my database has been made in MSDE. I've searched all over the web for the answer to this, but can't quite work it out. Basically, I am showing all Timesheet Entries in a DataGrid, by
8
3185
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 the CompareTo function, not one. Test1: I need to know the distance in space from the "current point" to the point sent in. This is what the current IComparable interfeace gives me. Test2 : I also need to know the distance in space from the "previous point" to the point being sent in. HERE is...
10
9212
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 takes really long to return back the records due to which a timeout occurs.If I change the cursorlocation to adUseNone(1) or adUseServer(2) then the search is faster and without any problems.But the sort on records cannot be done if I use adUseClient(3).I need to have sort on these records.
4
1995
pradeepjain
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 sort ...its sorts the result by the value he selects . its working fine... say a user selects price as the sort thing . so it lists all mobiles with its price range with mobile in some range together . wht i need to do here is in the say there are 4 groups with diff price range and in each range...
0
8380
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
8296
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8816
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8710
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8497
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7310
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4299
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1928
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1598
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.