Member Avatar

Hi, I am relatively new to C++ and I am having problems sorting a set of items here is my code:

using namespace std;

struct SimpsonChars
	string firstname,lastname;
	double firstnum,secondnum,thirdnum,fourthnum,fifthnum;
	SimpsonChars(const string& lname = "", const string& fname = "", double first = 0, double second = 0, double third = 0, 
					double fourth = 0, double fifth = 0, double sixth = 0):lastname(lname),firstname(fname),firstnum(first),

bool operator <(const SimpsonChars& sc1, const SimpsonChars& sc2) { return sc1.lastname < sc2.lastname; }

int main(int argc, char * argv[])
	multiset<SimpsonChars> sc;
	vector<string> v;

	string firstname,lastname;
	string Simpson;
	string value;
	double first,second,third,fourth,fifth;
	while(getline(cin, Simpson))
	vector<string> v2(v);
	//replaces all the commas with spaces
	for(vector<string>::size_type i = 0; i < v2.size(); i++)
		for(vector<string>::size_type j = 0; j < v2[i].size(); j++)
			if(v2[i][j] == ',')
				v2[i][j] = ' ';

	//inserts contents of the text file into a set
	for(vector<string>::size_type i = 0; i < v2.size(); i++)
		istringstream iss(v2[i]);
		if(iss>>lastname && iss>>firstname && iss>>first && iss>>second && iss>>third && iss>>fourth && iss>>fifth)
	//prints out the set
	for(set<SimpsonChars>::iterator it = sc.begin(); it != sc.end(); ++it)
		cout<<endl<<it->lastname<<" "<<it->firstname<<" "<<it->firstnum<<" "<<it->secondnum<<" "<<it->thirdnum<<" "<<it->fourthnum<<" "<<it->fifthnum<<endl;
	//sort(sc.begin(), sc.end(),operator<());//tried calling sort like this, but got                                  a huge error

My program is suppose to read a text file using I/O Redirection containing names of Simpsons characters and 5 numbers following it. The format of the text file is something like


I managed to get each individual item into a set, but I am having trouble sorting it. I have tried putting my sort function at the top of my program into struct and passing it into the set like so,

struct cmp
	bool operator()(const SimpsonChars& sc1, const SimpsonChars& sc2) { return sc1.lastname < sc2.lastname; }

set<SimpsonChars,cmp> sc;

but it gives me a huge error. I can't see what I am doing wrong here, any help would be appreciated.

Recommended Answers

All 3 Replies

What is the text of the error?

You can just separate your compare function from your struct. For example :

struct Point
    int x,y;

bool myobject(Point x1,Point x2)
    return (x1.x<x2.x);

Just remember to put struct before compare function

Member Avatar

What is the text of the error?

i get the following error:

In file included from /usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/string:50:0,

                 from /usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/bits/locale_
                 from /usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/bits/ios_bas
                 from /usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/ios:43,
                 from /usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/ostream:40,
                 from /usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/iostream:40,

                 from a1.cpp:1:
/usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/bits/stl_function.h: In member fun
ction 'bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp =
/usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/bits/stl_tree.h:1211:4:   instanti
ated from 'std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator st
d::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_equal(const _V
al&) [with _Key = SimpsonChars, _Val = SimpsonChars, _KeyOfValue = std::_Identit
y<SimpsonChars>, _Compare = std::less<SimpsonChars>, _Alloc = std::allocator<Sim
psonChars>, std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator =
/usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/bits/stl_multiset.h:397:40:   inst
antiated from 'std::multiset<_Key, _Compare, _Alloc>::iterator std::multiset<_Ke
y, _Compare, _Alloc>::insert(const value_type&) [with _Key = SimpsonChars, _Comp
are = std::less<SimpsonChars>, _Alloc = std::allocator<SimpsonChars>, std::multi
set<_Key, _Compare, _Alloc>::iterator = std::_Rb_tree_const_iterator<SimpsonChar
s>, value_type = SimpsonChars]'
a1.cpp:58:78:   instantiated from here
/usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/bits/stl_function.h:230:22: error:
 no match for 'operator<' in '__x < __y'
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.