vector不得不说的故事



1、 const vector, 要想得到迭代器的话就要是const_iterator,不然会报错
const std::vector<Touch*>& touches
std::vector<Touch*>:: const_iterator iter = touches.begin();

2、普通vector
std::vector<Touch*>& touches
std::vector<Touch*>:: iterator iter = touches.begin();

3、vector删除代码
for(vector<int>::iterator it = vecNum.begin(); it != vecNum.end();)
    {
        if((*it) == 1)
        {
            CCLog("zhong biao le");
            it = vecNum.erase(it);
        }
        else
             ++it;
    }
4、“#include <vector>” 和 “#include <vector.h>”的区别

有区别,如果您#include <vector>,若使用vector声明变量的话,要使用using namespace std; vector<int> a;才能通过编译,或者使用std::vector<int> a;。如果#include <vector.h>,则仅仅需要vector<int> a;就行了。因为在标准C++中,vector是定义在std空间中的,这意味着您必须在使用前声明使用std空间,否则编译器报错。另外,如果您定义了自己的vector,可以通过定义独立的namespace空间,使得在您的程序里同时存在std::vector和您自己的vector。

5、vector内存释放
法一(通用型):
// 一个块,为了使temp出了大括号就被释放掉;通过swap方法将testvec的大小控制在bengin()和end()之间;
    {
        vector<int> temp = testvec;
        testvec.swap(temp);
    }

法二(清空):
    vector<int>().swap(testvec);

法三(建立一个新对象释放多余空间):
        vector<int>(testvec).swap(testvec);

头文件 #include <algorithm> 中有一些公共函数很不错。如find、sort等,方便对vector容器数据的操作。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值