Hi! I wrote a program that converts Binary code to Grey code. It works, but I feel like it's highly inefficient and I was wondering if anyone has any suggestions from where to go from here or any other method of doing the same thing. I'd really like to become a more elegant programmer, but I'm not quite sure how to go about doing so.
I've also been trying out Project Euler, but I seem to always turn to using vectors whenever solving a problem. I feel like that's not a good habit to practice. So, any advice from the better programmers out there?
It may appear to work, but it has some undefined behavior. In particular, you access outside the bounds of the bin vector in toGrey.
(And, btw, it's Gray code, not grey code. It's a name, not a color.)
I've also been trying out Project Euler, but I seem to always turn to using vectors whenever solving a problem. I feel like that's not a good habit to practice.
vectors and/or deques should be your "go to" container.
If we can guarantee we only need a certain number of bits and that number of bits can be held in an integral type, we might be better off manipulating bits, but since there is no such guarantee mentioned in your code, I think I would probably just use strings as in the following code: