sorting classes read from a file

Hi can anyone help? Can i get an example of sorting classes in ascending order read from a file without using vector? I know I'm supposed to use a copy constructor but I'm not sure how to do this. Please help?
Copy constructors and sorting are two independent things.

1
2
3
4
5
6
7
8
struct Foo {
   Foo( const Foo& f ) :
      field1( f.field1 ), field2( f.field2 ) 
   {}

   int field1;
   char field2;
};


is an example of a copy constructor although in this case the default copy constructor provided by the compiler would suffice without writing the code.


What kind of sorting algorithm must you use?

std::sort() works on any container that implements a random access iterator (which includes vectors, deques, and arrays).

For sorting, you need a comparison algorithm, and this depends on the class being sorted.

An example of a class with a copy constructor:
1
2
3
4
5
6
7
8
9
class A{
	int d;
	//Copy constructor.
	//Given a class T, the copy constructor always has the prototype
	//T(const T &)
	A(const A &original){
		this->d=original.d;
	}
};
this is my code for my sort function
//function to sort my database of favorite celebs by the last name

void sort(con favcelebs[], int n)
{
string temp;

for(int i=0; i<n-1; i++)
for(int cand=i+1;cand<n; cand++)
if(favcelebs[i].name.last<favcelebs[cand].name.last){
temp=favcelebs[i].name.last;
favcelebs[i].name.last=favcelebs[cand].name.last;
favcelebs[cand].name.last=temp;}
return;
}

i know i did this wrong. My professor said to use a feature in c++ that lets you copy one entire object to another object in one statement.
also this is the text that i'm reading:

Pitt Brad M Blonde 40 Actor
Jolie Angelina F Black 33 Actress
Smith William M Black 37 Actor
Washignton Denzel M Black 45 Actor
Angelou Mya F Black 70 Poet
Last edited on
Topic archived. No new replies allowed.