What is the difference between deque.at(0) vs deque[0] [duplicate] - c++

This question already has answers here:
vector::at vs. vector::operator[]
(8 answers)
Closed 7 months ago.
So i have this queue
deque<int> deq1(2,10);
I Have accessed the element using 2 way and both of them return the same value
cout<<deq1[0];
cout<<deq1.at(0);
why did them make a special function to do the same thing or is one way better than the other?

The only difference is that the function at throw an exception if the index is out of range while the operator[] doesn't make any check. You can see the documentation here
https://en.cppreference.com/w/cpp/container/deque/at
https://en.cppreference.com/w/cpp/container/deque/operator_at

Related

Does using reference in ranged for loop count as "reseating reference to a different object"? [duplicate]

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.

Can I partly bind a tuple using a structured binding with fewer elements than the size of the tuple? [duplicate]

This question already has answers here:
C++17: Keep only some members when tuple unpacking
(4 answers)
std::ignore with structured bindings?
(3 answers)
Closed 11 months ago.
I'm surprised I couldn't find this question asked anywhere. Also it would seem this language feature would not be hard to implement (may be wrong though).
const auto [a, b] = function_returning_triplet(); // error: only 2 names provided for structured binding

A strange output in C++ [duplicate]

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

Explain what for (char c : str) does? [duplicate]

This question already has answers here:
'colon' and 'auto' in for loop c++? need some help understanding the syntax
(3 answers)
colon in for loop in C++
(1 answer)
Closed 3 years ago.
I am asking since I couldnt immediately find an answer on google, I know the answer is simple.
what does for (char c : str) {} do in a for loop?
Thank you!
It iterates the individual characters of str, copying each one to the (local) variable c for use in each iteration of the loop.

Mysterious C++ declaration [duplicate]

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.