Skip to main content
added 604 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

This is how I would've implemented it with extensive use of the STL and C++11. It is a bit shorter than yours, but also more idiomatic. I've added comments throughout to help explain what each part does.

The main thing to note here is that you should return a container object instead of a pointer to a static C array. It's best not to mess with raw pointers in C++ when possible, especially when it makes more sense to return a container object itself. The compiler should be able to optimize this via Return Value Optimization (RVO) as well. This object should then not be static.

This does produce the same output as yours:

#include <algorithm> // std::reverse_copy()
#include <cstdint>   // std::int32_t
#include <iostream>
#include <iterator>  // std::ostream_iterator
#include <vector>

// you could use a typedef to save some typing
// also to give more meaning to your vector
typedef std::vector<int> SplitValues;

SplitValues splitter(std::int32_t number)
{
    SplitValues values(8);

    // work on vector elements using iterators
    for (auto& iter : values)
    {
        iter = static_cast<int>(number & 0xF); // cast the C++ way
        number >>= 4; // shorter form
    }

    return values;
}

int main()
{
    std::int32_t number = 432214123;

    // local vector assigned to one returned from function
    SplitValues values = splitter(number);
    
    // display vector in reverse order using ostream iterator
    // this is done with one (wrapped) line and no loop
    std::reverse_copy(values.begin(), values.end(),
        std::ostream_iterator<int>(std::cout, " "));
}
#include <algorithm> // std::reverse_copy()
#include <cstdint>   // std::int32_t
#include <iostream>
#include <iterator>  // std::ostream_iterator
#include <vector>

// you could use a typedef to save some typing
// also to give more meaning to your vector
typedef std::vector<int> SplitValues;

SplitValues splitter(std::int32_t number)
{
    SplitValues values(8);

    // work on vector elements using iterators
    for (auto& iter : values)
    {
        iter = static_cast<int>(number & 0xF); // cast the C++ way
        number >>= 4; // shorter form
    }

    return values;
}

int main()
{
    std::int32_t number = 432214123;

    // local vector assigned to one returned from function
    SplitValues values = splitter(number);
    
    // display vector in reverse order using ostream iterator
    // this is done with one (wrapped) line and no loop
    std::reverse_copy(values.begin(), values.end(),
        std::ostream_iterator<int>(std::cout, " "));
}

This is how I would've implemented it with extensive use of the STL and C++11. It is a bit shorter than yours, but also more idiomatic. I've added comments throughout to help explain what each part does.

The main thing to note here is that you should return a container object instead of a pointer to a static C array. It's best not to mess with raw pointers in C++ when possible, especially when it makes more sense to return a container object itself. The compiler should be able to optimize this via Return Value Optimization (RVO) as well. This object should then not be static.

This does produce the same output as yours:

#include <algorithm> // std::reverse_copy()
#include <cstdint>   // std::int32_t
#include <iostream>
#include <iterator>  // std::ostream_iterator
#include <vector>

// you could use a typedef to save some typing
// also to give more meaning to your vector
typedef std::vector<int> SplitValues;

SplitValues splitter(std::int32_t number)
{
    SplitValues values(8);

    // work on vector elements using iterators
    for (auto& iter : values)
    {
        iter = static_cast<int>(number & 0xF); // cast the C++ way
        number >>= 4; // shorter form
    }

    return values;
}

int main()
{
    std::int32_t number = 432214123;

    // local vector assigned to one returned from function
    SplitValues values = splitter(number);
    
    // display vector in reverse order using ostream iterator
    // this is done with one (wrapped) line and no loop
    std::reverse_copy(values.begin(), values.end(),
        std::ostream_iterator<int>(std::cout, " "));
}
added 111 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
#include <algorithm> // std::reverse_copy()
#include <cstdint>   // std::int32_t
#include <iostream>
#include <iterator>  // std::ostream_iterator
#include <vector>

// you could use a typedef to save some typing
// also to give more meaning to your vector
typedef std::vector<int> SplitValues;

SplitValues splitter(long intstd::int32_t number)
{
    SplitValues values(8);

    // work on vector elements using iterators
    for (auto& iter : values)
    {
        iter = static_cast<int>(number & 0xF); // cast the C++ way
        number >>= 4; // shorter form
    }

    return values;
}

