vector基础应用

#include <iostream>
#include <vector>
#include <string>
#include <iterator>
#include <algorithm>
#include <functional>
using namespace std;
int main1()
{
    vector <string> vec;
    vector<string>::const_iterator i;//相当于指针的迭代器(游标)
    vec.push_back("dog");//插入字符串到向量vec中
    vec.push_back("bird");
    vec.push_back("girl");
    vec.push_back("boy");
    vec.push_back("hello,there");
    for (i = vec.begin(); i != vec.end(); ++i)//循环输出向量中的所有字符串
        cout << (*i) << endl;
    return 0;
}
//反向迭代器
int main2()
{
    vector <int> v;
    v.push_back(6);
    v.push_back(8);
    v.push_back(9);
    reverse_iterator<vector<int>::iterator> rfirst(v.end());
    reverse_iterator<vector<int>::iterator> rend(v.begin());

    while (rfirst !=rend)
    {
        cout << *rfirst << endl;
        ++rfirst;
    }   
    return 0;
}

//函数对象适配器的使用
int main3()
{
    vector<int> v;
    v.push_back(20);
    v.push_back(13);
    v.push_back(6);
    v.push_back(3);
    v.push_back(29);
    //利用bind1st适配器和greater函数对象生成一个“小于7”的为此判断,作为find_if的查找判断
    //从而在v中找到第三个元素6,并将其打印出来;
    vector<int>::iterator less7_iter = find_if(v.begin(), v.end(), bind1st(greater<int>(), 7));
    cout << *less7_iter << endl;
    return 0;

}
//函数自动转换为函数对象
bool less7(int x)
{
    return x < 7;
}
int main4()
{
    vector<int> v;
    v.push_back(20);
    v.push_back(13);
    v.push_back(6);
    v.push_back(3);
    v.push_back(29);
    vector<int>::iterator less7_iter = find_if(v.begin(), v.end(), less7);
    cout << *less7_iter << endl;
    return 0;
}
//任意位置insert  vector元素
int main5()
{
    vector<int> v;
    v.push_back(6);
    v.push_back(7);
    v.push_back(8);
    v.push_back(10);
    v.insert(v.begin() + 3, 9);
    v.insert(v.begin(), 5);
    v.insert(v.end(), 11);
    vector<int>::iterator i;
    int j ;
    for ( i= v.begin(), j=0; i != v.end(); i++,j++)
        cout << "[" << j << "] = " << *i << endl;
    return 0;
}
class MyAnimal
{
public:
    char* name;
    int age;

public:
    MyAnimal(char *name, int age)
    {
        this->name = name;
        this->age = age;
    }
    ~MyAnimal() {}
};
//利用erase删除元素
int main6()
{
    MyAnimal* pDog = new MyAnimal("dog", 1);
    MyAnimal* pMonkey = new MyAnimal("monkey", 2);
    MyAnimal* pChicken = new MyAnimal("chicken", 3);
    MyAnimal* pSnake = new MyAnimal("snake", 4);

    vector<MyAnimal*> v;                //MyAnimal*
    v.push_back(pDog);
    v.push_back(pMonkey);
    v.push_back(pChicken);
    v.push_back(pSnake);

    delete pMonkey;//物理删除pMonkey所指的对象
    v.erase(v.begin() + 1);//删除第二个元素
    vector<MyAnimal*>::iterator i, iend;
    iend = v.end();
    for (i = v.begin(); i != iend; i++)
        cout << (*i)->name << ' ' << (*i)->age << endl;//注意括号!!

    v.clear();
    cout << "执行clear()" << endl << "所有元素已经全部清除" << endl;
    return 0;
}

//反向遍历
int main7()
{
    vector<int> v;
    v.push_back(1);
    v.push_back(3);
    v.push_back(5);
    v.push_back(7);
    v.push_back(9);

    vector<int>::reverse_iterator ri, riend;
    riend = v.rend();
    for (ri = v.rbegin(); ri != riend; ri++)
        cout << *ri << endl;
    return 0;
}
//两个vector容器元素交换
void print(vector<int> &v);//声明打印函数

int main8()
{
    vector<int> v1;
    v1.push_back(5);
    v1.push_back(7);
    v1.push_back(9);
    cout << "v1=";
    print(v1);

    vector<int> v2;
    v2.push_back(22);
    v2.push_back(23);
    cout << "v2=";
    print(v2);


    swap(v1, v2);
    cout << "v1,v2交换后" << endl;

    cout << "v1=";
    print(v1);

    cout << "v2=";
    print(v2);
    return 0;
}

void print(vector<int> &v)
{
    for (unsigned int i = 0; i < v.size(); i++)//符号列表匹配
        cout << v[i] << " ";
    cout << endl;
}

//vector的一些统计函数的使用

void print1(vector<int> &v);
int main()
{
    vector<int> v;
    print1(v);
    v.push_back(1);
    v.push_back(3);
    v.push_back(5);
    v.push_back(7);
    v.push_back(9);
    print1(v);

    v.push_back(2);
    v.push_back(4);
    v.push_back(6);
    v.push_back(8);
    print1(v);

    v.reserve(30);
    print1(v);
    return 0;
}
void print1(vector<int> &v)
{
    cout << "-----------------------" << endl;
    cout << "empty = " << v.empty() << endl;//判断是否为空,为空返回true,否则返回false
    cout << "size = " << v.size() << endl;//当前vector的实际元素个数
    cout << "Max_size = " << v.max_size() << endl;//系统所允许的vector最大元素个数
    cout << "capacity = " << v.capacity() << endl;//当前可容纳的vector元素个数
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值