This is the function I've implemented in an answer to this questionthis question. I've tried to make this as simple and idiomatic as possible, using C++11. Could this still be improved in any way?
#include <algorithm>
#include <cstdint>
#include <iostream>
#include <iterator>
#include <vector>
typedef std::vector<int> SplitValues;
SplitValues splitter(std::int32_t number)
{
SplitValues values(8);
for (auto& iter : values)
{
iter = static_cast<int>(number & 0xF);
number >>= 4;
}
return values;
}
int main()
{
std::int32_t number = 432214123;
SplitValues values = splitter(number);
std::reverse_copy(values.begin(), values.end(),
std::ostream_iterator<int>(std::cout, " "));
}
Questions:
- Is
SplitValuesan appropriate name for atypedef, or does it sound more like a variable? - If I pass the hard-coded number instead of the
numbervariable, will it still be treated as the same type as the parameter?