c++容器

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;
    



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值