itoa or bitset?

I want to transform decimal to binary
1
2
3
4
5
char str[10];
for(size_t i = 1;i <= 256;i++)
{
 std::cout<<itoa(i, str, 2)<<"\n";
}


or
1
2
3
4
5
6
std::bitset<9> a;
for(size_t i = 1;i <= 256;i++)
{
 a = i;
 std::cout<<a<<"\n";
}

which one is better?
I think that bitset would be slower than itoa but the space
would be smaller than the first solution
itoa is faster, but need more space and it is non-standard(I can write one for myself)

Is there any "absolute reoson" I should dump one of them?
Or could I just use bitset to finish the task if it wouldn't
become the bottle neck of my programs?

ps : My classmates told me I should use itoa, because itoa is far more better than bitset

Thank you
Last edited on
itoa is NOT a standard C++ function.
You can use bitset or write a function yourself to do this.
I think the instantiation of a bitset might be a bottleneck. Try writing a special function specifically for binary (a generalized algorithm would be slower).
Thanks, I would write or find a function to fulfill the job if it becomes the bottel neck of my programs
I don't think a generalized function has to be slower but I am pretty sure a bitset that small does have a little memory overhead. I would prefer the bitset approach--it's a very elegant solution that is both standard and very reliable.
Last edited on
Topic archived. No new replies allowed.