This question already has answers here:
Define bitset size at initialization?
(7 answers)
Closed 5 years ago.
Now I've this code:-
The code is about taking in an integer and providing its binary form in the given number of bits.
#include <iostream>
#include <bitset>
using namespace std;
int main(){
//creating instance using bitset (6 bit). here you can specify the length such as 8,16,32,64...
int n=5;
bitset< 6 > btFlaged;
//assigning integer value to instance
btFlaged = 7;
//print bit string in the string
for(int i=btFlaged.size()-1;i>-1;i--)
{
cout <<btFlaged.test(i);
}
}
How do I use an integer(E.g. n) in the place of '6' so that a value entered at run-time can be used in the code?
I've done some research on the net and I know that bitset needs a value at compile time and so instead of bitset, I should use vector bool but I don't know how I should incorporate that in the program?
If any of you guys can tell me how to use vector or if you have an altogether different theory on how to the the task done, please do share.
Also I cant use boost:dynamic_bitset as the code is going to be judged by an online judge which may not have the seperate header file.
a std::bitset's size must be set at compile time as it is a template parameter. If you need a dynamic bitset you can look at boost:dynamic_bitset
Related
This question already has answers here:
Accessing an array out of bounds gives no error, why?
(18 answers)
Closed 10 months ago.
When creating a pointer using new keyword or <stdlib.h>'s malloc, and put its size 0; what ever is the index number it doesnt give an error and works. ( i am not sure if this works if the size bigger then 0, because some times when i allocate a memory block and place an element outside its range program crashes).
My question : is this a c++ thing? Or just from my compiler? Is it safe to use it for arrays i don't want to limit it with specific size. Because i am thinking to use this in my game to make unlimited world generation and save it ( Not all terrain just the breakable and placable objects)
#include <iostream>
using namespace std;
int main() {
int* x = new int[0];
x[100] = 0;
cout << x[100];
return 0;
}
is this a c++ thing? Or just from my compiler?
It's a bug in your program. The behaviour of the program is undefined.
Is it safe to use it
It is not safe. The program is broken.
This question already has answers here:
Are variable length arrays there in c++?
(2 answers)
Array[n] vs Array[10] - Initializing array with variable vs numeric literal
(1 answer)
Closed 1 year ago.
Given that user input is taken at runtime but array is created during compile time. How and why does this piece of code work?
#include <iostream>
using namespace std;
int main(){
int n;
cin>>n;
int arr[n]; //Why can I use this statement to create an array?
}
It's a non-standard extension, supported by gcc and clang.
It works by allocating space on the stack at the point the array is declared, a bit like alloca.
The memory allocated is automatically freed (by adjusting the stack pointer) when the function returns (or arr goes out of scope).
This question already has answers here:
Array[n] vs Array[10] - Initializing array with variable vs numeric literal
(1 answer)
Why aren't variable-length arrays part of the C++ standard?
(10 answers)
Closed 2 years ago.
I'm very new to C++, but I've been digging around and the general answer is no, c++11 does not support variable sized arrays, since array sizes need to be constant expressions.
However, I've tried this code on XCode 11 (C++11):
#include <iostream>
using namespace std;
int main(int argc, const char * argv[]) {
unsigned long arrayS;
cin >> arrayS;
bool a[arrayS];
return 0;
}
and it totally works. When I set a breakpoint at
bool a[arrayS];
I can see that the array a has arrayS elements. I have verified this with
*(&a + 1) - a
and it shows that the number of elements in a is arrayS.
Does c++11 support variable sized arrays? Or, is it only working for me because of the compiler I'm using?
I'm asking this question because I'm unsure of what compiler my friend is using and want to send the code to him to run.
Any help is much appreciated.
That is non standard C++. Some compilers accept it, but if you want to be portable you should stick with standard C++.
In c++, array size should be supplied at compile time. For variable sized arrays use std::vector
This question already has answers here:
Array size at run time without dynamic allocation is allowed? [duplicate]
(8 answers)
Closed 6 years ago.
I am reading C++ Primer plus on arrays, and it says the following
typeName arrayName[arraySize];
//Arraysize cannot be a variable whose value is set while the program is running"
However, I wrote a program
#include <iostream>
using namespace std;
int main()
{
int n;
cin>>n;
int array[n];
for(int i=0; i<n; i++)
{
cout<<array[i]<<endl;
}
}
And it works fine, I am able to set the size of the array during run time. I am not getting any compilation errors, or run time crashes.
Can someone explain what is happening?
Thanks
Some compilers like g++ allow the use of C variable length arrays and will happily compile the code without any warnings or error. This is not standard and is a compiler extension.
If you need an "array" and you do not know what the size will be until run time then I suggest you use a std::vector You can use it as a direct replacement to an array but it allows run time sizing and it offers a lot of other useful features.
This question already has answers here:
C++: Why does int array[size] work?
(3 answers)
Closed 8 years ago.
#include <iostream>
using namespace std;
int main(){
int n;
cout<<"Enter the size :";
cin>>n;
int array[n]; // I've worked some outputs and it works
return 0;
}
Is this some kind of dynamic allocation?
Why doesn't it even gives an error for 'n' to be a "const"?
Also, writing cout << array[n+5]; doesn't result in an compile time or runtime error.
I'm using Dev-C++.
Apparently one can declare variable length arrays in C99, and it seems GCC accepts then for C++ also.
Variable-length automatic arrays are allowed in ISO C99, and as an
extension GCC accepts them in C90 mode and in C++. These arrays are
declared like any other automatic arrays, but with a length that is
not a constant expression.
You learn something every day .. I hadn't seen that before.