Having the following objects:
const char a[]{"abcdefghij"}; // Arreglo de caracteres (longitud 11)
const std::string s{"abcdefghij"}; // basic_string<char> estandar
I expected these loops to behave the same:
// 1) Muestra NADA, esperaba "jihgfedcba"
for (auto begin = std::rbegin(a), end = std::rend(a); begin != end; ++begin)
std::cout << *begin;
std::cout << std::endl;
// 2) Muestra "jihgfedcba", tal y como esperaba
for (auto begin = std::rbegin(s), end = std::rend(s); begin != end; ++begin)
std::cout << *begin;
std::cout << std::endl;
But showing the character array shows nothing while displaying the string
shows the expected output.
Displaying the array of characters also affects how the string
is displayed: if loop 1 is written before loop 2 the program shows nothing but writing loop 2 before the one shows a single jihgfedcba
.
I have noticed that modifying the return value of std::rbegin(a)
solves the problem:
// Muestra "jihgfedcba", tal y como esperaba
// notese el ++!!
for (auto begin = ++std::rbegin(a), end = std::rend(a); begin != end; ++begin)
std::cout << *begin;
std::cout << std::endl;
// Muestra "jihgfedcba", tal y como esperaba
for (auto begin = std::rbegin(s), end = std::rend(s); begin != end; ++begin)
std::cout << *begin;
std::cout << std::endl;
Why is this happening?