473,386 Members | 1,621 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

order by

/*

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;

}
Jul 22 '05 #1
5 1311

"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

Jul 22 '05 #2

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:yE******************@newsread1.news.pas.earth link.net...
bool operator<(const person& rhs)
This should really be a const function:

bool operator<(const person& rhs) const
{
return m_name < rhs.m_name;
}


-Mike
Jul 22 '05 #3
Wen
Mike,
It works! I must go study your code's.
Thank you very much!!
Wen

"Mike Wahler" <mk******@mkwahler.net> schreef in bericht
news:hI*******************@newsread1.news.pas.eart hlink.net...

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:yE******************@newsread1.news.pas.earth link.net...
bool operator<(const person& rhs)


This should really be a const function:

bool operator<(const person& rhs) const
{
return m_name < rhs.m_name;
}


-Mike

Jul 22 '05 #4
"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.
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.
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;

}

Jul 22 '05 #5
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&

"Stuart Golodetz" <st*************@new.ox.ac.uk> schreef in bericht
news:bp**********@news.ox.ac.uk...
"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.
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.
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;

}


Jul 22 '05 #6

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

Similar topics

7
by: svilen | last post by:
hello again. i'm now into using python instead of another language(s) for describing structures of data, including names, structure, type-checks, conversions, value-validations, metadata etc....
9
by: Steven T. Hatton | last post by:
The following works: template <typename T> struct ID3M{ static const T ID; }; template <typename T> const T ID3M<T>::ID = {{1,0,0},{0,1,0},{0,0,1}};
15
by: | last post by:
The data file is a simple Unicode file with lines of text. BCP apparently doesn't guarantee this ordering, and neither does the import tool. I want to be able to load the data either sequentially...
27
by: Abdullah Kauchali | last post by:
Hi folks, Can one rely on the order of keys inserted into an associative Javascript array? For example: var o = new Object(); o = "Adam"; o = "Eve";
8
by: kaosyeti | last post by:
i have a (hopefully) small problem. i have created a system where a user enters customer information into a table through a form. this table has no primary key. there are 9 fields on the form to...
104
by: Beowulf | last post by:
I have the view below and if I use vwRouteReference as the rowsource for a combo box in an MS Access form or run "SELECT * FROM vwRouteReference" in SQL Query Analyzer, the rows don't come through...
13
by: bevanward | last post by:
Hi All I am finding unexpected results when inserted into a newly created table that has a field of datatype int identity (1,1). Basically the order I sort on when inserting into the table is...
3
by: Hartmut Dippon | last post by:
Hi all, I hope somebody can help me with following problem: I have an application where I can drag&drop files/dirs from within explorer onto my form. If multiple files/dirs are selected I...
54
by: Rasjid | last post by:
Hello, I have just joined and this is my first post. I have never been able to resolve the issue of order of evaluation in C/C++ and the related issue of precedence of operators, use of...
25
by: DanicaDear | last post by:
Hello again Bytes...I missed you! First, background: In a hotstick lab, we ship orders every two years. We ship a new order and the customer uses the new box to return the previous year's order....
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...

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.