Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector> // we should include vector before using it.
- using namespace std;
- int main()
- {
- std::vector<int> myVector;
- for ( int i = 0 ; i < 10 ; i++ )
- {
- myVector.push_back( i );
- }
- cout << "Size : " << myVector.size() << endl; // print the size of the vector
- // if we want to delete the 1st element
- // erase() deletes the element after the given iterator, in this case 0th element.
- // please note begin() does NOT return the 0th element, rather the dummy element before the 0th element => 0 in this case.
- myVector.erase( myVector.begin() );
- cout << "Size after deletion: " << myVector.size() << endl << endl;
- // deteles 3 elements from begin() => 1, 2, 3 will be deleted.
- myVector.erase( myVector.begin(), myVector.begin() + 3 );
- // find and delete
- std::vector<int>::iterator ele6 = myVector.find(6);
- // if the given value is not there, iterator will reach the end
- if ( ele6 != myVector.end() )
- {
- myVector.erase(ele6);
- }
- std::vector<int>::iterator itr = myVector.begin();
- // iterate untin the iterator hits the end of the container
- for ( ; itr != myVector.end(); itr++ )
- {
- int val = *itr; // * dereferences the iterator and returns the actual value.
- cout << "Value : " << val << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement