vector 对象类型用法

/***
	- 删除 某一个范围的 vector 的元素 可以使用 remove_if()
	- 练习使用 vector 对象类型的对象

***/
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

class Car
{
	public:
		string name;
		int nub;
		int speed;
	Car(string na,int nb)
	{
		name = na;
		nub = nb;
	}

	Car(int p_speed)
	{
		speed = p_speed;
		cout<<"p_speed"<<p_speed<<endl;
	}
	~Car(){};
};

bool cmp(Car& ca)
{
	return ca.nub < 100; //eraes 小于 100的
}

int main()
{
	vector<Car> cars;
	vector<Car> :: iterator it;

	Car ca1("yq1",666);
	Car ca2("yq2",888);
	Car ca3("yq3",999);
	Car ca4("yq4",66);

	cars.push_back(ca1);
	cars.push_back(ca2);
	cars.push_back(ca3);
	cars.push_back(ca4);
	
	cars.erase(remove_if(begin(cars),end(cars),cmp),end(cars)); // remove_if() 的用法
	for(it = cars.begin();it != cars.end();++it)
	{
		cout<< " car.mame = "<<(*it).name << " car.nub = "<< (*it).nub<<endl;
	}
	
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值