c++容器

来源:互联网 发布:淘宝游戏专营怎么进入 编辑:程序博客网 时间:2024/06/08 10:35

1、vector

删除vector元素

v.erase(m,n);

两个vector合并

strTotalVec.insert(strTotalVec.end(),strVec.begin(),strVec.end());

容器赋值

vData.assign(strVec.begin(), strVec.end())

容器删除

strVec.insert(strVec.begin()+j,sName);

例子:

vector<Curve*> vCurve;

function(vCurve);  //函数调用

function(vector<Curve*> &curves)

{

for(int i = 0; i < num; i++)

{

curves.push_back(new Curve[1]);

}

for(int i = 0; i < num; i++)

{

Curve *curve = curves.at(i);

//然后再给curve赋值

}

}

释放

list<obj *>::iterator ite; 

  1. for( ite = m_list.begin(); ite != m_list.end(); ++ite)  
  2. {  
  3.     delete (*ite);  
  4.     ite = m_list.erase(ite);  
  5. }  

容器释放内存出错:  new时,一会new a()  一会new a[1] 指针和数组混乱

容器删除元素

vector<CurveSamples::CurveSample>::iterator ite; 
for( ite = vCurveSample.begin()+1; ite != vCurveSample.end();)  
{
CurveSamples::CurveSample one = *ite;
CurveSamples::CurveSample two = *(ite-1);
if((one.y - two.y) < 0.00001)
ite = vCurveSample.erase(ite);
else
++ite;
}

自定义容器排序

  1. class CompLess  
  2.  {  
  3.  public:  
  4.      bool operator ()(const TItem& stItem1, const TItem& stItem2)  
  5.      {  
  6.          return stItem1.m_i32Type < stItem2.m_i32Type;  
  7.      }  
  8.  };  
  9.    
  10.   
  11. class CompGreater  
  12.  {  
  13.  public:  
  14.      bool operator ()(const TItem& stItem1, const TItem& stItem2)  
  15.      {  
  16.          return stItem1.m_i32Type > stItem2.m_i32Type;  
  17.      }  
  18.  };  

sort(stItemVec.begin(), stItemVec.end(), CompLess()); //升序排序  


2、Map

    map<CString, int> colIndo;    map<int, ColInfo> colHead;    



原创粉丝点击