Usage of vector Array

Usage of vector Array

Label (Space Separation): vector STL C++ ACM

STL Standard Template Library provides three types of components:

Containers, iterators and algorithms all support generic programming standards. 

Containers are divided into two main categories:

Sequential containers and associated containers.
Sequential containers are: (vector,list,deque, string, etc.) an ordered set of elements.
Association container: (set, multiset,map, multimap) contains the key value of the search element;
The function of iterators is to traverse containers.
It is important to note that after each group of tests, the data must be cleared, otherwise the container will retain the data of the last test; (example bottom) 

Vector uses I:

#include <iostream> 
#include <vector>  //Vector needs; 
#include <cstdio> 
#include <numeric>  //accumulate algorithm needs; 
using namespace std; 
int a[10005]; 
int main() 
{ 
     vector <int> v;   //Define vector v; 
    vector<int>::iterator it;  //Define iterator it; 
    int n; 
     while(~scanf("%d", &n)) 
    { 
        for(int i=0; i<n; i++) {  //Assignment; 
            scanf("%d", &a[i]); 
            v.push_back(a[i]); 
        } 
        for(it=v.begin(); it!=v.end(); it++) {//Iterator iterator is used to traverse all elements sequentially. 
            printf("%d ", *it);  //Output the element values at the current position of the iterator; 
        } 
        printf("\n"); 
        printf("%d\n",accumulate(v.begin(), v.end(), 0));  //The sum of all elements of the vector is counted and output. 
    } 
    return 0; 
} 

Realize Fibonacci sequence:

#include <iostream> 
#include <vector> 
using namespace std; 
int main() 
{ 
    vector <unsigned int> v; 
    unsigned int n; 
    v.push_back(0); 
    v.push_back(1); 
    for(int i=2; i<50; i++) { 
        v.push_back(v[i-1]+v[i-2]); 
    } 
    while(cin >> n) { 
        cout << v[n] << endl; 
    } 
    return 0; 
} 

ZOJ 1179 *

#include <iostream> 
#include <cstdio> 
#include <numeric> 
#include <vector> 
using namespace std; 
int main() 
{ 
    vector <double> v; 
    int n; 
    while(~scanf("%d", &n)) { 
        double m; 
        v.clear(); 
        for(int i=0; i<n; i++) { 
            scanf("%lf", &m); 
            v.push_back(m); 
        } 
        double sum =(double)accumulate(v.begin(), v.end(), 0)/n; 
        printf("%.3lf\n", sum); 
    } 
    return 0; 
} 

Creating vector Objects

vector Vector vector Vector vector Vector vector Vector vector Vector vector Vector vector Vector vector Vector vector Vector vector Vector vector Vector vector Vector vector Vector vector Vector vector Ve 
It is worth noting that the vector container has the function of automatic management, which can dynamically adjust the memory space for insertion and deletion of elements; using the vector container, it needs to include the header file # include < vector >; 
The following table of vectors container counts from 0, that is, if the size of vectors container is n, then the following table of elements is from 0 to n-1. For the definition of the capacity of vectors container, we can define a fixed size. After that, we can adjust the size at any time, or we can not define it beforehand. We can use push_back() method to expand the element from the tail at any time, or we can use INS to define the capacity of vectors container. ERT () inserts a new element before the position of an element; 
vector containers have two important methods, begin() and end(). begin() returns an iterator for the location of the first element; end() returns an iterator for the location of the next element of the last element. 

Creating vector Objects
There are three forms:
(1) The number of elements in a container is not specified, such as defining a container for storing integers:
      vector v;
(2) When created, specify the size of the container, such as defining a vector container for storing 10 double-type elements:
Vector v(10); //Note: The subscript of the element is 0-9, and each element has a specified initial value;
(3) Create a vector container object with n elements, each of which has a specified initial value;
Vector V (10, 8.6); // Defines a vector container with 10 elements, each of which has a value of 8.6;
     

Tail element expansion

Use push_back() to append the element // that is, the tail element extension

By appending elements to the tail, the vector container automatically allocates new memory space. Expansion of empty vector objects can also extend vectors of existing elements.
Example: Add 2,7,9 three elements from the tail to the v container, so that there are three elements in the v container, 2,7,9;

