T
Tomás
Looking at the following function:
#include <cstddef>
#include <iostream>
template<typename T, std::size_t len>
void PrintOutStrings( const char* const (&Array)[len] )
{
for (std::size_t i = 0; i < len; ++i) std::cout << Array;
}
Let's say we want to print the strings out backwards... how do you
usually do it? I've a way of doing it and I'm wondering what you think of
it?
#include <cstddef>
#include <iostream>
template<typename T, std::size_t len>
void PrintOutStringsBackwards( const char* const (&Array)[len] )
{
for (std::size_t i = len - 1; ~i; --i) std::cout << Array;
}
My rationale here is that the counter variable "i" will get smaller and
smaller. It will become 0, and then when it's decremented again, it will
become 0xFFFF (or 0xFFFFFFFF, or 0xFFFFFFFFFFFFFFF...). If we then invert
the bits using the "~" operator, then we'll get 0; so the loop should
stop then.
Any potential pitfalls?
A quiet nod of approval would do me.
-Tomás
#include <cstddef>
#include <iostream>
template<typename T, std::size_t len>
void PrintOutStrings( const char* const (&Array)[len] )
{
for (std::size_t i = 0; i < len; ++i) std::cout << Array;
}
Let's say we want to print the strings out backwards... how do you
usually do it? I've a way of doing it and I'm wondering what you think of
it?
#include <cstddef>
#include <iostream>
template<typename T, std::size_t len>
void PrintOutStringsBackwards( const char* const (&Array)[len] )
{
for (std::size_t i = len - 1; ~i; --i) std::cout << Array;
}
My rationale here is that the counter variable "i" will get smaller and
smaller. It will become 0, and then when it's decremented again, it will
become 0xFFFF (or 0xFFFFFFFF, or 0xFFFFFFFFFFFFFFF...). If we then invert
the bits using the "~" operator, then we'll get 0; so the loop should
stop then.
Any potential pitfalls?
A quiet nod of approval would do me.
-Tomás