boost库 set初步使用

本文介绍了Boost库中的侵入式容器,它与STL容器不同,通过减少数据拷贝来提高效率。示例展示了如何使用Boost::intrusive::set创建一个动物集合,并进行插入、删除操作,展示了侵入式容器的便捷和高效。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

boost库提供了一种容器叫做侵入式容器,侵入式容器与stl库提供的容器有所不同;侵入式容器的使用减少了数据之间的拷贝。。。。。

#include <boost/intrusive/set.hpp>
#include < iostream>
#include < utility>
#include < string>
using namespace boost::intrusive;
using namespace std;
class animal{
public:
int _legs;
set_member_hook<> _set_hook;
animal(int legs) : _legs(legs) {
}
~animal() {
std::cout << "legs = " << _legs << std::endl;
std::cout << “I am ~animal” << std::endl;
}
bool operator<(const animal &a) const {
return _legs < a._legs;
}
};

int main () {
std::cout << “Hello Boost” << std::endl;
animal a1(1);
animal a2(2);
{
typedef member_hook<animal, set_member_hook<>, &animal::_set_hook> hook;
typedef set<animal, hook, constant_time_size< false>> animal_set;
animal_set animals;
animals.insert(a2);
animals.insert(a1);
std::cout << "size = " << animals.size() << std::endl;
for (const animal &a : animals) {
std::cout << "legs = " << a._legs << std::endl;
}
auto begin_it = animals.begib();
animals.erase(begin_it);
std::cout << "size = " << animals.size() << std::endl;
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值