My program takes in a vector std::vector<std::string> vector
and a character char separator
and returns a string with all of the strings added together between the separator character. The concept is: vector[0] + separator + vector[1] + separator
The Code
std::string VectorToString(std::vector<std::string> vector, char separator)
{
std::string output;
for(std::string segment : vector)
{
std::string separator_string(&separator);
output += segment + separator_string;
}
return output;
}
int main()
{
std::vector<std::string> vector = {"Hello", "my", "beautiful", "people"};
std::cout << VectorToString(vector, ' ');
}
My expected output is Hello my beautiful people
However the output is:
Hello �����my �����beautiful �����people �����
What I have found is that something is wrong with the character, specifically its pointer: std::cout << &separator;
-> �ƚ��
. However if I do like this: std::cout << (void*) &separator;
-> 0x7ffee16d35f7
. Though I don't really know what (void*)
does.
Question:
1.What is happening?
2.Why is it happening?
3.How do I fix it?
4.How do I prevent it from happening in future projekts?
Aucun commentaire:
Enregistrer un commentaire