#include<iostream> 
#include<vector> 
using namespace std; 
int main(int argc,char *argv[]) 
{ 
    vector<int>v; 
    v.push_back(2); 
    v.push_back(7); 
    v.push_back(9); 
    return 0; 
} 

Subscript access to vector elements

Accessing or traversing a vector object - For a vector object, one element can be accessed arbitrarily by subscription, and of course, one element can be reassigned by subscription, which is similar to the way of array access.
Using the example in the previous section, the elements in the container are accessed with subscripts:

#include<iostream> 
#include<vector> 
using namespace std; 
int main(int argc,char *argv[]) 
{ 
    vector<int>v(3); 
    v[0] = 2; 
    v[1] = 7; 
    v[2] = 9; 
    cout << v[0] << " "<< v[1] << " " << v[2] << endl; 
    return 0; 
} 

Iterator access to vector elements

The iterator is often used to traverse the vector object with for loop statement. The amount type of the iterator must be the same as the element type of the vector object he traverses.

#include<iostream> 
#include<vector> 
using namespace std; 
int main(int argc,char *argv[]) 
{ 
    vector<int>v(3); 
    v[0] = 2; 
    v[1] = 7; 
    v[2] = 9; 
    //Define iterator variables 
    vector <int> :: iterator it; 
    for(it=v.begin(); it != v.end(); it++) { 
        //Output the element values on the iterator; 
        cout << *it << " "; 
    } 
    cout << endl;  //Line change; 
    return 0; 
} 

vector element insertion

The insert() insert method can insert a new element in front of any position of the vector object. At the same time, the vector automatically expands the space of an element, and all elements in the insert position move one position backward at a time.
Note that the insert() method requires the insert location to be the iterator location of the element, not the subscript of the element.
Operational results: 82 1 7 9 5 3

#include<iostream> 
#include<vector> 
using namespacestd; 
int main(int argc,char *argv[]) 
{ 
    vector <int> v(3); 
    vector <int> :: iterator it;  //Define iterator variables; 
    v[0] = 2; 
    v[1]= 7; 
    v[2] = 9; 
    v.insert(v.begin(), 8);  //Insert a new element at the front with an element value of 8. 
    v.insert(v.begin()+2, 1);  //Insert a new element 1 after the second element. 
    v.insert(v.end(), 3);  //A new element 3 is added at the end of the vector. 
    v.insert(v.end()-1, 5);   //Insert a new element 5 after the penultimate element of the vector. 
    for(it=v.begin(); it != v.end(); it++) { 
        //Output the element values on the iterator; 
        cout << *it << ""; 
    } 
    cout << endl;  //Line change; 
    return 0; 
} 

vector element deletion

erase() method can delete an element caused by an iterator in vector or all elements in an interval.
clear() deletes all elements in vector at once.

#include<iostream> 
#include<vector> 
using namespace std; 
int main(int argc,char *argv[]) 
{ 
    vector <int> v(10); 
    vector <int> :: iterator it; 
    for(int i=0; i<10; i++) { 
        v[i] = i; 
    } 
    v.erase(v.begin()+2);   //Delete the second element and count from 0. 
    for(it=v.begin(); it != v.end(); it++) { 
        cout << *it << ""; 
    } 
    cout << endl; 
    v.erase(v.begin()+1, v.begin()+5);   //Delete the elements of interval [1, 5]; 
    for(it=v.begin(); it != v.end(); it++) { 
        cout << *it<< " "; 
    } 
    cout << endl; 
    v.clear(); //Delete all elements; (empty vectors) 
    cout << v.size() << endl;  //Output surname Liang size; 
    return 0;  
} 

vector elements are arranged in reverse

Reverse reverse permutation algorithm, need to define the header file include < algorithm >;
Reverse algorithm can arrange the elements of iterator intervals in a vector in reverse.

#include<iostream> 
#include<vector> 
#include<algorithm> 
using namespacestd; 
int main(int argc,char *argv[]) 
{ 
    vector <int> v(10); 
    vector <int> :: iterator it; 
    for(int i=0;i<10; i++) { 
        v[i] = i; 
    } 
    for(it=v.begin(); it != v.end(); it++) { 
        cout << *it << ""; 
    } 
    cout << endl; 
    reverse(v.begin(), v.end());  //Inversely arranging the elements of vectors from beginning to end; 
    for(it=v.begin(); it != v.end(); it++) { 
        cout << *it << ""; 
    } 
    cout << endl; 
    return 0; 
} 

Sorting Elements in vector Using sort Algorithms

The header file # include < algorithm > must be added to use sort algorithm.
The sort algorithm requires the use of random access iterators for sorting, and the vector elements are sorted in ascending order by default.
Similar to sorting arrays;

#include<iostream> 
#include <vector> 
#include<algorithm> 
using namespace std; 
int main(int argc,char *argv[]) 
{ 
    vector <int> v; 
    vector <int> :: iterator it; 
    for(int i=0; i<10; i++) { 
        v.push_back(9-i); 
    } 
    for(int i=0; i<10; i++) {  //Output element values before sorting; 
        cout << v[i] << ""; 
    } 
    cout << endl; 
    sort(v.begin(), v.end());  //Ascending order arrangement; 
    for(it=v.begin(); it != v.end(); it++){  //Output sorted element values; 
        cout << *it << ""; 
    } 
    cout << endl; 
    return 0; 
} 

You can also define a cmp sort comparison function by yourself, and then assign this function to the sort algorithm, so sort is sorted according to the sort rules specified by the comparison function. The following example is sorted from large to small.

#include<iostream> 
#include<vector> 
#include<algorithm> 
using namespacestd; 
bool cmp(const int a, const int b) 
{ 
    return a > b; 
} 
int main(int argc,char *argv[]) 
{ 
    vector <int> v; 
    vector <int> :: iterator it; 
    for(int i=0; i<10; i++) { 
        v.push_back(i); 
    } 
    for(int i=0; i<10; i++) {  //Output element values before sorting; 
        cout << v[i] << " "; 
    } 
    cout << endl; 
    sort(v.begin(), v.end(), cmp);  //Ascending order arrangement; 
    for(it=v.begin(); it != v.end(); it++){  //Output sorted element values; 
       cout << *it << " "; 
    } 
    cout << endl; 
    return 0; 
} 

Is the size of the vector empty?

The size() method can be used to return the size of the vector, that is, the number of elements.
empty() method is used to return whether the vector is empty or not.
The following are simple applications of these two methods:

#include<iostream> 
#include<vector> 
using namespace std; 
int main(int argc,char *argv[]) 
{ 
    vector <int> v; 
    vector <int> :: iterator it; 
    for(int i=0; i<10; i++) {   //Assign values to vectors; 
        v.push_back(i); 
    } 
    cout << v.size() << endl;   //The size of the output vector; (that is, the number of elements) 
    cout << v.empty() << endl;  //Determine whether it is empty, return 0 if it is not empty, and return 1 if it is empty. 
    v.clear(); 
    cout << v.empty() <<endl; 
    return 0; 
} 

Application of vector Vector vector Vector vector Container ZOJ 1208

Some of the strings are symmetrical, and some are not. Please output the symmetrical strings in order from small to large. The strings are length-based first, and if the length is equal, then the ASCII code value is used as the sorting standard.
Input Description: Enter an n to indicate that there are n groups of strings next, with string length <=256; n<=1000;
Output Description: According to each string, output symmetrical strings, and require output in order from small to large;
Input sample:

7 
123321 
123454321 
123 
321 
sdfsdfd 
\\dd\\ 
121212 
Output sample: 
123321 
\\dd\\ 
123454321 
#include<iostream> 
#include<vector> 
#include<string> 
#include<algorithm> 
using namespace std; 
bool cmp(const strings1, const string s2) 
{ 
    return (s1.length() != s2.length() ?s1.length() < s2.length() : s1 < s2); 
} 
int main() 
{ 
    vector <string> v; 
    vector <string>:: iterator it; 
    int n; 
    string s, t; 
    while(cin >> n) { 
        for(int i=0; i<n; i++) { 
            cin >> s; 
            t = s; 
            reverse(t.begin(), t.end()); 
            if(t == s) { 
                v.push_back(s); 
            } 
        } 
        sort(v.begin(), v.end(), cmp); 
        for(it=v.begin(); it != v.end(); it++){ 
            cout << *it<< endl; 
        } 
    } 
    return 0; 
}

Keywords: Programming ascii

Added by arbab on Wed, 19 Jun 2019 20:41:04 +0300