vector-size

////////////////////////////////////////
//      2018/04/21 22:11:06
//      vector-size
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

template<class T>
class Print
{
public:
    void operator ()(T& t){
        cout << t << " ";
    }
};

//====================
int main(){

    vector<char> v(5);
    Print<char> print;
    cout << "Size of v = " << v.size();
    fill(v.begin(),v.end(), '*');
    cout << endl;

    for (int i = 0; i < v.size(); i++){
        cout << v[i] << " ";
    }
    cout << endl;

    for (int i = 0; i < 5; i++){
        cout << "Size of V = " << v.size() << endl;
        for_each(v.begin(),v.end(),print);
        cout << endl;
        v.pop_back();
    }

    return 0;
}

/*
OUTPUT:
    Size of v = 5
    * * * * *
    Size of V = 5
    * * * * *
    Size of V = 4
    * * * *
    Size of V = 3
    * * *
    Size of V = 2
    * *
    Size of V = 1
    *
*/
原文地址:https://www.cnblogs.com/laohaozi/p/12538024.html