What is the output of the following snippet? #include <iostream> #include <string> #include <vector> using namespace std; int main() { vector<string> t = { "alpha", "beta", "gamma" }; int *cnt = new int[3], *p = &cnt[0]; for(int i = 0; i < t.size(); i++) *p++ = t[i].length(); cout << cnt[0] << cnt[1] << cnt[2]; delete [] cnt; } Compilation fails 012 111 545
Question
What is the output of the following snippet?
#include <iostream> #include <string> #include <vector>
using namespace std;
int main() { vector<string> t = { "alpha", "beta", "gamma" }; int *cnt = new int[3], *p = &cnt[0];
for(int i = 0; i < t.size(); i++)
*p++ = t[i].length();
cout << cnt[0] << cnt[1] << cnt[2];
delete [] cnt;
}
Compilation fails
012
111
545
Solution 1
The output of the given C++ code snippet will be "545".
Here's the step-by-step explanation:
-
A vector of strings
tis declared and initialized with three strings: "alpha", "beta", "gamma". -
An integer pointer
cntis declared and a new integer array of size 3 is allocated on the heap. The pointercntpoints to the first element of this array. -
Another integer pointer
pis declared and initialized to point to the same location ascnt(i.e., the first element of the array). -
A for loop is used to iterate over the vector
t. For each iteration, the length of the current string is calculated using thelength()function and this value is assigned to the location pointed to byp. The pointerpis then incremented to point to the next location in the array. -
After the loop, the lengths of the strings "alpha", "beta", "gamma" have been stored in the array pointed to by
cnt. These lengths are 5, 4, and 5 respectively. -
The
coutstatement prints the values in the array pointed to bycnt, which are 5, 4, and 5. -
Finally, the dynamically allocated array is deallocated using the
delete[]operator.
Solution 2
The output of the given C++ code snippet will be "545".
Here's the step-by-step explanation:
-
A vector of strings
tis declared and initialized with three strings: "alpha", "beta", "gamma". -
An integer pointer
cntis declared and a new integer array of size 3 is allocated on the heap. This array is used to store the lengths of the strings in the vectort. Another integer pointerpis declared and initialized to point to the first element of thecntarray. -
A for loop is used to iterate over the vector
t. For each string in the vector, its length is calculated using thelength()function and this value is stored in the location pointed to by the pointerp. The pointerpis then incremented to point to the next location in thecntarray. -
After the loop, the lengths of the strings "alpha", "beta", and "gamma" are stored in the
cntarray. These lengths are 5, 4, and 5 respectively. -
The
coutstatement prints the contents of thecntarray, which are the lengths of the strings. So, "545" is printed to the console. -
Finally, the
delete[]operator is used to deallocate the memory that was previously allocated for thecntarray. This is a good practice to prevent memory leaks.
Similar Questions
What is the output for the following code?
What does the following code snippet do?
What is the output of the following code in Python?
What is the output for the following code?Note: This question helps in clearing conceptual technical interviews for service-based companies.
What will be the output of the following PHP code?
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.