This question already has answers here:
How do I use arrays in C++?
(5 answers)
Closed 4 years ago.
Sorry for the dumb question (I'm a total C++ noob), but what does
int x[101010];
declare? Is it list, a vector et cetera? And what is the meaning of 101010? I have never seen a declaration like this.
At block scope, int x[101010]; declares a uninitialised array with 101010 elements.
At global scope, the effect is similar but the elements are set to 0.
Note that if you had written int x[010101];, then you would have created 4161 elements as a leading 0 denotes an octal literal in C++.
In C++, a good rule of thumb is to use a std::vector unless you have a good reason not to.
Related
This question already has answers here:
Reference referring to multiple objects, how is it possible? [duplicate]
(2 answers)
Why reference const can be re-assigned in for-statement?
(3 answers)
Closed 21 days ago.
for (int& i : array) {
//do something on i
}
Does this count as "reseating reference i to different objects"?
This, appearently, let i refers to array[0] in first iteration, array[1] in second iteration, ..., and so forth.
Edit 1 in response to comment: I know exact what happens in implementation level. I used compiler explorer to see what does this compile to and I know that this looks completely same as other type of iteration (some level of optimization is assumed). This question is more about on the language level, not implementation.
This question already has answers here:
Strange numbers when array is not initialized in C++ [duplicate]
(3 answers)
What happens to a declared, uninitialized variable in C? Does it have a value?
(9 answers)
Closed 1 year ago.
I have tried a simple code and found a strange error(wrt me)
it is something like s[10]
now i have put some number of values into this array.t
like s[0]=0;s[1]=1.s[2]=2 and all others are empty.
now i put a for loop to see how it goes and to my surprise after index 2, some random numbers popping up in output and idk why. It should be null and the loop should have exited but here, it gives me some output like 01248766575...
why is it happening? pls do help me if u know
This question already has answers here:
What are differences between std::string and std::vector<char>?
(5 answers)
C++: char test[100] vs array<char, 100> vs string
(4 answers)
Closed 7 years ago.
It's known to everyone of us that we should prefer string class in C++ for all string applications due to the many special functions they perform & their ability to grow & reduce dynamically. What string is for characters, vector is for other data types & classes because it shows great performance.
However is there any situation where we would need to prefer vector<char> (which I see seldom) over string ?
I'd use vector<char> only if I explicitly intent to store an array of char values, which is not a string. E.g. if for some reason I'd collect all the characters used somewhere in a specific text, the result might be a vector<char>.
To be clear: it is all about expressing the intent.
To put it briefly: if you're storing text, then string, otherwise vector<char>.
This question already has answers here:
Multicharacter literal in C and C++
(6 answers)
What do single quotes do in C++ when used on multiple characters?
(5 answers)
Closed 9 years ago.
C++ seems to allow up to four characters to be held in single quotes, such as:
char c = 'abcd';
but at runtime, only the last value ('d') seems to be actually stored away. This behavior seems to happen for pairs of two, three, or four (at five the compiler finally calls uncle). But what's the deal with this design? I don't really see the logic in it.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What does the word “literal” mean?
Often when reading literature about C++, I encounter the word "literal". It is a bit unclear to me what exactly this term means in C++.
A literal is some data that's presented directly in the code, rather than indirectly through a variable or function call.
Here are some examples, one per line:
42
128
3.1415
'a'
"hello world"
The data constituting a literal cannot be modified by a program, but it may be copied into a variable for further use:
int a = 42; // creates variable `a` with the same value as the literal `42`
This concept is by no means unique to C++.
The term "literal" comes from the fact that you've written data literally into your program, i.e. exactly as written, not "hidden" behind a variable name.
Wikipedia gives you quickly this about literals.
In your C or C++ source code, Things like 1234, nullptr (in recent C++), "abcd" are literals.