bitset

ahuisafe 2008-05-28 09:46:27
1.问题是:循环中为什么打印只打印了一次,for语句为什么没起作用?

#include <iostream>
#include <cstring>

int main()
{
// C-style character string implementation
const char *pc = "a very long literal string";
const size_t len = strlen(pc +1); // space to allocate
std::cout<<len<<std::endl;
// performance test on string allocation and copy
int i=0;
for (size_t ix = 0; ix != 10; ++ix,++i)
{
char *pc2 = new char[len + 1]; // allocate the space
strcpy(pc2, pc); // do the copy
if (strcmp(pc2, pc)) // use the new string
; // do nothing
std::cout<<pc2<<std::endl;
std::cout<<i<<std::endl;
delete [] pc2; // free the memory
}
system("pause");
return 0;
}

2.另外还有个问题:
string对象和bitset对象是反向转化的,即string对象的最右边字符(即下标最大的那个字符)用来初始化bieset的低阶位(即下标位0的那个位),若string对象的个数小于bieset对象个数,则高阶位置0。但为什么会这样呢?如 string s="1100" bieset<8> bs(s) 则bs="00110000",而打印出来的是"00001100",为什么是反的呢,请解释一下为什么是这样?和高低字节存储有关系吗?
...全文
179 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
Paradin 2008-05-28
  • 打赏
  • 举报
回复
用string01串和用数初始化不同撒
ahuisafe 2008-05-28
  • 打赏
  • 举报
回复
谢谢两位解答,我没在gcc上试过,现在机子没装那个环境
ahuisafe 2008-05-28
  • 打赏
  • 举报
回复
我也知道bitset是从高位到低位打印的,我想问下为什么要这样?
比如我们定义一个int i=1234;打印出来肯定是 1234,而不是4321;
而我们定义一个 bitset<8> bs(oxF0) 打印出来却是00001111,why?
ahuisafe 2008-05-28
  • 打赏
  • 举报
回复
VS2005, 第一个程序运行结果:
25
a very long literal string
0
这是我在vs2005的运行结果,怎么不一样呢
Paradin 2008-05-28
  • 打赏
  • 举报
回复
bitset也是从高位到低位打印的,说错了。而bs[i]是从低到高的第i位
Paradin 2008-05-28
  • 打赏
  • 举报
回复
VS2005, 第一个程序运行结果:
25
a very long literal string
0
a very long literal string
1
a very long literal string
2
a very long literal string
3
a very long literal string
4
a very long literal string
5
a very long literal string
6
a very long literal string
7
a very long literal string
8
a very long literal string
9

2. string s="1100" bieset <8> bs(s) 打印出来的是"00001100",
意思就是我们把string的二进制串看做从高位到低位的排列,bitset也是从低位到高位打印的,这样符合人的习惯,这和用数组模拟
大数运算时的存储习惯也是一样的。至于bitset内部怎么存储应用时不需考虑,一般是long数组.
lionc650 2008-05-28
  • 打赏
  • 举报
回复
第二个问题就不太清楚了...
lionc650 2008-05-28
  • 打赏
  • 举报
回复
楼主的第一个程序在g++里面能够正确执行,而且程序看上去也没什么问题,就是不知道具体要做什么,执行结果如下:
25

a very long literal string

0

a very long literal string

1
a very long literal string

2

a very long literal string

3

a very long literal string

4

a very long literal string

5

a very long literal string

6

a very long literal string

7

a very long literal string

8

a very long literal string

9

ahuisafe 2008-05-28
  • 打赏
  • 举报
回复
第一次发贴问问题,题目还没写好就发出去了,好像不能改~
帅得不敢出门 2008-05-28
  • 打赏
  • 举报
回复

#include <stdafx.h>
#include <iostream>
#include <cstring>

int main()
{
// C-style character string implementation
const char *pc = "a very long literal string";
const size_t len = strlen(pc); //注意把这里改成这样,因为你后面new的时候len+1了
std::cout<<len<<std::endl;
// performance test on string allocation and copy
int i=0;
for (size_t ix = 0; ix != 10; ++ix,++i)
{
char *pc2 = new char[len + 1]; // allocate the space
strcpy(pc2, pc); // do the copy
if (strcmp(pc2, pc)) // use the new string
; // do nothing
std::cout<<pc2<<std::endl;
std::cout<<i<<std::endl;
delete [] pc2; // free the memory
}
//system("pause");
return 0;
}





26
a very long literal string
0
a very long literal string
1
a very long literal string
2
a very long literal string
3
a very long literal string
4
a very long literal string
5
a very long literal string
6
a very long literal string
7
a very long literal string
8
a very long literal string
9
请按任意键继续. . .
bitxinhai 2008-05-28
  • 打赏
  • 举报
回复
const size_t len = strlen(pc) +1; // space to allocate
ahuisafe 2008-05-28
  • 打赏
  • 举报
回复
楼上的理解是对的,为什么要这样做呢?在等等看又没有新的回答
请问楼上为什么我第一个程序在vs2005上 不想楼下显示的那样 我的只显示一行啊
怎么回事 分配内存 然后再释放 我建的是console 型程序没问题吧
帅得不敢出门 2008-05-28
  • 打赏
  • 举报
