#include <bits/stdc++.h>
#include <string>
using namespace std;
vector<string> func(string mag) {
vector<string> s1;
string temp1;
string temp2;
temp1[0] = 'a';
temp1[1] = 'b';
temp1[2] = 'c';
temp2 += "xyz";
s1.push_back(temp1);
s1.push_back(temp2);
return s1;
}
int main() {
string st;
vector<string> xyz;
xyz = func(st);
for (int a = 0; a < xyz.size(); a++)
cout << xyz[a] << ',';
}
The output of the above code is ,xyz,
even though I was expecting abc,xyz,
. The string temp1
, added to the vector, is not printed.
The string temp1
is non-empty, and printing it out gives output abc
, but pushing it to the vector s1
and printing out the vector, the string temp1
is not printed, while string temp2
, where "xyz"
is added to it as temp2+="xyz"
, is added to the vector and is printed when the contents of the vector are printed.
Can someone explain to me why the string temp1
is not appearing in the vector contents?