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

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.

Related

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

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

How do i "subtract" a character from a string in c++ [duplicate]

This question already has answers here:
Remove last character from C++ string
(12 answers)
Closed 8 months ago.
Lets say I have a string variable with the value "bananas" in it. I want to subtract the last letter so the string becomes "banana". I am quite a newbie, so I dont even know how to tackle this.
Just use the pop_back() function.
Try this code, it 'subtracts' the last character:
std::string str = "bananas";
str.pop_back();

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

What is R"===(? [duplicate]

This question already has an answer here:
What does 'R' mean in the context of string literals?
(1 answer)
Closed 4 years ago.
Specifically in this file
https://github.com/fireice-uk/xmr-stak/blob/master/xmrstak/backend/amd/amd_gpu/opencl/cryptonight.cl
The first line is this and the end ends with
)==="
And it seems to be sprinkled within.
Could someone explain what this is and does? I'm having a difficult time googling this.
The format for the raw-string literals[2] is: R"delimiter( raw_characters )delimiter", so === is the delimiter in R"===( and line 243 in your link

How to copy part of CString to a new one? [duplicate]

This question already has answers here:
mfc copy certain sections of a CString
(2 answers)
Closed 7 years ago.
I got a CString str
I want to copy str[3]~ str[5] to a brand new one.
I tried C++ char* 's method, not compatible.
So what is the right way do this in VC++
Thank you,
Have you tried using the the MID function?
str.Mid( 3, 3 ) should give you the substring you are looking for.
Updated reference: http://msdn.microsoft.com/en-us/library/b4c90k3d(v=vs.80).aspx