C++ Bitset Library - to_string() Function


Advertisements

Description

The C++ function std::bitset::test() Tests whether Nth bit is set or not.

Description

The C++ function std::bitset::to_string() converts bitset object to string object.

Declaration

Following is the declaration for std::bitset::to_string() function form std::bitset header.

C++98

template <class charT, class traits, class Alloc>
basic_string<charT,traits,Alloc> to_string() const;

C++11

template <class charT = char,
          class traits = char_traits<charT>,
          class Alloc = allocator<charT>>
          basic_string<charT,traits,Alloc> to_string (charT zero = charT('0'),
          charT one  = charT('1')) const;

Parameters

None

Return value

Returns string representation of bitset object.

Exceptions

No change in bitset if exception is thrown.

Example

The following example shows the usage of std::bitset::to_string() function.

#include <iostream>
#include <bitset>

using namespace std;

int main(void) {

   bitset<4> b;

   string s = b.to_string();

   cout << s << endl;

   return 0;
}

Let us compile and run the above program, this will produce the following result −

0000
bitset.htm
Advertisements