Basic usage of vector container

Source: Internet
Author: User
Tags arrays data structures printf sorts

Vector, as one of the standard containers provided by STL, is often used, has a very important status, and is often used as a convenience. Vector is also known as vectors, vector can be described as an image of the length can be dynamically changed arrays, functions and arrays are more similar. In fact, a more professional description is: Vector is a multifunctional, able to manipulate a variety of data structures and algorithms of the template class and function library, vector is considered a container, because it can be like a container to store various types of objects, in short, Vector is a dynamic array that can hold any type and can add and compress data.

Vector expansion mechanism: increases by one times the container's current capacity. The vector container allocates a contiguous memory space, each time the container grows, instead of simply overlaying the original contiguous memory space, it re-applies a larger piece of new memory and copies the elements of the existing container one by one, then destroys the old memory. Iterators that originally point to the old memory space are invalidated, so when the container is manipulated, the iterator is updated in a timely manner.

#include <stdio.h> #include <string.h> #include <algorithm> #include <math.h> #include <

vector> #include <map> #include <iostream> using namespace std;
vector<int>mem;

int Z, t, N;

    void Declars ()///vector declaration of the container {////defines an empty vector vector<int>mem1;

    The vector vector<int>mem2 (50) with a size of 50 is generated;

    A vector vector<int>mem3 (50,6) that has a size of 50 and each element is 6;
    int ans[5]={1,2,3,4,5}; VECTOR&LT;INT&GT;MEM4 (ans,&ans[5]);/////(Begin,end) as the initial value of the vector} void Cins ()///vector container input {for (int i = 0; I & Lt Z
        i++) {scanf ("%d", &t);   Mem.push_back (t);    add element at tail}///mem.pop_back ();
    Delete element at tail} void couts ()///vector container output {for (int i = 0; i < z-1; i++) printf ("%d", mem[i]);

    printf ("%d\n", mem[z-1]); Vector<int>::iterator it; Defines an iterator that iterates through or points to the elements inside the container, which can be understood as a container pointer for (it = Mem.begin (); it < mem.end () 1; it++) printf ("%d", *it);
    printf ("%d\n", *it);
    } void Sizes ()///vector the number of elements in the container {printf ("%d\n", Mem.size ()); printf ("%d\n", Mem.max_size ()),///vector container can hold the maximum number of elements} void Emptys ()///vector container is empty {printf ("%d\n", Mem.empty ()? 0: 1); NULL returns 0, non-null returns 1} void Sorts ()///vector container sorted by {sort (Mem.begin (), Mem.end ());///From small to large sort reverse (mem.begin (), Mem.end (
    ), or/////////To sort from small to large to sort through} void finds (int n)///Find the value of n with the vector container and return the position of n {///vector<int>::iterator fd;
    Vector<int>::iterator fd = Find (Mem.begin (), Mem.end (), n);
printf ("%d\n", *FD);
    } struct Strings {char C;
    string S;
int e;

}STR;
    void structs () Application of///vector container in structure {vector<strings> maps;
    STR.S = "Hellow word!";
    STR.E = 66666;
    str.c = ' A ';
    Maps.push_back (str);
cout << str.s << endl << str.c << endl << str.e << Endl;
        } int main () {while (~SCANF ("%d", &z)) {scanf ("%d", &n);     Cins (); Vector container Input couts ();    The output sorts () of the vector container;    Vector container sorting couts ();    The output sizes () of the vector container;   The number of elements in the vector container finds (n);  Finds the value of n with the vector container and returns the position of n structs ();    Application of vector container in structure mem.clear ();///Empty container vector <int> (). Swap (MEM);
Clear the container and minimize its capacity,} return 0;
 }


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.