News for hackers. Stuff that matters.
Re:C++ basics for competitive programming by svalorzen on Friday March 04, 2016 @01:15PM (#11225686)
This article is probably one of the reasons why C++ gets the reputation it has. Please do not program like this. Aside from the fact that this is C++98, which lacks tons of features now widely accepted and useful in C++, the code in this article has no consistency, uses most bad practices I know, and is most certainly not geared towards competitive programming.

All this information is readily found in any online docs about C++. Please read those.

Re:C++ basics for competitive programming by bdrool on Friday March 04, 2016 @01:25PM (#11225753)
Re:C++ basics for competitive programming by jstimpfle on Friday March 04, 2016 @01:54PM (#11225978)
Can you give concrete example what you are referring to? From a quick skim, I would say that's quite clean, straightforward algorithmic code -- while it could be improved a tiny little bit by sprinkling in a little C++11 syntax (don't overdo it).

(For context, I'm mainly a C / Python / Unix Programmer, with an interest in common sense and architectures based on clean plain data interfaces instead of wrong abstractions, and with a good track on SPOJ)

Re:C++ basics for competitive programming by svalorzen on Friday March 04, 2016 @02:03PM (#11226049)
Just random things that pop up:

- using namespace std;

- variable names inconsistent and random, using uppercase and lowercase randomly (code also would not compile since it is cased randomly)

- indenting and braces inconsistent

- use of macros for trivial tasks where they are absolutely not required

- use of macros in totally dangerous ways ("#define all(c) c.begin(), c.end()", seriously?)

- usage of compiler dependent features

- code should be for "competitive programming", and proposes a vector of vectors to make a 2D matrix

- Counterproductive and unhelpful typedefs

EDIT:

I'd like to tell you more, but looking at that page is making me feel unwell. The ostringstream example seems to be out of a nightmare. Just learn C++ from a reputable source and save yourself and everybody else around you the pain.

Re:C++ basics for competitive programming by rjtobin on Friday March 04, 2016 @02:28PM (#11226251)
For programming contests, often saving keypresses is more valuable than good style or practice. Agreed that no one should cut these corners when writing code that will be used again tomorrow, but when you're trying to hammer out something in under 10 minutes (or whatever), there are lots of bad practice tricks that become very useful (like macros that would be awful in any other context).

Before people lynch me, I just wants emphasize again that I'm not defending these practices in production code, but the article wasn't advocating that either. And of course there's the old debate about whether the contests themselves become harmful because they encourage bad practice, and that may be true, but that's a different conversation.

Re:C++ basics for competitive programming by radicality on Friday March 04, 2016 @02:23PM (#11226212)
Did you read that this is for _competitive programming_?. I know you did as you mention it, but then some of your points don't make sense. In topcoder every second counts, and if using `all(c)` instead of writing up `c.begin(), c.end()` can save you two seconds, you should do it. Also your code can be 'challenged' by someone else (they give an example where your code fails), and making obscure typedefs and macros that will make it ever so slightly harder for someone to decipher what you are going is allright.
Re:C++ basics for competitive programming by jzwinck on Friday March 04, 2016 @02:17PM (#11226169)
You're right that some of those items are bad practices in normal development. But some of them would actually help a person bang together crappy but working code more quickly for a one-off task.

Perhaps we should read "competitive programming" as "programming quickly without regard for quality." Taken that way, the article could even be titled "programming to impress nontechnical managers so you can become one."

Re:C++ basics for competitive programming by Nimitz14 on Friday March 04, 2016 @02:19PM (#11226188)
> proposes a vector of vectors to make a 2D matrix

what would be the right way to do this?

Re:C++ basics for competitive programming by Stasis5001 on Friday March 04, 2016 @03:04PM (#11226508)
2D arrays that are laid out as a linear array of mn elements are faster to iterate through. That's what happens when you declare e.g. int a[5][4], you get the same layout as int a[20]. A vector of vectors is fairly sprawling in memory, and generally matrices don't get resized so you want this compact form because it's quicker to loop through (either way is O(mn), but one has a way better coefficient).
Re:C++ basics for competitive programming by Devid2014 on Friday March 04, 2016 @04:18PM (#11226932)
Is this enough ?

    typedef vector<int> vi;

    typedef vector<vi> vvi;

    typedef pair<int,int> ii;

    #define sz(a) int((a).size())

    #define pb push_back

    #defile all(c) (c).begin(),(c).end()

    #define tr(c,i) for(typeof((c).begin() i = (c).begin(); i != (c).end(); i++)

    #define present(c,x) ((c).find(x) != (c).end())

    #define cpresent(c,x) (find(all(c),x) != (c).end())
Re:C++ basics for competitive programming by jonathankoren on Friday March 04, 2016 @05:00PM (#11227159)
That's just bad C code, and is exactly the type of stuff I'd expect on topcoder, which seems to attract the types that write incomprehensible code... but hey man, it's fast!

Just write C. C++ is too much of a pain.

Re:C++ basics for competitive programming by Hernanpm on Friday March 04, 2016 @02:16PM (#11226148)
I do not agree, this is tutorial is very specific for competitive programming and it is well known that it doesn't mean good code, e.g. https://news.ycombinator.com/item?id=9324209 and actually it was great tutorial years ago but now we have c++11 and I would like an updated version of this tutorial that use c++11 features.
Re:C++ basics for competitive programming by ThatGeoGuy on Friday March 04, 2016 @01:32PM (#11225804)
I cannot support this view enough. After reading about `typeof(a + b)` to do what `auto` in C++11 does, I cringed a little inside.

If you want to get good at C++, there are no shortcuts. Read books (Scott Meyers, Bjarne Stroustrup), read Herb Sutter's GotW series, but don't read this.

Re:C++ basics for competitive programming by fsloth on Friday March 04, 2016 @02:12PM (#11226124)
I would add "Physically based rendering" by Pharr and Humphreys to that list. They demonstrate how to construct a complex application using C++ using a fairly unique literary programming style, and they also provide the source code for a functional system in their homepage. It's domain specific but the domain happens to be concerned with efficiency and correctness so it's not a bad one to learn.
Re:C++ basics for competitive programming by zjarek on Friday March 04, 2016 @02:40PM (#11226344)
You don't get C++11 or newer features on gcc 4.0.2 (https://community.topcoder.com/tc?module=Static&d1=help&d2=g...).

You have to keep in mind that code for this kind of competitions is generally write only and uses basic facilities of a language to solve given algorithmic problem as fast as possible. Consistency isn't important when your solution fits on a single page.

Re:C++ basics for competitive programming by aksx on Friday March 04, 2016 @01:35PM (#11225835)
You think this is bad? try reading the code submitted during coding competitions (hackerrank, google's code jam).

lots of macros to do operations quickly, the code is not even readable.

Re:C++ basics for competitive programming by thrownaway2424 on Friday March 04, 2016 @01:30PM (#11225792)
Seriously. Nobody needs to think about stuff like this any more:

  vector< vector<int> >
Who even does that?
Re:C++ basics for competitive programming by GFK_of_xmaspast on Friday March 04, 2016 @02:20PM (#11226191)
Personally I find the version with spaces easier to read, but I'm not going to get annoyed either way.

Of course, it should also be std::vector<std::vector<int>> ...

Re:C++ basics for competitive programming by stinos on Friday March 04, 2016 @02:50PM (#11226423)
or more spacey, like std::vector< std::vector< int > >
Re:C++ basics for competitive programming by forrestthewoods on Friday March 04, 2016 @01:57PM (#11226003)
People on C++03 where vector<vector<int>> is defined as operator >>. This wasn't fixed until C++11.

https://en.wikipedia.org/wiki/C%2B%2B11#Right_angle_bracket

Re:C++ basics for competitive programming by static_noise on Friday March 04, 2016 @02:16PM (#11226161)
Don't use C++03 in 2016 please.
Re:C++ basics for competitive programming by jjnoakes on Friday March 04, 2016 @03:51PM (#11226763)
Not all of us have the luxury of using the latest compilers. Or the systems we support don't even have compilers which handle anything newer than C++03.

Do you think compiler support is instantaneous for all platforms on all operating systems, retroactively, when the standard is published?

Re:C++ basics for competitive programming by xorblurb on Friday March 04, 2016 @04:15PM (#11226913)
instantaneous != retroactively, neither it is the same as "5 years after".

And all major compiler vendors have since learned that they need to be up to date with the standard ASAP, even before official publication when it is possible.

Advocating C++ < 11 is harmful to the community, given how bad it was at that time.

Re:C++ basics for competitive programming by jjnoakes on Tuesday March 08, 2016 @08:21AM (#11245010)
Suggesting that compilers immediately adopt the latest standards is the "instantaneous".

Suggesting that older versions of the compilers (which may be the latest available versions on certain systems) also do so is the "retroactive".

So no, they are not the same. But yes, they are both reasons why one can not just blindly start using the latest features from the latest standards.

Re:C++ basics for competitive programming by hikarudo on Friday March 04, 2016 @02:39PM (#11226337)
Except if the compiler for your target platform (cough TI DSP cough) supports only C++03, and never will support anything newer.
Re:C++ basics for competitive programming by whitegrape on Friday March 04, 2016 @02:54PM (#11226449)
> Don't use C++ in 2016 please.

Fixed that for you.

Re:C++ basics for competitive programming by ska on Friday March 04, 2016 @03:13PM (#11226573)
No, you really didn't.

For all it's problems (some of which are being addressed in recent standard update , some not so much yet) it remains the best available tool for some domains. More's the pity.

Re:C++ basics for competitive programming by 1024core on Friday March 04, 2016 @03:14PM (#11226578)
Seriously. C++ seems like a layer of hacks, one upon another, each designed to "fix" things in the layers below, only to add more confusion. Maybe "D" is the answer, or Go...? I don't know.

I started with C++ a long time ago, but for whatever reason, kept getting out of it (for a few years) and then every time I'd get back in, I'd be surprised at the new stuff.

Re:C++ basics for competitive programming by spdionis on Friday March 04, 2016 @01:52PM (#11225966)
Just curious, what should you use instead?
Re:C++ basics for competitive programming by scott_s on Friday March 04, 2016 @01:57PM (#11226006)

  vector<vector<int>>
Prior to C++11, the code above would fail to compile. The lexer would see the final ">>" and assume it was the out-stream operator. This was such an annoying issue that they eventually changed the grammar so that the above would compile as expected.
Re:C++ basics for competitive programming by svalorzen on Friday March 04, 2016 @01:55PM (#11225988)
He's talking about the fact that older compilers used to get confused if you happened to open/close multiple template tags in sequence; i.e. "<<" or ">>". These were mistaken for operators, and the code failed to compile. Nowadays there's no need to keep them separated, and compilers parse them correctly.
Re:C++ basics for competitive programming by jzwinck on Friday March 04, 2016 @02:19PM (#11226180)
Matrix2D<int>. Removing a space from between the brackets misses the forest.