"Kees Hoogendijk" <ni********@haalditweg-hcopleidingen.nl> wrote in message
news:bp**********@news.cistron.nl...
/*
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::ostream& operator<<(std::ostream& 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::ostream_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