•
•
•
•
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
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?"
// 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; }
Post Comment
•
•
•
•
DaniWeb Marketplace (Sponsored Links)