-1

My code extract is

if (num == 0) {
    cout << 1;
    cout << 2;
}
else if (num == 1) {
    cout << 0;
    cout << 2;
}
else if (num == 2) {
    cout << 0;
    cout << 1;
}

where 0 <= num <= 2.

I am asking because the cout << ... statements will be converted into something much larger, but (almost) identical to each other.

*I mean optimize as 'beautify' the code (ex. remove the 5 repeated cout statements).

I didn't put "beautify" in the question because it just sounds weird in a SO question.

sudomeacat
  • 95
  • 1
  • 2
  • 11
  • 1
    Use a table per value. Feed the table per value into cout. This is less about optimization and more about reducing redundant code and making things easier to maintain. – Michael Dorgan Nov 11 '17 at 01:06
  • Why would `cout << ...` be *converted into something much larger*? They're just [function calls](http://en.cppreference.com/w/cpp/io/basic_ostream/operator_ltlt). – Praetorian Nov 11 '17 at 01:07
  • @Praetorian I think they mean `cout` is a placeholder – Nick is tired Nov 11 '17 at 01:08
  • 1
    Well the last `else if` can just be an `else` if you don't have to worry about anything else. – NathanOliver Nov 11 '17 at 01:09
  • @DimChtz I meant optimize as 'beautify and/or remove repeated code (like the 5 repetitions of cout'. But any of those phrases would be weird in the question. – sudomeacat Nov 11 '17 at 01:22
  • @sudomeacat: please put the information you gave to DimChtz in the comments (what you mean by optimize), and I'll upvote your question then, because it is clearly not a bad question. – geza Nov 11 '17 at 02:01
  • @geza I'm sure this is the comments, but did you mean the question (just making sure)? – sudomeacat Nov 11 '17 at 02:10
  • Yes, I was ambiguous. I meant to please edit your question, and put the information what you gave to DimChtz. – geza Nov 11 '17 at 02:21
  • For future reference, questions like this don't belong on StackOverflow. If the code works in general, and you just want to clean it up and make it better, ask on [CodeReview](http://codereview.stackexchange.com) instead. – Remy Lebeau Nov 11 '17 at 03:36

4 Answers4

6

Perhaps this?

cout << "100"[num];
cout << "221"[num];
geza
  • 28,403
  • 6
  • 61
  • 135
  • That is cute. Why use 1 table lookup, when you can have 2 with 0 compares. Nice solution. – Michael Dorgan Nov 11 '17 at 01:26
  • @MichaelDorgan: if you like it, then check out this :) https://stackoverflow.com/a/45249531/8157187 – geza Nov 11 '17 at 01:30
  • @MichaelDorgan: woha, I didn't know that this kind of stuff actually has a name. Code golf, cool :) – geza Nov 11 '17 at 01:34
  • That's a really creative answer, but wouldn't the result of string::operator[] be a char? (I mean I guess I could subtract '0' from it.) But it would be fun to see this in code golf. – sudomeacat Nov 11 '17 at 01:53
  • @sudomeacat: it prints it as a char (you cannot differentiate between '1' and 1, when it's printed). If you do need an integer, then yes, your subtraction idea works. Or, you could get dirty, and use `"\x01\x00\x00"[num]` :) But maybe a proper `static const char table[2][3] = { ... };` is a better idea. – geza Nov 11 '17 at 01:56
2

Something along these lines, perhaps:

for (int i : {0, 1, 2}) {  // or for (int i = 0; i <= 2; ++i) {
  if (i != num) {
    std::cout << i;
  }
}
Igor Tandetnik
  • 50,461
  • 4
  • 56
  • 85
  • Lol - but this still has 3 compares! How can that be faster than... waiting for an eternity for cout to run. – Michael Dorgan Nov 11 '17 at 01:08
  • 2
    @MichaelDorgan I understood "optimize" to mean not so much "make this code faster" but rather "organize this code better". To reduce repetition. – Igor Tandetnik Nov 11 '17 at 01:09
  • And I completely agree, otherwise this is a fruitless adventure with no real prize to be had. My comment was complete sarcasm. Forgot the markups. – Michael Dorgan Nov 11 '17 at 01:11
1

Well, here's something different (without if or for):

cout << ( (n + 1) % 2); 
cout << ( (n + 2) % 2) + (2 - n);

Obviously: n = 0,1,2.

DimChtz
  • 4,043
  • 2
  • 21
  • 39
0

This is an alternative one

  cout.write((0==n)?("12"):((1==n)?("02"):("01")),2);
  • 3 implicit table/strings, 2 compares, less maintainable. I'm sorry, but putting everything on 1 line doesn't make it run faster or make it more maintainable. The reason @Geza's answer is so nice is that it has 0 compares and the lookups he includes are very plain to see. – Michael Dorgan Nov 13 '17 at 18:00