【发布时间】:2013-06-16 12:04:22
【问题描述】:
我正在尝试创建一个表示布尔数组的 BoolArray 类,而不是为每个布尔变量保存 1 个布尔值。它使用 1 个字符变量来表示 8 个使用位的布尔值,并节省内存。我想使用运算符 [] 使其像普通数组一样方便。
我可以使用 arr[5] 并为第五位返回一个布尔值。我的问题是分配了一点。我无法引用一位,因此我需要创建一个执行该任务的函数。 我的问题是是否可以使用运算符重载来控制数组样式分配。我的解决方案是使用另一个类(MyBoolean)并将其作为对 BoolArray 运算符 [] 的引用返回。 MyBoolean 重载 operator= 并更改 BoolArray 的位。有没有更简单的方法可以重载 operator[]= 或类似的东西?
#include <iostream>
#include <tgmath.h>
using namespace std;
string int_to_binary_string(int number)
{
if ( number == 0 ) return "0";
if ( number == 1 ) return "1";
if ( number % 2 == 0 )
return int_to_binary_string(number / 2) + "0";
else
return int_to_binary_string(number / 2) + "1";
}
class MyBoolean;
class BoolArray {
private:
unsigned char* arr;
int size;
void setBit(int bit_number,bool value);
public:
explicit BoolArray(int size);
~BoolArray();
MyBoolean operator[](unsigned int index);
int getSize() const;
friend class MyBoolean;
};
// ***************************** MyBoolean *********************************
class MyBoolean {
private:
bool value;
int bit_number;
BoolArray* bool_array;
public:
// --- Constructor ---
MyBoolean(bool value,int bit_number,BoolArray* bool_array) :
value(value),bit_number(bit_number),bool_array(bool_array)
{}
// --- cast ---
operator bool() const{
return value;
}
MyBoolean& operator=(bool new_value) {
value = new_value;
bool_array->setBit(bit_number,new_value);
}
};
// ***************************** BoolArray *****************************
void BoolArray::setBit(int bit_number,bool value) {
int index = floor((double)bit_number/8);
bit_number -= index*8;
cout << "trying to set " << bit_number << " to " << value << endl;
if(value==true) {
int binary_num = pow(2,bit_number);
cout << "arr[index]: " << int_to_binary_string(arr[index]) << " - binary_num: " << int_to_binary_string(binary_num);
arr[index] = arr[index] | binary_num;
cout << " - after: " << int_to_binary_string(arr[index]) << endl;
}
else {
int binary_num = 0 | 255;
int binary_num_2 = pow(2,bit_number);
binary_num = binary_num^binary_num_2;
arr[index] = arr[index] & binary_num;
}
}
// --- Constructor ---
BoolArray::BoolArray(int size) :
size(size)
{
size = ceil((double)size/8);
arr = new unsigned char[size];
}
// --- Destructor ---
BoolArray::~BoolArray() {
delete[] arr;
}
// --- operator[] ---
MyBoolean BoolArray::operator[](unsigned int index) {
if(index>size-1)
throw "error";
int arr_index = floor((double)index/8);
int bit_number = index - arr_index*8;
unsigned int binary_num = pow(2,bit_number);
int value= false;
if((arr[arr_index] & binary_num)>0)
value = true;
MyBoolean my_bool(value,index,this);
return my_bool;
}
// --- size ---
int BoolArray::getSize() const {
return size;
}
ostream& operator<<(ostream& os, MyBoolean b) {
os << (bool) b;
return os;
}
ostream& operator<<(ostream& os,BoolArray& arr) {
for(int i=0;i<arr.getSize();i++)
os << arr[i] << "->";
return os;
}
int main() {
BoolArray arr(12);
arr[0] = true;
arr[1] = false;
arr[2] = true;
arr[3] = false;
arr[4] = true;
arr[5] = false;
arr[6] = true;
arr[7] = false;
arr[8] = true;
arr[9] = false;
arr[10] = true;
arr[11] = false;
cout << arr;
return 0;
}
【问题讨论】:
-
为什么不用位域?
-
如果我做对了,你是在问如何重载 [] 和 =。您应该在任何体面的重载教程中找到这一点,请参见例如en.wikibooks.org/wiki/C%2B%2B_Programming/Operators/…。 Stroustrup 的书也提供了一个很好的例子。
-
手动编写
std::bitset是学生的问题吗?查看文档并尝试自己实现这些文档。另外,为了便携性,在<climits>中使用CHAR_BIT而不是8。 -
标准库为 bool 类型提供了 std::vector 的特化,它针对空间效率进行了优化。为什么你不使用它?
-
你需要一些“代理”魔法......但是看看
boost::dynamic_bitset或臭名昭著的std::vector<bool>(如果你不需要动态大小,还有std::bitset)。跨度>