order by

K

Kees Hoogendijk

/*

Hi, everyone,

I've got a problem with sort (just dont know how ;-().

I have here 2 lists of names; both have been sorted on alphabetical (type in
the prompt). These 2 lists must be added and to be sorted again.

Can someone help me?

TIA

W&

*/

# include <string>

# include <iostream>

#include <vector>

using namespace std;




struct persoon

{

string naam;

};

void print(const persoon& p);



int main()

{ const int AANTAL =3;

vector <persoon> plijst1, plijst2, plijst3; /*vector decl*/

persoon p;

for ( int i=0; i< AANTAL; i++)

{

cout<<"Naam: ";

getline(cin, p.naam);

plijst1.push_back(p);

}

cout<<"Geeft nog een groep namen erin: "<<endl;

for ( int i=0; i< AANTAL; i++)

{

cout<<"Naam: ";

getline(cin, p.naam);

plijst2.push_back(p);

}



/*the third list must be here.

plijst3.push_back(p);

*/

vector <persoon>::iterator pos, einde = plijst3.end();

for (pos=plijst3.begin();pos!=plijst3.einde;++pos)

print(*pos);

cin.get();

}



void print(const persoon& p)

{

cout<< p.naam<<endl;

}
 
M

Mike Wahler

Kees Hoogendijk said:
/*

Hi, everyone,

I've got a problem with sort (just dont know how ;-().

I have here 2 lists of names; both have been sorted on alphabetical (type in
the prompt). These 2 lists must be added and to be sorted again.

Can someone help me?

TIA

W&

*/

# include <string>

# include <iostream>

#include <vector>

using namespace std;




struct persoon

{

string naam;

};

void print(const persoon& p);



int main()

{ const int AANTAL =3;

vector <persoon> plijst1, plijst2, plijst3; /*vector decl*/

persoon p;

for ( int i=0; i< AANTAL; i++)

{

cout<<"Naam: ";

getline(cin, p.naam);

plijst1.push_back(p);

}

cout<<"Geeft nog een groep namen erin: "<<endl;

for ( int i=0; i< AANTAL; i++)

{

cout<<"Naam: ";

getline(cin, p.naam);

plijst2.push_back(p);

}



/*the third list must be here.

plijst3.push_back(p);

*/

vector <persoon>::iterator pos, einde = plijst3.end();

for (pos=plijst3.begin();pos!=plijst3.einde;++pos)

print(*pos);

cin.get();

}



void print(const persoon& p)

{

cout<< p.naam<<endl;

}

I find your query rather vague, perhaps this example might help:

#include <algorithm>
#include <iostream>
#include <iterator>
#include <ostream>
#include <string>
#include <vector>

/* a simple 'person' type */
class person
{
std::string m_name;

public:
person(const std::string& n) : m_name (n)
{ }

/* specifies ordering for sort */
bool operator<(const person& rhs)
{
return m_name < rhs.m_name;
}

const std::string& name() const
{
return m_name;
}
};

/* stream inserter for type 'person' object */
std::eek:stream& operator<<(std::eek:stream& os, const person& p)
{
return os << p.name();
}

/* display contents of vector of 'person' objects,
with optional 'prefix' text */
void show(const std::vector<person>& data,
const std::string& prefix = "")
{
std::cout << prefix;

std::copy(data.begin(), data.end(),
std::eek:stream_iterator<person>(std::cout, "\n"));

std::cout << '\n';
}

int main()
{
/* create and populate vector of 'person' objects */
std::vector<person> people1;
people1.push_back(person("Tom"));
people1.push_back(person("Dick"));
people1.push_back(person("Harry"));

/* sort the 'people1' vector */
std::sort(people1.begin(), people1.end());

/* create and populate a second vector of 'person' objects */
std::vector<person> people2;
people2.push_back(person("John"));
people2.push_back(person("Paul"));
people2.push_back(person("George"));
people2.push_back(person("Ringo"));

/* sort the 'people2' vector */
std::sort(people2.begin(), people2.end());

/* display contents of 'people1' and 'people2' vectors */
show(people1, "people1:\n");
show(people2, "people2:\n");

/* create a third (empty) vector of 'person' objects */
std::vector<person> people3;

/* copy contents of vector 'people1' into vector 'people3' */
std::copy(people1.begin(), people1.end(),
std::back_inserter(people3));

/* copy contents of vector 'people2' into vector 'people3' */
std::copy(people2.begin(), people2.end(),
std::back_inserter(people3));

/* now 'people3' vector contains copies of all 'person' objects
from 'people1' and 'people2' vectors */

/* sort the 'combined' vector, 'people3' */
std::sort(people3.begin(), people3.end());

/* display 'people3' vector contents */
show(people3, "people3:\n");

return 0;
}


Output:

people1:
Dick
Harry
Tom

people2:
George
John
Paul
Ringo

people3:
Dick
George
Harry
John
Paul
Ringo
Tom

-Mike
 
S

Stuart Golodetz

Kees Hoogendijk said:
/*

Hi, everyone,

I've got a problem with sort (just dont know how ;-().

I have here 2 lists of names; both have been sorted on alphabetical (type in
the prompt). These 2 lists must be added and to be sorted again.

Hint: Find out about merge sort (try a Google search). In merge sort, you
recursively split the list into two, sort each half, and then merge the
results back together. In this instance, you've got two sorted "halves", all
you have to do now is merge them. There's no point doing more work than you
need to, after all, and you've already sorted each half, so you can take
advantage of that.

HTH,

Stuart.
 
K

Kees Hoogendijk

Hi Stuart,
Thanks for your hint.
Mike's example was very clear. After I'd made some changes, my program also
works.

Best Regards,
W&
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,147
Messages
2,570,834
Members
47,382
Latest member
MichaleStr

Latest Threads

Top