The original code prints the msg in the brackets, but the second code prints just Hey there. Why is this? I started C++ a few days ago. Have experience in Python and some C#.
Original code:-
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the C++
extension!"};
for (const string& word : msg)
{
cout << word << " ";
}
cout << endl;
}
This is the new code:-
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the C++
extension!"};
cout << "Hey there" << endl;
}
>Solution :
In the first code, you are populating a vector with string elements, and then using a range-for loop to iterate through that vector printing out its elements.
In the second code, you are still populating the vector, but you are not iterating through the vector at all, you are just printing out a separate message instead.