Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Big side note, but C++ for me is just... too much. I come to C++ from Python, and they are just so far from each other it hurts. One of the fundamental things about Python that I loved was:

    "There should be one-- and preferably only one --obvious way to do it."
Meanwhile, the sage wisdom I heard learning C++ is:

    "C++ is wonderful because there are so many ways to do anything."
These two methods of thought fly right in the face of each other, and it's very hard to reconcile. With Python, I felt like I truly was learning a language, a language of action where doing any one thing was consistently defined. With C++ I feel like I'm in an ocean of choice and ambiguity, having to carry around this huge load of tribal knowledge to get anything done.

Not saying it's a bad language, just sharing my feelings on it.



Reversing a string in Python is not obvious nor consistent:

    str[::-1]
While it is in C++:

    std::reverse(str.begin(), str.end());
And Ruby is even more obvious and consistent:

    str.reverse()
Most people who rag on C++ have never seriously used it. It's not that bad. Really, it isn't.


That's one point against python, and half a point for C++ [1]. By my accounting that leaves the score still hugely in python's favor when it comes to obviousness and consistency.

Python vs Ruby is another subject entirely. Largely I feel like it's mostly a narcissism of small differences situation, and have only been doing more Python than Ruby lately because more people around me are Pythonistas than Rubyists at the moment.

1] ideally you wouldn't have to tell the reverse function the starting and ending points for the string unless you only wanted the reverse of a substring


begin() and end() return iterators. Python has iterators too, so this concept shouldn't be alien to Python programmers. reverse() is a generic algorithm that operates on any pair of bidirectional iterators.

Actually, in C++, you ideally wouldn't reverse the string at all... you'd just call str.rbegin() and str.rend() to grab the reverse iterators, and pass them straight to your next algorithm.

The two approaches are just as consistent as one another, Python just uses a terser syntax (which, in my opinion, isn't as obvious).

In C++14, if all things go according to plan with Concepts Lite, you'll be able to write a short 3-5 line, reusable, version of reverse() that takes your string (or container) as one argument, deduces at compile time, during overload resolution, that begin() and end() return bidirectional iterators, and then does the right thing. Failing that, C++14 may introduce Ranges. So C++ is only getting terser.


> Failing that, C++14 may introduce Ranges.

It would have been nice to have some sort of simple range syntax added to C or C++ long ago. In the true spirit of C, don't even make it safe, just make it bloody well work.

FirstArray[0..3] = SecondArray[4..7];

case 2..10:

for(int i : 0..ArrayLength)

for(int i : ArrayLength..0)

(hah that'd prove horrible if ArrayLength ended up being a negative number, obviously some proper syntax would need to be determined :) )

I am always annoyed that such simple things are ignored in the language. Sure they don't enable any "cool new abilities", but they make using the language a lot more friendly (especially in comparison to having a ton of case fall through statements!)


The problem is your proposed [] syntax implies random access. Many algorithms and data structures don't require or support random access.


reverse iterate a string? i have never done that


Well, there's always reversed(str) for Python (I realise this returns a reversed object and not a string object, which is something I dislike immensely).

I've always looked at [::-1] as being a bit of a hack, even though we use it all the time for both strings and lists.


How often do you need to reverse a string, or even a list?


that is little verbose, you can save a whole 7 characters.

  reverse(begin(str), end(str));


also you will need #include <algorithm>


Python 3 also has `reversed(str)`.


You're right, C++ is like a huge load of tribal knowledge. It's not like a monolithic language, it is like a group of languages tied together, a swiss army knife if you will. Try take them gradually, in the order of their historical development: C with its preprocessor, then C++ classes with all their stuff, then generic programming (templates), and finally - in-depth stl. You may be told that it's best to start with stl because it was developed for (among other things) the beginners to avoid shooting themselves in the foot, but I find it to be just a coverage for higher-level programming (used best when you already know its under-the-hood low-level stuff).




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: