使用 std::vector 导致未知输出 C++
Use of std::vector results in unknown output C++
我不明白为什么我收到的输出不仅仅是“00110”,还有其他乱码。不确定我的矢量 push_back 有什么问题。这对我来说绝对有意义。如果我将其更改为 std::string 实现,它会给出正确的输出。但在这种情况下,我需要使用 vector 来正确封装对象的状态。我已经调试了几个小时了,但仍然找不到原因。希望任何人都能提供帮助!谢谢!注意:main()不可修改。
#include <iostream>
#include <vector>
template<size_t NumBits>
class bitsetts
{
private:
static const unsigned int NO_OF_BITS = CHAR_BIT * sizeof(int); //32 bits
static const unsigned NumBytes = (NumBits - 7) /8;
unsigned char array[NumBytes];
public:
bitsetts() { }
void set(size_t bit, bool val = true) {
if (val == true)
{
array[bit] |= (val << bit );
}
else
{
array[bit] &= (val << bit );
}
}
bool test(size_t bit) const {
return array[bit] & (1U << bit );
}
const std::string to_string()
{
std::vector<char> str;
for (unsigned int i=NumBits; i-- > 0;)
str.push_back('0' + test(i));
return str.data();
}
friend std::ostream& operator<<(std::ostream& os, const bitsetts& ob)
{
for (unsigned i = NumBits; i-- > 0;)
os << ob.test(i);
return os << '\n';
}
};
int main()
{
try
{
bitsetts<5> bitsetts;
bitsetts.set(1);
bitsetts.set(2);
const std::string st = bitsetts.to_string();
if (st != "00110")
{
std::cout << st << std::endl;
throw std::runtime_error{ "-" };
}
}
catch (const std::exception& exception)
{
std::cout << "Conversion failed\n";
}
}
您正在用 char
值填充 std::vector
,然后使用 std::string
构造函数从原始 char
数据构建 std::string
单个 const char*
参数。该构造函数期望 char
数据以空值终止,但您没有将空终止符推入向量,这就是为什么您在 std::string
.
因此,要么将空终止符推入 vector
,例如:
const std::string to_string()
{
std::vector<char> str;
for (unsigned int i=NumBits; i-- > 0;)
str.push_back('0' + test(i));
str.push_back('[=10=]'); // <-- add this!
return str.data();
}
或者,使用不同的 std::string
构造函数,可以将向量的 size()
作为参数,例如:
const std::string to_string()
{
std::vector<char> str;
for (unsigned int i=NumBits; i-- > 0;)
str.push_back('0' + test(i));
return std::string(str.data(), str.size()); // <-- add size()!
}
附带说明:您的 to_string()
方法应标记为 const
,例如:
const std::string to_string() const
这将允许您在 operator<<
中使用 to_string()
,例如:
friend std::ostream& operator<<(std::ostream& os, const bitsetts& b)
{
return os << b.to_string() << '\n';
}
我不明白为什么我收到的输出不仅仅是“00110”,还有其他乱码。不确定我的矢量 push_back 有什么问题。这对我来说绝对有意义。如果我将其更改为 std::string 实现,它会给出正确的输出。但在这种情况下,我需要使用 vector 来正确封装对象的状态。我已经调试了几个小时了,但仍然找不到原因。希望任何人都能提供帮助!谢谢!注意:main()不可修改。
#include <iostream>
#include <vector>
template<size_t NumBits>
class bitsetts
{
private:
static const unsigned int NO_OF_BITS = CHAR_BIT * sizeof(int); //32 bits
static const unsigned NumBytes = (NumBits - 7) /8;
unsigned char array[NumBytes];
public:
bitsetts() { }
void set(size_t bit, bool val = true) {
if (val == true)
{
array[bit] |= (val << bit );
}
else
{
array[bit] &= (val << bit );
}
}
bool test(size_t bit) const {
return array[bit] & (1U << bit );
}
const std::string to_string()
{
std::vector<char> str;
for (unsigned int i=NumBits; i-- > 0;)
str.push_back('0' + test(i));
return str.data();
}
friend std::ostream& operator<<(std::ostream& os, const bitsetts& ob)
{
for (unsigned i = NumBits; i-- > 0;)
os << ob.test(i);
return os << '\n';
}
};
int main()
{
try
{
bitsetts<5> bitsetts;
bitsetts.set(1);
bitsetts.set(2);
const std::string st = bitsetts.to_string();
if (st != "00110")
{
std::cout << st << std::endl;
throw std::runtime_error{ "-" };
}
}
catch (const std::exception& exception)
{
std::cout << "Conversion failed\n";
}
}
您正在用 char
值填充 std::vector
,然后使用 std::string
构造函数从原始 char
数据构建 std::string
单个 const char*
参数。该构造函数期望 char
数据以空值终止,但您没有将空终止符推入向量,这就是为什么您在 std::string
.
因此,要么将空终止符推入 vector
,例如:
const std::string to_string()
{
std::vector<char> str;
for (unsigned int i=NumBits; i-- > 0;)
str.push_back('0' + test(i));
str.push_back('[=10=]'); // <-- add this!
return str.data();
}
或者,使用不同的 std::string
构造函数,可以将向量的 size()
作为参数,例如:
const std::string to_string()
{
std::vector<char> str;
for (unsigned int i=NumBits; i-- > 0;)
str.push_back('0' + test(i));
return std::string(str.data(), str.size()); // <-- add size()!
}
附带说明:您的 to_string()
方法应标记为 const
,例如:
const std::string to_string() const
这将允许您在 operator<<
中使用 to_string()
,例如:
friend std::ostream& operator<<(std::ostream& os, const bitsetts& b)
{
return os << b.to_string() << '\n';
}