Let's say you worked in the White House and had to keep two lists, one for the friends and one for the enemies. The boss came to you and said: "The Almighty talked to me out of a burning bush last night, telling me that I shall make my enemies my friends! Can you help me?"
Merging two lists (STL)
// merge two STL lists
// using list from the Standard Template Libraries
// a Dev-C++ tested console application
#include <iostream>
#include <list> // list header
#include <iterator> // ostream_iterator
static char friends[6][15]=
{
"Britain","Israel","Italy","Lichtenstein",
"Poland","Upper Volta"
};
static char enemies[8][15]=
{
"France","China","Germany","Spain",
"Canada","Iran","Brazil","Syria"
};
using namespace std;
int main()
{
int k;
list<string> fL;
list<string> eL;
// load the f list
for(int k = 0; k < 6; k++)
{
fL.push_back(friends[k]);
}
cout << "The friends list:\n";
// print out the list one item on a line
copy(fL.begin(),fL.end(),ostream_iterator<string>(cout,"\n"));
// load the e list
for(int k = 0; k < 8; k++)
{
eL.push_back(enemies[k]);
}
cout << "The enemies list:\n";
// print out the list one item on a line
copy(eL.begin(),eL.end(),ostream_iterator<string>(cout,"\n"));
// make 'em all friends, sort 'em to be impartial
// merge() does sort the resulting list, list eL is now empty
fL.merge(eL);
cout << "The new friends list (sorted):\n";
// print out the list one item on a line
copy(fL.begin(),fL.end(),ostream_iterator<string>(cout,"\n"));
cin.get(); // wait
return 0;
}
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.