Question

I am a Python/Scala/Haskell coder, trying to learn C++ and I am having hard time remembering anything because so many of the names of methods or classes in C++ do not make any sense to me. This is possibly because I do not understand what they actually intend to do. Can you anyone explain these for me:

  1. vector - vector is such a odd thing to call a list or sequence of things. Why vector?
  2. unordered_set - Why not simply a set? Since when are sets ordered? Doesn't set implictly mean order does not matter and the ordered version (java's TreeSets) should be explicitly called ordered_set instead??
  3. vector.cend - What does cend mean? I could not find cend in the English dictionary? Why not simply call it end?
  4. emplace - What does emplace mean? Shouldn't a better word be "insert"?
  5. push_back - Why not simply call push or append? Isn't the "back" part implicit?
  6. Why does empty() return a boolean? Shouldn't it be isEmpty()?
  7. Also, the .begin() and .end() seems inconsistent. begin() is a verb while end() is a noun in this context. begin denotes to start something whereas it simply returns a pointer to start. Shouldn't the method be called either start which is a noun and goes well with "end" or beginning atleast instead of begin
  8. Also, list.unique does not do at all what I think it does - It simply chops of consecutive similar items!?! (huge violation of principal of least astonishment) - funnily you can do unique correctly in linear run time too with an extra hashset

I have a lot more but if I grok the above, I might have an easier time learning C++. Thanks!

Was it helpful?

Solution 2

  1. vector: There are vectors in functional languages too, like this package
  2. unordered_set - Unordered/ordered refers to the underlying implementation; how the elements are actually stored.
  3. vector.cend - cend is a constant iterator pointing to the end. Constant iterators don't let you modify the value pointed to by the iterator.
  4. emplace - Emplace is like insert except it creates the object "in-place"; you pass emplace the parameters you would pass to the object's constructor.
  5. push_back - This is to maintain a uniform interface. Some containers like std::list let you push_front
  6. Why does empty() return a boolean? Shouldn't it be isEmpty()?: Should it? Says who? You're used to Python/Scala/Haskell but that's hardly the one true way.
  7. Also, the .begin() and .end() seems inconsistent. begin() is a verb while end() is a noun in this context. begin denotes to start something whereas it simply returns a pointer to start. Shouldn't the method be called either start which is a noun and goes well with "end" or beginning atleast instead of begin: I suppose. This seems like a petty gripe. It's just the way it is.
  8. Why does list.unique not return a set? It seems weird it not only modifies the data structure (coming from a functional world, it seems really weird to mutate data as side effects) and it is doubly weird here since C++ already has sets. Am I understanding unique wrong??: A lot of things will be weird if you carry that mindset of no side-effects into C++. The same open mind that let you learn Python/Scala/Haskell should be applied to C++ if you are serious about learning it. list.unique is more like Ruby's Array#uniq. From the documentation:

Notice that an element is only removed from the list container if it compares equal to the element immediately preceding it. Thus, this function is especially useful for sorted lists.

As described, returning a set would lose information because it wouldn't preserve duplicates in the case where they are not consecutive.

Like I said, if you're serious about learning C++ you'll have to deal with some of these language/library design decisions. Many of these points seem like petty gripes to me. Some of these are "just the way it is". Some things in C++ are straight up crazy, but if you're serious about C++ what are you going to do? Indeed, Haskell for example seems to have a more straightforward design that "naturally follows," but that's a different kind of language. Hopefully you can get over this.

OTHER TIPS

in C++, the naming strategy is quite different from those other languages. But as you mentioned, they named the function push_back simply because in some containers like deque has push_front (it is a double end co ntrainer). And we use functions like clear to empty a container instead of empty. Also, in C++, the set container is ordered. It sorts out the elements in O(nlogn) times by average.

For unique in particular, it's about efficiency; it should in fact be called compact. It compacts consecutive equal elements into a single element, which can be done in O(n), where as returning a set would be O(n log n), and incur the cost of construction and moving objects around.

vector - vector is such a odd thing to call a list or sequence of things. Why vector?

C++ also has std::list<T> which is a doubly linked list. I sort of agree it could be called array but then again it is not fixed size.

unordered_set - Why not simply a set? Since when are sets ordered? Doesn't set implictly mean order does not matter and the ordered version (java's TreeSets) should be explicitly called ordered_set instead??

C++03 already has std::set<T> which is ordered set based on search trees. C++11 has added unordered_set<T>, which is based on hash tables. There was a need to distinguish the ordered set named set and the new unordered_set.

vector.cend - What does cend mean? I could not find cend in the English dictionary? Why not simply call it end?

The end() member function returns iterator or const_iterator depending on context. The cend() function always returns const_iterator. This is useful in some situations.

emplace - What does emplace mean? Shouldn't a better word be "insert"?

There is already existing insert() member function which takes value of type T. emplace() is different, its arguments are passed directly to the constructor of the T type and the T value is constructed in-place at the right spot in the container.

push_back - Why not simply call push or append? Isn't the "back" part implicit?

This is likely for parity with std::deque's push_front().

Why does empty() return a boolean? Shouldn't it be isEmpty()?

The C++ standard library does not use camel case anywhere. This would violate the overall style. Neither does it use this is_foo() style.

Also, the .begin() and .end() seems inconsistent. begin() is a verb while end() is a noun in this context. begin denotes to start something whereas it simply returns a pointer to start. Shouldn't the method be called either start which is a noun and goes well with "end" or beginning atleast instead of begin

I am not a native English speaker but I believe both begin and end can be interpreted as both nouns and verbs.

Why does list.unique not return a set? It seems weird it not only modifies the data structure (coming from a functional world, it seems really weird to mutate data as side effects) and it is doubly weird here since C++ already has sets. Am I understanding unique wrong??

Operating on std::list with existing non-member functions (std::unique()) could be less efficient. std::list also provides sort() member function to optimize sort. (It is possible to sort doubly linked lists in place in O(n * log n) time.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top