User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 402,626 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,216 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Dec 1st, 2004
Views: 4,007
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?"
cplusplus Syntax | 5 stars
  1. // merge two STL lists
  2. // using list from the Standard Template Libraries
  3. // a Dev-C++ tested console application
  4.  
  5. #include <iostream>
  6. #include <list> // list header
  7. #include <iterator> // ostream_iterator
  8.  
  9. static char friends[6][15]=
  10. {
  11. "Britain","Israel","Italy","Lichtenstein",
  12. "Poland","Upper Volta"
  13. };
  14.  
  15. static char enemies[8][15]=
  16. {
  17. "France","China","Germany","Spain",
  18. "Canada","Iran","Brazil","Syria"
  19. };
  20.  
  21. using namespace std;
  22.  
  23. int main()
  24. {
  25. int k;
  26. list<string> fL;
  27. list<string> eL;
  28.  
  29. // load the f list
  30. for(int k = 0; k < 6; k++)
  31. {
  32. fL.push_back(friends[k]);
  33. }
  34. cout << "The friends list:\n";
  35. // print out the list one item on a line
  36. copy(fL.begin(),fL.end(),ostream_iterator<string>(cout,"\n"));
  37.  
  38. // load the e list
  39. for(int k = 0; k < 8; k++)
  40. {
  41. eL.push_back(enemies[k]);
  42. }
  43. cout << "The enemies list:\n";
  44. // print out the list one item on a line
  45. copy(eL.begin(),eL.end(),ostream_iterator<string>(cout,"\n"));
  46.  
  47. // make 'em all friends, sort 'em to be impartial
  48. // merge() does sort the resulting list, list eL is now empty
  49. fL.merge(eL);
  50. cout << "The new friends list (sorted):\n";
  51. // print out the list one item on a line
  52. copy(fL.begin(),fL.end(),ostream_iterator<string>(cout,"\n"));
  53.  
  54. cin.get(); // wait
  55. return 0;
  56. }
  57.  
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 1:44 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC