Skip to main content
replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

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, " "));
}

Test run

Questions:

  1. Is SplitValues an appropriate name for a typedef, or does it sound more like a variable?
  2. If I pass the hard-coded number instead of the number variable, will it still be treated as the same type as the parameter?

This is the function I've implemented in an answer to this 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, " "));
}

Test run

Questions:

  1. Is SplitValues an appropriate name for a typedef, or does it sound more like a variable?
  2. If I pass the hard-coded number instead of the number variable, will it still be treated as the same type as the parameter?

This is the function I've implemented in an answer to this 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, " "));
}

Test run

Questions:

  1. Is SplitValues an appropriate name for a typedef, or does it sound more like a variable?
  2. If I pass the hard-coded number instead of the number variable, will it still be treated as the same type as the parameter?
added 243 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

This is the function I've implemented in an answer to this question. I've tried to make this as simple and idiomatic as possible, using C++11. Could this still be improved in any way?

Test run

#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, " "));
}

Test run

Questions:

  1. Is SplitValues an appropriate name for a typedef, or does it sound more like a variable?
  2. If I pass the hard-coded number instead of the number variable, will it still be treated as the same type as the parameter?

This is the function I've implemented in an answer to this question. I've tried to make this as simple and idiomatic as possible, using C++11. Could this still be improved in any way?

Test run

#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, " "));
}

This is the function I've implemented in an answer to this 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, " "));
}

Test run

Questions:

  1. Is SplitValues an appropriate name for a typedef, or does it sound more like a variable?
  2. If I pass the hard-coded number instead of the number variable, will it still be treated as the same type as the parameter?
Forgot to take out a comment
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

This is the function I've implemented in an answer to this question. I've tried to make this as simple and idiomatic as possible, using C++11. Could this still be improved in any way?

Test run

#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; // shorter form
    }

    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, " "));
}

This is the function I've implemented in an answer to this question. I've tried to make this as simple and idiomatic as possible, using C++11. Could this still be improved in any way?

Test run

#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; // shorter form
    }

    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, " "));
}

This is the function I've implemented in an answer to this question. I've tried to make this as simple and idiomatic as possible, using C++11. Could this still be improved in any way?

Test run

#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, " "));
}
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
Loading