c++笔记一 c++里面的二进制

学习c++要有好的参考资料,在这里推荐大家两个:

http://www.cplusplus.com/
http://www.cppreference.com/wiki/
曾经试图把整个网站下载下来,结果却是没成功,她们连接都太多了,丰富的一塌糊涂~~~

size_t


type
<cstddef>

Unsigned integral type

size_t corresponds to the integral data type returned by the language operator sizeof and is defined in the <cstddef> header file (among others) as an unsigned integral type.

It expresses a size or count in bytes.

下面就先贴出一粒用她们来编的一道c++
二进制数流转化成ip地址,主要采用了一个<bitset>类库,<string>类库,
#include    <iostream>
#include  <string>
#include <bitset>
using namespace std;
void bintoip(string s)
{
int j,i;
for(i=0,j=0;i<32;i+=8,j++)
{
bitset<8> temp(s.substr(i,i+8));       //从第i位开始取8位包括第i位
if(j!=3)
cout<<temp.to_ulong()<<".";    //二进制转化成十进制
else
cout<<temp.to_ulong()<<endl;    //要注意bitset<>定义的对象长度是固定的
}
}
int main()
{
int i,n;
cin>>n;
string *p;
p=new string [n];
for(i=0;i<n;i++)
{
cin>>p[i];
}
for(i=0;i<n;i++)
{
bintoip(p[i]);
}
return 0;
}


同时也说明STL库的好处,特举下例

另一种方法  (这是利用bitset类库来做的,跟简单明了)
#include  <string>
#include <bitset>
using namespace std;
int main()
{
string p;
cin>>p;
bitset<20> temp(p);    //要注意bitset<>定义的对象长度是固定的
cout<<temp;
return 0;
}


我的百度空间有其他的资料:http://hi.baidu.com/dudiaodaoke/blog/item/9cc4d213d9eaee72cb80c45a.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值