Vector applications (element traversal, insertion, deletion, switching, etc.), vector

Source: Internet
Author: User

Vector applications (element traversal, insertion, deletion, switching, etc.), vector
Here is a summary of some common applications of vector containers. For the vector constructor and initialization, refer to http://blog.csdn.net/lsh_2013/article/details/21191289. Element Traversal

Use an iterator to access a vector Element

#include <vector>#include <iostream>using namespace std;int main(void){vector<int> v;v.push_back(42);v.push_back(51);v.push_back(69);vector<int>::iterator i,iend;iend=v.end();int j;for(i=v.begin(),j=0; i!=iend; i++,j++)    cout<<"v[" << j << "] = " << *i<< endl;return 0;}

Element insertion

Iterator insert (iteratorpos, const T & x); // insert an element x before the element indicated by the iterator pos

# Include <vector> # include <iostream> using namespace std; int main (void) {vector <int> v; v. push_back (6); v. push_back (7); v. push_back (8); v. push_back (10); v. insert (v. begin () + 3, 9); // insert v before the element. insert (v. begin (), 5); // Insert the first element v. insert (v. end (), 11); // Insert the last element for (int I = 0; I <v. size (); I ++) cout <"v [" <I <"] =" <v [I] <endl; return 0 ;}


Element Deletion

1) iterator erase (iterator pos); // Delete the elements referred to by the iterator pos

2) iterator erase (iterator first, iterator last); // delete all elements in the iteration interval [first, last)

# Include <iostream> # include <vector> using namespace std; int main () {vector <int> myvector; for (int I = 1; I <= 10; I ++) myvector. push_back (I); // Delete the first element myvector. erase (myvector. begin () + 5); // Delete the previous element myvector. erase (myvector. begin (), myvector. begin () + 3); cout <"myvector contains:"; for (unsigned I = 0; I <myvector. size (); ++ I) cout <''<myvector [I]; cout <'\ n'; return 0 ;}


Reverse traversal of Elements
#include <vector>#include <iostream>using namespace std;   int main(void){vector<int> v;v.push_back(1);v.push_back(3);v.push_back(5);v.push_back(7);v.push_back(9);vector<int>::reverse_iterator ri,riend;riend=v.rend();for(ri=v.rbegin();ri!=riend;ri++)cout << *ri << endl;return 0;}


Vector Switching

Void swap (vector & x); // element exchange between two vector containers

# Include <iostream> # include <vector> using namespace std; int main () {vector <int> v1 (2, 10 ); // 10 10 vector <int> v2 (3, 30); // 30 30 30 cout <"v1 contains:"; for (unsigned I = 0; I <v1.size (); I ++) cout <''<v1 [I]; cout <'\ n'; cout <" v2 contains: "; for (unsigned I = 0; I <v2.size (); I ++) cout <''<v2 [I]; cout <'\ n'; v1.swap (v2 ); // switch cout <"After switch" <endl; cout <"v1 contains:"; for (unsigned I = 0; I <v1.size (); I ++) cout <''<v1 [I]; cout <'\ n'; cout <" v2 contains: "; for (unsigned I = 0; I <v2.size (); I ++) cout <''<v2 [I]; cout <'\ n'; return 0 ;}
Other common functions

1) bool empty ();

// Determine whether the vector container is empty. If the container does not have one element, true is returned; otherwise, false is returned.

2) size_type size ();

// The actual number of elements in the current vector container.

3) size_typemax_size ();

// The maximum number of elements allowed by the system in the vector container.

4) size_typecapacity ();

// The number of elements that can accommodate a vector.

5) reference front ()

// The first element (reference) of the vector container. The vector must not be empty.

6) reference back ();

// Returns the last element (reference) of the vector container. The vector must not be empty.

7) void pop_back ();

// Delete a container element at the end.


This is my original, reproduced please indicate the source: http://blog.csdn.net/lsh_2013/article/details/46731331





Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.