C++/List/list const iterator

Материал из C\C++ эксперт
Версия от 10:27, 25 мая 2010; Admin (обсуждение | вклад) (1 версия: Импорт контента...)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Create list iterator

  
#include <list> 
#include <string> 
#include <iostream> 
using namespace std;
int main() 
{ 
  list<int> list1;
  size_t n = 10; 
  double val = 3.14; 
  list<double> list2(n, val);   
  list<double> list3(list2);    
  cout << "Size of list1 " << list1.size() << endl; 
  cout << "Size of list2 " << list2.size() << endl; 
  cout << "Size of list3 " << list3.size() << endl; 
  // Create list iterator 
  list<double>::const_iterator i; 
  for (i = list2.begin(); i != list2.end(); ++i) 
  { 
    cout << *i << ","; 
  } 
  return 0; 
}


Loop through list using list<char>::const_iterator

  
 
/* 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.
 */
#include <iostream>
#include <list>
using namespace std;
int main()
{
    list<char> coll;      // list container for character elements
    // append elements from "a" to "z"
    for (char c="a"; c<="z"; ++c) {
        coll.push_back(c);
    }
    /* print all elements
     * - iterate over all elements
     */
    list<char>::const_iterator pos;
    for (pos = coll.begin(); pos != coll.end(); ++pos) {
        cout << *pos << " ";
    }
    cout << endl;
}
 /* 
a b c d e f g h i j k l m n o p q r s t u v w x y z
 */