4

I want to create an array of Bitset .Binary Bitset(example "100","1010",etc) After that I want to input from user and store in the the Bitset . I have tried the following line but it says error.

#include<bits/stdc++> 
using namespace std;
int main() 
{ 
  int n,i;
  string bit_string; 
  cin>>n  // size of Bitset array.
  bitset<8> brr[n];//  
  for(i=0;i<n;i++)
  {
    cin>>bit_string;
    brr[i](bit_string);
  }



  return 0; 
}  

I want to create n Bitset each of size 8 bits.Where n is given by user. my input is binary string like. "110010","001110" please help

3
  • Not directly related to your question but worth reading anyway: Why should I not #include <bits/stdc++.h>? Commented Apr 2, 2020 at 6:50
  • 1
    cin>>n; bitset<8> brr[n]; is a variable-length array and not supported by C++. Use a std::vector Commented Apr 2, 2020 at 6:52
  • What is the error it says? Commented Apr 2, 2020 at 8:00

2 Answers 2

3

The error ocurrs because you are trying to creat a C-style array using n which is not compile-time constant. It's not possible to creat a C-style array without being n known at compile time.

The following is a good way to do what you want

Creat a std::vector<std::bitset<8>> to hold your bitset<8>s, as follows.

Note that the code ignores the excess of characters in strings iput like "111111110" (makes it "11111111") and treats any character except '1' as if it were '0' and if the input string is less than 8 characters, the code adds zeros by the default of the bitsets

#include <vector>
#include <bitset>
#include <iostream>
int main() {
    int n, i;
    std::string bit_string;
    std::cout << "Enter the size";
    std::cin >> n; // size of Bitset array.
    std::vector<std::bitset<8>> brr(n);//
    for (i = 0; i < n; i++) {
        std::cin >> bit_string;
        for (int j{}; j < bit_string.size() && j < 8; ++j) {
            brr[i][j] = (bit_string[j] == '1') ? 1 : 0;
        }
    }
    //To test

    for(auto const& el :brr)
    {
        for(int i{}; i < 8;)
            std::cout << el[i++];
        std::cout<<"\n";
    }
}

See Why is "using namespace std;" considered bad practice? and Why should I not #include <bits/stdc++.h>?

Sign up to request clarification or add additional context in comments.

Comments

2

For dynamic count of the objects , Please try vector<> instead of array[]


#include<bits/stdc++> 
using namespace std;
int main()
{
    int n, i;
    string bit_string;
    cin >> n;  // size of Bitset array.

    vector<bitset<8>> arr;      //size()=>0
    arr.resize(n);      //size()=>n

    for (i = 0; i < n; i++)
    {
        cin >> bit_string;
        bitset<8>& br = arr[i]; //get the i of n

        int maxlen = 8;
        if (bit_string.size() <= 8)
            maxlen = bit_string.size();
        else
            cout << "warning invalid len " << bit_string.size() << " of " << bit_string << endl;

        for (int j = 0; j < maxlen; j++)
        {
            if (bit_string[j] == '1')
                br.set(j, true);
        }

        //cout << endl << br << endl;   //output test
    }

    return 0;
}

If you still want to use array , please try this way


#include<bits/stdc++> 
using namespace std;
int main()
{
    int n, i;
    string bit_string;
    cin >> n;  // size of Bitset array.

    bitset<8>* arr = new bitset<8>[n];

    for (i = 0; i < n; i++)
    {
        cin >> bit_string;
        bitset<8>& br = arr[i]; //get the i of n

        int maxlen = 8;
        if (bit_string.size() <= 8)
            maxlen = bit_string.size();
        else
            cout << "warning invalid len " << bit_string.size() << " of " << bit_string << endl;

        for (int j = 0; j < maxlen; j++)
        {
            if (bit_string[j] == '1')
                br.set(j, true);
        }

        //cout << endl << br << endl;   //output test
    }

    delete[] arr;   //IMPROTAND , delete the array and free memory

    return 0;
}

1 Comment

I posted another solution for you . use bitset<8>* arr = new bitset<8>[n];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.