int main()
{
    long intstd::int32_t number = 432214123L;432214123;

    // local vector assigned to one returned from function
    SplitValues values = splitter(number);
    
    // display vector in reverse order using ostream iterator
    // this is done with one (wrapped) line and no loop
    std::reverse_copy(values.begin(), values.end(),
        std::ostream_iterator<int>(std::cout, " "));
}
#include <algorithm> // std::reverse_copy()
#include <iostream>
#include <iterator> // std::ostream_iterator
#include <vector>

// you could use a typedef to save some typing
// also to give more meaning to your vector
typedef std::vector<int> SplitValues;

SplitValues splitter(long int number)
{
    SplitValues values(8);

    // work on vector elements using iterators
    for (auto& iter : values)
    {
        iter = static_cast<int>(number & 0xF); // cast the C++ way
        number >>= 4; // shorter form
    }

    return values;
}

int main()
{
    long int number = 432214123L;

    // local vector assigned to one returned from function
    SplitValues values = splitter(number);
    
    // display vector in reverse order using ostream iterator
    std::reverse_copy(values.begin(), values.end(),
        std::ostream_iterator<int>(std::cout, " "));
}
#include <algorithm> // std::reverse_copy()
#include <cstdint>   // std::int32_t
#include <iostream>
#include <iterator>  // std::ostream_iterator
#include <vector>

// you could use a typedef to save some typing
// also to give more meaning to your vector
typedef std::vector<int> SplitValues;

SplitValues splitter(std::int32_t number)
{
    SplitValues values(8);

    // work on vector elements using iterators
    for (auto& iter : values)
    {
        iter = static_cast<int>(number & 0xF); // cast the C++ way
        number >>= 4; // shorter form
    }

    return values;
}

int main()
{
    std::int32_t number = 432214123;

    // local vector assigned to one returned from function
    SplitValues values = splitter(number);
    
    // display vector in reverse order using ostream iterator
    // this is done with one (wrapped) line and no loop
    std::reverse_copy(values.begin(), values.end(),
        std::ostream_iterator<int>(std::cout, " "));
}
added 86 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
#include <algorithm> // std::reverse_copy()
#include <iostream>
#include <iterator> // std::ostream_iterator
#include <vector>

// you could use a typedef to save some typing
// also to give more meaning to your vector
typedef std::vector<int> SplitValues;

SplitValues splitter(long int number)
{
    static std::vector<int>SplitValues values(8); // set vector size to 8

    // work on vector elements using range-based for-loopiterators
    for (auto& iter : values)
    {
        iter = static_cast<int>(number & 0xF); // cast the C++ way
        number >>= 4; // shorter form
    }

    return values;
}

int main()
{
    long int number = 432214123;432214123L;

    // local vector assigned to vectorone returned from function
    std::vector<int>SplitValues values = splitter(number);
    
    // display vector in reverse order using ostream iteratorsiterator
    std::reverse_copy(values.begin(), values.end(),
        std::ostream_iterator<int>(std::cout, " "));
}
#include <algorithm> // std::reverse_copy()
#include <iostream>
#include <iterator> // std::ostream_iterator
#include <vector>

std::vector<int> splitter(long int number)
{
    static std::vector<int> values(8); // set vector size to 8

    // work on vector elements using range-based for-loop
    for (auto& iter : values)
    {
        iter = static_cast<int>(number & 0xF); // cast the C++ way
        number >>= 4; // shorter form
    }

    return values;
}

int main()
{
    long int number = 432214123;

    // local vector assigned to vector returned from function
    std::vector<int> values = splitter(number);
    
    // display vector in reverse order using ostream iterators
    std::reverse_copy(values.begin(), values.end(),
        std::ostream_iterator<int>(std::cout, " "));
}
#include <algorithm> // std::reverse_copy()
#include <iostream>
#include <iterator> // std::ostream_iterator
#include <vector>

// you could use a typedef to save some typing
// also to give more meaning to your vector
typedef std::vector<int> SplitValues;

SplitValues splitter(long int number)
{
    SplitValues values(8);

    // work on vector elements using iterators
    for (auto& iter : values)
    {
        iter = static_cast<int>(number & 0xF); // cast the C++ way
        number >>= 4; // shorter form
    }

    return values;
}

int main()
{
    long int number = 432214123L;

    // local vector assigned to one returned from function
    SplitValues values = splitter(number);
    
    // display vector in reverse order using ostream iterator
    std::reverse_copy(values.begin(), values.end(),
        std::ostream_iterator<int>(std::cout, " "));
}
deleted 43 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
Loading
deleted 13 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
Loading
deleted 90 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
Loading
deleted 185 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
Loading
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
Loading