vector的一些操作

vector 为什么不可以直接用cin插入的故事
vector
http://www.cplusplus.com/reference/vector/vector/?kw=vector
template < class T, class Alloc = allocator<T> > class vector; // generic template

当你写个程序比如下面这个
#include <bits/stdc++.h>

int main(){
    int n; 
    std::cin >> n;
    std::vector<int> v;
    for(int i = 0; i < n; i++)    std::cin >> v[i];

    for(int i = 0; i < n; i++)    std::cout << v[i] << " ";
    return 0;
}

 你会发现欸, 程序怎么突然挂了  异常退出

 那是因为上面的程序 vector 只是声明而已, 并没有开辟内存  你有如下两种选择

 1) If you know the size of vector will be (in your case/example it's seems you know it):

#include <bits/stdc++.h>

int main(){
    int n; 
    std::cin >> n;
    std::vector<int> v(n);//std::vector<int> v;  v.resize(n);
    for(int i = 0; i < n; i++) std::cin >> v[i]; 

    for(int i = 0; i < n; i++) std::cout << v[i] << " "; 
    return 0; 
}
2) if you don't and you can't get it in you'r program flow then:
#include <bits/stdc++.h>

int main(){
    int n; 
    std::cin >> n;
    std::vector<int> v;
    for(int i = 0; i < n; i++)   {
        int x;
        std::cin >> x; 
        v.push_back(x);  
    } 
for(int i = 0; i < n; i++) std::cout << v[i] << " "; return 0; }

Size is not allowed to differ between multiple compilers. The size of a vector is the number of elements that it contains, which is directly controlled by how many elements you put into the vector.

Capacity is the amount of space that the vector is currently using. Under the hood, a vector just uses an array. The capacity of the vector is the size of that array. This is always equal to or larger than the size. The difference between them is the number of elements that you can add to the vector before the array under the hood needs to be reallocated.

You should almost never care about the capacity. It exists to let people with very specific performance and memory constraints do exactly what they want.
Size 是vector当前使用的大小,  Capacity是vector的总容量  后者一定会大于等于前者

   resize和reserve

void reserve (size_type n);
void resize (size_type n);
void resize (size_type n, const value_type& val);
reserve 和 resize 都是重新分配vector的容量, 区别在于后者可以给新元素赋值 如果后者重新分配的空间大于vector之前的容量且val给定值的话 那新的元素就会被初始化为 val值
当vector当前容量 大于重新分配的容量时, 前者无影响 后者会将多出的元素删去
当vector容量不够时, 会自动扩展内存 1.5~2倍(depend on编译器)
#include <bits/stdc++.h>

int main(){
    
    std::vector<int> v;
    int storge = v.size();
    std::cout << v.max_size() << '
';
    for(int i = 0; i < 100; i++)
    {
        v.push_back(i);
        if(storge != v.capacity()) {
            storge = v.capacity();
            std::cout << "v.capacity = " << storge << '
';
        }
    }
    v.reserve(10);//v.resize(10);
    for(int i = 0; i < v.size(); i++)
         std::cout << v[i] << " ";
    return 0;
}
/*
output
1073741823
v.capacity = 1
v.capacity = 2
v.capacity = 4
v.capacity = 8
v.capacity = 16
v.capacity = 32
v.capacity = 64
v.capacity = 128
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
*/


原文地址:https://www.cnblogs.com/163467wyj/p/12008751.html