I expect to output:
acej
which works fine with this algorithm but there is a problem with outputting the result and this problem causes stack to overflow.
How do I fix it?
#include <iostream>
#include <vector>
std::vector<char> charAt(std::vector<char> str)
{
std::vector<char> result;
result.resize(str.size());
for (int i = 0; i < str.size(); i++)
{
if (i % 2 == 0)
{
for (int j = 0; j < str.size(); j++)
{
if (result[j] == '\0')
{
result[j] = str[i];
break;
}
}
}
}
return result;
}
std::ostream& operator<<(std::ostream& stream, std::vector<char> vector)
{
stream << "Vector: " << vector << std::endl;
return stream;
}
int main() {
std::vector<char> foo = { 'a', 'b','c','d','e','f','j' };
std::vector<char> bar = charAt(foo);
std::cout << bar << std::endl;
}
stream << "Vector: " << vector << std::endl;What did you expect this to do?'\0'?jbut then looksjup in the wrong container.