C++/Vector/vector iterator
Содержание
- 1 Change contents of vector through an iterator
- 2 const_iterator from different containers
- 3 Display contents of vector through an iterator
- 4 interator operator for vector
- 5 Loop through all elements in a vector in reverse order by using rbegn, rend
- 6 Use const_iterator to loop through the vector
- 7 vector.begin, vector.end returns the iterators
Change contents of vector through an iterator
<source lang="cpp">
- 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
*/ </source>
const_iterator from different containers
<source lang="cpp">
- 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;
}
</source>
Display contents of vector through an iterator
<source lang="cpp">
- 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
*/ </source>
interator operator for vector
<source lang="cpp">
- 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);
}
</source>
Loop through all elements in a vector in reverse order by using rbegn, rend
<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. */
- 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
*/ </source>
Use const_iterator to loop through the vector
<source lang="cpp">
- 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
*/ </source>
vector.begin, vector.end returns the iterators
<source lang="cpp">
- 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 << " " ;
}
</source>