C++/List/list iterator

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Loop through list back and forth

<source lang="cpp">

/* The following code example is taken from the book

* "The C++ Standard Library - A Tutorial and Reference"
* by Nicolai M. Josuttis, Addison-Wesley, 1999
*
* (C) Copyright Nicolai M. Josuttis 1999.
* Permission to copy, use, modify, sell and distribute this software
* is granted provided this copyright notice appears in all copies.
* This software is provided "as is" without express or implied
* warranty, and with no claim as to its suitability for any purpose.
*/
  1. include <iostream>
  2. include <list>
  3. include <algorithm>

using namespace std; void print (int elem) {

   cout << elem << " ";

} int main() {

   list<int> coll;
   // insert elements from 1 to 9
   for (int i=1; i<=9; ++i) {
       coll.push_back(i);
   }
   // print all elements in normal order
   for_each (coll.begin(), coll.end(),      // range
             print);                        // operation
   cout << endl;
   // print all elements in reverse order
   for_each (coll.rbegin(), coll.rend(),    // range
             print);                        // operations
   cout << endl;

} /* 1 2 3 4 5 6 7 8 9 9 8 7 6 5 4 3 2 1

*/
       
   
 </source>


Move list iterator using ++

<source lang="cpp">

  1. include <iostream>
  2. include <cassert>
  3. include <list>
  4. include <algorithm> // for find

using namespace std; int main() {

 char x[5] = {"a", "r", "e", "q", "t"};
 list<char> list1(&x[0], &x[5]);
 // Search for the first occurrence of the letter e:
 list<char>::iterator where = find(list1.begin(), list1.end(), "e");
 list<char>::iterator next = where;
 ++next;
 cout << *next << endl;
 return 0;

}

/* 

q

*/       
   
 </source>


Traverse a List Using an Iterator

<source lang="cpp">

  1. include <iostream>
  2. include <list>

using namespace std; typedef list<int> IntegerList; int main() {

      IntegerList intList;
      for (int i = 1; i <= 10; ++i)
              intList.push_back(i * 2);
      for (IntegerList::const_iterator ci = intList.begin(); ci != intList.end(); ++ci)
              cout << *ci << " ";
      return 0;

}


 </source>


Use iterator to change all elements in a list

<source lang="cpp">

  1. include <iostream>
  2. include <list>

using namespace std; int main() {

 list<int> lst; // create an empty list
 int i;
 for(i=0; i<10; i++) lst.push_back(i);
 cout << "Size = " << lst.size() << endl;
 cout << "Contents: ";
 list<int>::iterator p = lst.begin();
 while(p != lst.end()) {
   cout << *p << " ";
   p++;
 }
 cout << "\n\n";
 // change contents of list
 p = lst.begin();
 while(p != lst.end()) {
   *p = *p + 100;
   p++;
 }
 cout << "Contents modified: ";
 p = lst.begin();
 while(p != lst.end()) {
   cout << *p << " ";
   p++;
 }
 return 0;

} /* Size = 10 Contents: 0 1 2 3 4 5 6 7 8 9 Contents modified: 100 101 102 103 104 105 106 107 108 109

*/
       
   
 </source>


Use iterator to loop through all elements in a list

<source lang="cpp">

  1. include <iostream>
  2. include <list>

using namespace std; int main() {

 list<int> lst; // create an empty list
 int i;
 for(i=0; i<10; i++) lst.push_back(i);
 cout << "Size = " << lst.size() << endl;
 cout << "Contents: ";
 list<int>::iterator p = lst.begin();
 while(p != lst.end()) {
   cout << *p << " ";
   p++;
 }
 cout << "\n\n";
 return 0;

}

/* 

Size = 10 Contents: 0 1 2 3 4 5 6 7 8 9

*/       
   
 </source>


Use reverse_iterator and iterator with list

<source lang="cpp">

/* The following code example is taken from the book

* "The C++ Standard Library - A Tutorial and Reference"
* by Nicolai M. Josuttis, Addison-Wesley, 1999
*
* (C) Copyright Nicolai M. Josuttis 1999.
* Permission to copy, use, modify, sell and distribute this software
* is granted provided this copyright notice appears in all copies.
* This software is provided "as is" without express or implied
* warranty, and with no claim as to its suitability for any purpose.
*/
  1. include <iostream>
  2. include <list>
  3. include <algorithm>

using namespace std; int main() {

   list<int> coll;
   // insert elements from 1 to 9
   for (int i=1; i<=9; ++i) {
       coll.push_back(i);
   }
   // find position of element with value 5
   list<int>::iterator pos;
   pos = find (coll.begin(), coll.end(),    // range
               5);                          // value
   // print value of the element
   cout << "pos:   " << *pos << endl;
   // convert iterator to reverse iterator
   list<int>::reverse_iterator rpos(pos);
   // print value of the element to which the reverse iterator refers
   cout << "rpos:  " << *rpos << endl;
   // convert reverse iterator back to normal iterator
   list<int>::iterator rrpos;
   rrpos = rpos.base();
   // print value of the element to which the normal iterator refers
   cout << "rrpos: " << *rrpos << endl;

}

/* 

pos: 5 rpos: 4 rrpos: 5

*/       
   
 </source>