Reputation: 2546
Generating binary representation of numbers from 0 to 255. This is causing segmentation fault. Kindly enlighten.
vector<bitset<7>> vb;
for (i = 0; i < 256; i++)
{
bitset<7> b(i);
vb[i] = b;
}
//print
for(i=0;i<256;i++){
cout<<vb[i]<<"\n";
Upvotes: 0
Views: 64
Reputation: 117856
When you declare your vector it is empty
vector<bitset<7>> vb;
you can initialize it with a given size
vector<bitset<7>> vb(256);
Otherwise simply assigning to the empty vector will be writing out of bounds since it has not (re)allocated memory for the elements you are trying to access
vb[i] = b;
Upvotes: 2
Reputation: 73
Your vector is of size 0. Either use
vb.push_back(b);
or initialize a size like:
vector<bitset<7>> vb(256);
Upvotes: 1