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 397,695 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,526 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:
Nov 28th, 2004
Views: 13,880
Using the C++ Standard Template Libraries (STL) can be easy, once you know how to do it. No need to putz around with doubly linked lists anymore! Here is code showing how a STL list allows you to add, insert, remove, sort, splice, merge, display, and clean-out-duplicate strings.
cplusplus Syntax | 4 stars
  1. // manipulate strings in a list
  2. // using list from the Standard Template Libraries
  3. // list is just a very generic doubly linked list and
  4. // therefore allows for fast insertions and deletions
  5. // a Dev-C++ tested Console Application by vegaseat 28nov2004
  6.  
  7. #include <iostream>
  8. #include <list> // stl header
  9. #include <iterator> // ostream_iterator
  10.  
  11. static char names[8][10]=
  12. {
  13. "Heidi", "Bertha", "Samantha", "Rubin",
  14. "Frank", "Sandy", "Sunny", "Rubin"
  15. };
  16.  
  17. static char norge[6][10]=
  18. {
  19. "Jens", "Hildegard", "Bjorn", "Ulla", "Joerg", "Mila"
  20. };
  21.  
  22. using namespace std; // std::cout
  23.  
  24. int main()
  25. {
  26. int k;
  27. list<string> sL;
  28.  
  29. // load the list
  30. // push_back appends strings to the end of a list
  31. for(int k = 0; k < 8; k++)
  32. {
  33. sL.push_back(names[k]);
  34. }
  35. cout << "contents of the list in order:\n";
  36. // print out the list one item on a line
  37. copy(sL.begin(),sL.end(),ostream_iterator<string>(cout,"\n"));
  38.  
  39. // sort the list
  40. sL.sort();
  41. cout << "sort the list:" << endl;
  42. copy(sL.begin(),sL.end(),ostream_iterator<string>(cout,"\n"));
  43.  
  44. // remove all instances of Frank from the list
  45. sL.remove("Frank");
  46. cout << "remove Frank from the list:\n";
  47. copy(sL.begin(),sL.end(),ostream_iterator<string>(cout,"\n"));
  48.  
  49. // add Karl to the beginning of the list
  50. sL.push_front("Karl");
  51. cout << "add Karl to the beginning of the list:\n";
  52. copy(sL.begin(),sL.end(),ostream_iterator<string>(cout,"\n"));
  53.  
  54. // add Zoe as the third item in the list
  55. list<string>::iterator iter1; // pointer to list items
  56. iter1 = sL.begin(); // point to first item in list
  57. iter1++; // point to second item
  58. iter1++; // point to third item
  59. sL.insert(iter1,1,"Zoe"); // insert Zoe one time
  60. cout << "insert Zoe as third item in list:" << endl;
  61. copy(sL.begin(),sL.end(),ostream_iterator<string>(cout,"\n"));
  62.  
  63. // remove duplicate items
  64. sL.unique();
  65. sL.sort(); // sort again
  66. cout << "remove any duplicates and sort again:" << endl;
  67. copy(sL.begin(),sL.end(),ostream_iterator<string>(cout,"\n"));
  68.  
  69. // this shows a different way to initialize a list with an array
  70. list<string> nL(norge, norge+6);
  71.  
  72. // you made some friends on your last trip to Norway
  73. // and want to append their names to your present list
  74. // splice() merges the two lists without sort
  75. // you can use sL.merge(nL) to merge the two lists with sort
  76. sL.splice(sL.end(), nL);
  77. cout << "splice norge list to end of name list:" << endl;
  78. copy(sL.begin(),sL.end(),ostream_iterator<string>(cout,"\n"));
  79.  
  80. cin.get(); // wait
  81. return 0;
  82. }
  83.  
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:19 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC