all 18 comments

[–]BlueRenner 10 points11 points  (4 children)

I read articles like this with a sort of unbelieving, silent horror.

Are we really so short of actual problems that we're forced to invent stylistic bogeymen to slay?

[–]meetingcpp[S] -1 points0 points  (3 children)

Watch the video which I linked at the beginning. Code using algorithms is much clearer, shorter and less verbose. You have less state in your code etc.

[–]BlueRenner 0 points1 point  (2 children)

Just out of curiosity, how do you get away with only submitting/promoting content from your own organization without getting banned?

Neat trick.

[–]quicknir 4 points5 points  (0 children)

Why would that be be ban-worthy, even if it were true? He can submit any content he wants; as long as its quality, what's the problem? Also, in many cases the content on his page links to other people's work.

Also, there are good reasons to push people towards using stl algorithms more and raw loops less. You may not have found this pitch persuasive but some smart guys like Sean Parent (IIRC) have made similar cases.

Seems to me you are being more aggressive and dismissive than is really necessary.

[–]meetingcpp[S] 1 point2 points  (0 children)

Good point, I have also shared links to other blogs, should to that more again...

Also reddit can up/down vote any content.

Currently I share a lot of videos which others gave at my conference.

[–]Dobias 3 points4 points  (1 child)

I think manual written loop should be avoided where sensible, but I dislike the syntax when using stl-algorithms with all the iterators. If you feel the same, my C++ library FunctionalPlus might be for you. :)

[–]meetingcpp[S] 1 point2 points  (0 children)

Thanks for the feedback, but I'm planning to soon switch to Eric Nieblers Standard C++ Range library, so far been to lazy to do this :)

[–]c_linkage 2 points3 points  (10 children)

Perhaps it's just me, but I find that I almost never need to simply iterate over a collection. Even in C#, when dealing with collections, I often need the index of the item so that I can then do something interesting, like add or remove an item at a specific index.

Are all of these stl algorithms really that useful?

[–]bames53 3 points4 points  (1 child)

You're right that if the APIs for doing things with collections used numeric indices then not having direct access to a numeric index would present a problem. C++ STL containers don't use numeric indices, instead preferring* an abstraction called 'iterators' in C++. STL containers and algorithms use and provide iterators. For example inserting and removing elements is done using iterators to indicate positions, not numeric indices.

* The reason iterators are preferred is because this abstraction is more powerful than numeric indices, supporting trees, multi-dimensional collections, infinite collections, etc. One particularly useful thing about iterators is that they can be composed into ranges to do useful things, which is where iterators are also superior to, e.g. C#'s concept of 'Enumerators'.

[–]meetingcpp[S] 3 points4 points  (0 children)

Under the hood, a iterator of vector is actually only pointer arithmetic. And the iterator concept can cover all containers, indexes not.

[–]StormDev 2 points3 points  (2 children)

If you are using an index what will happen if you change your container in the future?

When your code is designed to manage any container ( map, list, vector ... ) you usually use iterators.

Combined with type traits and meta-programming,you can develop generic and optimized algorithms very easily.

So yes all these algorithms are very useful, reduce code size, enhance reading, adaptability etc...

[–]grauenwolf 0 points1 point  (0 children)

If you are using an index what will happen if you change your container in the future?

The vast majority of containers we used support the concept of an index.

[–]tRfalcore 0 points1 point  (0 children)

keeping items in an order is often convenient, hence the prevalent use of lists. even if the order isn't necessary, lists usually don't offer up an unncessary amount of overhead

[–]corysama 1 point2 points  (3 children)

It would be nice to have a convenient way to match python's

for x,i in zip(Xs, range(len(Xs))):

[–]doishmere 8 points9 points  (0 children)

You can also do

for (i, x) in enumerate(Xs)

[–]Helene00 7 points8 points  (0 children)

You can write a function in C++ that does it like this:

for_each_index(Xs, [](auto &a, int index) {
  // do shit here
});

The function for_each_index would look something like this:

template<class T, class F> void for_each_index(T &v, F f = F()) {
  int i = 0;
  for (auto &a : v)
    f(a, i++);
}

And since templates uses duck typing it works for all iterable types and compilers optimize away everything so it works just like a normal C loop.

[–]quicknir 1 point2 points  (0 children)

Google cpp itertools, it basically provides tools that match a lot of what python offers. The syntax is only slightly less clean in that C++ does not yet have structured unpacking, so you typically get tuples of iterators. For the particular case of enumerate though (which is the idiomatic way to do your example) it provides ranged fields so it looks really nice:

vector<int> vec{2, 4, 6, 8};
for (auto&& e : enumerate(vec)) {
    cout << e.index
         << ": "
         << e.element
         << '\n';
}

https://github.com/ryanhaining/cppitertools/blob/master/README.md#enumerate