【发布时间】:2020-08-02 20:30:07
【问题描述】:
我正在打印一个字符串(由 stl 中的 bitset 创建),然后直接打印该字符串并使用循环为什么输出有差异?
#include<iostream>
#include<bitset>
using namespace std;
int main()
{
const int m=16;
int n;
int arr[m];
cin>>n;
bitset<m>bt(n);
cout<<bt<<endl;
for(int i=0;i<m;i++)
{
cout<<bt[i];
}
}
输入:
995
输出:
0000001111100011 //打印字符串
1100011111000000 //循环打印
一个的输出与另一个相反。
我不明白为什么会这样?
【问题讨论】:
-
这都是因为
std::bitset::to_string返回字符串,其中第一位表示最高en.cppreference.com/w/cpp/utility/bitset/to_string -
你之前的问题(stackoverflow.com/a/61318994/104774)已经回答了这个问题,我想这还不够清楚。
-
之前我不知道位图的索引是从最右边的位开始的。因此我在那里无法理解。感谢您抽出宝贵的时间来解答我的疑问。