回复
bitset bit 其下标是这样的

.... 1 1
b[n] bit[1] bit[0]

下标从右向左增加的

而 string str的下标增加是
从左至右的
但是用string 对象初始化bitset对象时,string 对象直接表示为位模式, 从string对象读入位集的顺序则是从右向左.
所以
str的最右边向左,依次赋值给bit[0].....bit[n]
所以
string s="1100" bieset <8> bs(s)
打印出来的是"00001100", s是从右至左,依次对bitset赋值.
string对象和bitset对象是反向转化的

这就是楼主说的
string对象和bitset对象是反向转化的,即string对象的最右边字符(即下标最大的那个字符)用来初始化bieset的低阶位(即下标位0的那个位),若string对象的个数小于bieset对象个数,则高阶位置0

在C++的STL中实现由一个bitset类模板,其用法如下: std::bitset bs; 也就是说,这个bs只能支持64位以内的位存储和操作;bs一旦定义就不能动态增长了。本资源附件中实现了一个动态Bitset,和标准bitset兼容。 /** @defgroup Bitset Bitset位集类 * @{ */ //根据std::bitset改写,函数意义和std::bitset保持一致 class CORE_API Bitset: public Serializable { typedef typename uint32_t _Ty; static const int _Bitsperword = (CHAR_BIT * sizeof(_Ty)); _Ty * _Array; //最低位放在[0]位置,每位的默认值为0 int _Bits;//最大有效的Bit个数 private: int calculateWords()const; void tidy(_Ty _Wordval = 0); void trim(); _Ty getWord(size_t _Wpos)const; public: //默认构造 Bitset(); //传入最大的位数,每位默认是0 Bitset(int nBits); virtual ~Bitset(); //直接整数转化成二进制,赋值给Bitset,最高低放在[0]位置 Bitset(unsigned long long _Val); //拷贝构造函数 Bitset(const Bitset & b); Bitset(const char * str); Bitset(const std::string & str, size_t _Pos, size_t _Count); public: size_t size()const; //返回设置为1的位数 size_t count() const; bool subscript(size_t _Pos) const; bool get(size_t pos) const; //设置指定位置为0或1,true表示1,false表示0,如果pos大于数组长度,则自动扩展 void set(size_t _Pos, bool _Val = true); //将位数组转换成整数,最低位放在[0]位置 //例如数组中存放的1011,则返回13,而不是返回11 unsigned long long to_ullong() const; bool test(size_t _Pos) const; bool any() const; bool none() const; bool all() const; std::string to_string() const; public: //直接整数转化成二进制,赋值给Bitset,最高位放在[0]位置 Bitset& operator = (const Bitset& b); //直接整数转化成二进制,赋值给Bitset,最高位放在[0]位置 Bitset& operator = (unsigned long long ull); //返回指定位置的值,如果pos大于位数组长度,自动拓展 bool operator [] (const size_t pos); //测试两个Bitset是否相等 bool operator == (const Bitset & b); bool operator != (const Bitset & b); Bitset operator<>(size_t _Pos) const; bool operator > (const Bitset & c)const; bool operator < (const Bitset & c)const; Bitset& operator &=(const Bitset& _Right); Bitset& operator|=(const Bitset& _Right); Bitset& operator^=(const Bitset& _Right); Bitset& operator<>=(size_t _Pos); public: Bitset& flip(size_t _Pos); Bitset& flip(); //将高位与低位互相,如数组存放的是1011,则本函数执行后为1101 Bitset& reverse(); //返回左边n位,构成新的Bitset Bitset left(size_t n) const; //返回右边n位,构成新的Bitset Bitset right(size_t n) const; //判断b包含的位数组是否是本类的位数组的自串,如果是返回开始位置 size_t find (const Bitset & b) const; size_t find(unsigned long long & b) const; size_t find(const char * b) const; size_t find(const std::string & b) const; //判断本类的位数组是否是b的前缀 bool is_prefix(unsigned long long & b) const; bool is_prefix(const char * b) const; bool is_prefix(const std::string & b) const; bool is_prefix(const Bitset & b) const; void clear(); void resize(size_t newSize); void reset(const unsigned char * flags, size_t s); void reset(unsigned long long _Val); void reset(const char * _Str); void reset(const std::string & _Str, size_t _Pos, size_t _Count); //左移动n位,返回新的Bitset //extendBits=false "1101" 左移动2位 "0100"; //extendBits=true "1101" 左移动2位 "110100"; Bitset leftShift(size_t n,bool extendBits=false)const; //右移动n位,返回新的Bitset //extendBits=false "1101" 右移动2位 "0011"; //extendBits=true "1101" 右移动2位 "001101"; Bitset rightShift(size_t n, bool extendBits = false) const; public: virtual uint32_t getByteArraySize(); // returns the size of the required byte array. virtual void loadFromByteArray(const unsigned char * data); // load this object using the byte array. virtual void storeToByteArray(unsigned char ** data, uint32_t& length) ; // store this object in the byte array. };

64,639

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