簡體   English   中英

為 Bitset 賦值

[英]Assign a value to Bitset

我很想知道為什么以下代碼有效,根據 bitset 模板 class。您可以通過構造函數為 bitset(int 或二進制表示形式的字符串)分配一個值,但之后不能。 但在這里您可以看到 integer 的顯式分配工作正常。

#include <iostream>
#include <bitset>
#include <string>
using namespace std;

int main()
{
    bitset<8> b(string("101"));
    cout << b.to_ullong()<<"\n";
    b= 145;
    cout << b<<"\n";
    return 0;
}

這個問題也可能是相關的。 初始化后如何從字符串中分配位集值

位集的非字符串構造函數是隱式的

如果將它們聲明為顯式的,則確實需要編寫

b = bitset<8>(145);

令人困惑的是std::bitset沒有明確定義operator= 但是,編譯器會生成一個(請參閱問題)。 您實際上可以使用cppinsight檢查。 這意味着,結合接受的答案中提到的隱式構造函數,您的代碼可以正常工作。 您可以在 cppinsight 示例中看到構造函數調用和后續賦值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM