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容器数据的操作。