STL之bitset

两天没更新博客,看来想养成好习惯还是简单啊!

两天看来下mapmultimap的相关用法只有一个地方觉着新鲜。

map<int ,double> m1;
pair<map<int ,double>::iterator ,bool > p1 ,p2;
p1 = m1.insert(pair<int ,double>(1 ,2.0));
在往集合中插入元素的时候,会返回 pair<map<int ,double>::iterator ,bool > 类型的对象,其中包含了是否插入成功,以及插入成功后该元素的指针。

关于bitset,代码已经很清晰了,留着就行。

#include <iostream>
#include <bitset>
#include <string>

using namespace std;

void print(bitset<16> & b)
{
	int i = 0 ;
	int bsize = b.size();
	for(i = 0 ; i < bsize ;i++)
	{
		cout << b[i];
	}
	cout << endl;
}

int main()
{
	string str = "010101010101010101010101";
	bitset<16> b1;
	bitset<16> b2(3);
	bitset<16> b3(str ,2 ,16);

	print(b1);
	print(b2);
	print(b3);

	//统计'1'的个数
	int c1 = b1.count();
	int c2 = b2.count();
	int c3 = b3.count();
	cout << "b1 's count : " << c1 << ", " << "b2 's count : " << c2 << ", " << "b2 's count : " << c3 << ". " << endl;

	//判断位段中是否有一位为1;
	bool l1 = b1.any();
	bool l2 = b2.any();
	bool l3 = b3.any();
	cout << "b1 's any() : " << l1 << ", " << "b2 's any() : " << l2 << ", " << "b2 's any() : " << l3 << ". " << endl;

	//判断位段中是否全部为0;
	bool n1 = b1.none();
	bool n2 = b2.none();
	bool n3 = b3.none();
	cout << "b1 's none() : " << n1 << ", " << "b2 's none() : " << n2 << ", " << "b2 's none() : " << n3 << ". " << endl;

	//判断该位是0还是1;
	bool t1 = b1.test(2);
	bool t2 = b2.test(2);
	bool t3 = b3.test(2);
	cout << "b1 's test() : " << t1 << ", " << "b2 's test() : " << t2 << ", " << "b2 's test() : " << t3 << ". " << endl;

	//设置位;
	cout << "set(5) :" << endl;
	b1.set(5 ,1);
	b2.set(5 ,1);
	b3.set(5 ,1);
	print(b1);
	print(b2);
	print(b3);

	//复位;
	cout << "reset(5) :" << endl;
	b1.reset(5);
	b2.reset(5);
	b3.reset(5);
	print(b1);
	print(b2);
	print(b3);

	//"flip"
	cout << "flip() :" << endl;
	b1.flip();
	b2.flip();
	b3.flip();
	print(b1);
	print(b2);
	print(b3);

	//位段转换为数值;
	unsigned long u11 = b1.to_ulong();
	unsigned long u12 = b2.to_ulong();
	unsigned long u13 = b3.to_ulong();
	cout << "to_ulong() : " << endl;
	cout << u11 << endl;
	cout << u12 << endl;
	cout << u13 << endl;

	//尾段转换为字符串;
	string s1 = b1.to_string();
	string s2 = b2.to_string();
	string s3 = b3.to_string();
	cout << "to_string() : " << endl;
	cout << s1.c_str() << endl;
	cout << s2.c_str() << endl;
	cout << s3.c_str() << endl;
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值