C++/Vector/vector iterator

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

Change contents of vector through an iterator

   
 
#include <iostream>
#include <vector>
#include <cctype>
using namespace std;
int main()
{
  vector<char> v(10); // create a vector of length 10
  vector<char>::iterator p; // create an iterator
  int i;
  // assign elements in vector a value
  p = v.begin();
  i = 0;
  while(p != v.end()) {
    *p = i + "a";
    p++;
    i++;
  }
  // display contents of vector
  cout << "Original contents:\n";
  p = v.begin();
  while(p != v.end()) {
    cout << *p << " ";
    p++;
  }
  cout << "\n\n";

  // change contents of vector
  p = v.begin();
  while(p != v.end()) {
    *p = toupper(*p);
    p++;
  }

  // display contents of vector
  cout << "Original contents:\n";
  p = v.begin();
  while(p != v.end()) {
    cout << *p << " ";
    p++;
  }
  cout << "\n\n";
  return 0;
}
/* 
Original contents:
a b c d e f g h i j
Original contents:
A B C D E F G H I J

 */


const_iterator from different containers

  
#include <algorithm>
#include <vector>
#include <list>
#include <iostream>
using namespace std;
int main ()
{
    vector <int> v;
    for (int nNum = -9; nNum < 10; ++ nNum)
        v.push_back (nNum);
    v.push_back (9);
    v.push_back (9);
    list <int> l;
    for (int nNum = -4; nNum < 5; ++ nNum)
        l.push_back (nNum);
    vector <int>::const_iterator vi;
    for ( vi = v.begin (); vi != v.end (); ++ vi )
        cout << *vi << " ";
    list <int>::const_iterator li;
    for ( li = l.begin (); li != l.end (); ++ li )
        cout << *li << " ";
    return 0;
}


Display contents of vector through an iterator

   
 
#include <iostream>
#include <vector>
#include <cctype>
using namespace std;
int main()
{
  vector<char> v(10); // create a vector of length 10
  vector<char>::iterator p; // create an iterator
  int i;
  // assign elements in vector a value
  p = v.begin();
  i = 0;
  while(p != v.end()) {
    *p = i + "a";
    p++;
    i++;
  }
  // display contents of vector
  cout << "Original contents:\n";
  p = v.begin();
  while(p != v.end()) {
    cout << *p << " ";
    p++;
  }
  cout << "\n\n";
  return 0;
}
/* 
Original contents:
a b c d e f g h i j

 */


interator operator for vector

   
#include <vector>
using namespace std;
int main(int argc, char** argv)
{
  vector<int> intVector(10, 0);
  vector<int>::iterator it = intVector.begin();
  it += 5;
  --it;
  *it = 4;
  return (0);
}


Loop through all elements in a vector in reverse order by using rbegn, rend

   
 
/* 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 <vector>
#include <algorithm>
#include <iterator>
using namespace std;
int main()
{
    vector<int> coll;
    // insert elements from 1 to 9
    for (int i=1; i<=9; ++i) {
        coll.push_back(i);
    }
    // print all element in reverse order
    copy (coll.rbegin(), coll.rend(),        // source
          ostream_iterator<int>(cout," "));  // destination
    cout << endl;
}
/* 
9 8 7 6 5 4 3 2 1
 */


Use const_iterator to loop through the vector

   
 
#include <iostream>
using std::cout;
using std::endl;
#include <vector>
using std::vector;
int main()
{
   int array[ 6 ] = { 1, 2, 3, 4, 5, 6 }; // initialize array
   vector< int > integers; // create vector of ints
   integers.push_back( 2 );
   integers.push_back( 3 );
   integers.push_back( 4 );
   vector< int >::const_iterator constIterator;
   // display vector elements using const_iterator
   for ( constIterator = integers.begin();
      constIterator != integers.end(); ++constIterator )
      cout << *constIterator << " ";
   cout << endl;
   return 0;
}
/* 
2 3 4
 */


vector.begin, vector.end returns the iterators

   
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main(void)
{
    const int VECTOR_SIZE = 8 ;
    typedef vector<int> IntVector ;
    typedef IntVector::iterator IntVectorIt ;
    IntVector Numbers(VECTOR_SIZE) ;
    IntVectorIt start, end, it ;
    Numbers[0] = 4;
    Numbers[1] = 1;
    Numbers[2] = 7;
    Numbers[3] = 3;
    Numbers[4] = 1;
    Numbers[5] = 6;
    Numbers[6] = 9;
    Numbers[7] = 10;
    start = Numbers.begin() ;   
    end = Numbers.end() ;       
    for(it = start; it != end; it++)
        cout << *it << " " ;
    random_shuffle(start, end) ;
    for(it = start; it != end; it++)
        cout << *it << " " ;
}