vector-clear

////////////////////////////////////////
//      2018/04/15 19:24:30
//      vector-clear

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

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

// ==================
int main(){
    vector<int> v(10);
    Print<int> print;
    fill(v.begin(), v.end(), 5);

    cout << "Vector v:" << endl;
    for_each(v.begin(), v.end(), print);
    cout << endl;
    cout << "Size of v = " << v.size() << endl;

    cout << "v.clear()" << endl;
    v.clear();

    cout << "Vector v:" << endl;
    for_each(v.begin(), v.end(), print);
    cout << endl;
    cout << "Size of v =" << v.size() << endl;
    cout << "Vector is";
    cout << v.empty() ? "" : "not";
    cout << "empty" << endl;

    return 0;
}


/*
OUTPUT:
    Vector v:
    5 5 5 5 5 5 5 5 5 5
    Size of v = 10
    v.clear()
    Vector v:

    Size of v =0
    Vector is1empty
*/ 
阅读更多

扫码向博主提问

小小妖妖

非学,无以致疑;非问,无以广识
去开通我的Chat快问
版权声明:我是一只快乐的小妖精 https://blog.csdn.net/qwq1503/article/details/79952283
个人分类: STL
所属专栏: C++STL样列
上一篇vector - capacity(容量)
下一篇vector-empty
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