syntax highlight

Tuesday, 16 February 2016

Some new set operations in C++11 stl

The std header has a few cool additions that make life easier in C++11:

void f() {
  vector<int> v = {1, 2, 3, 4, 5, 60, 70, 80, 90};

  auto less_than_10 = [](int x){ return x < 10; };
  if (all_of(v.begin(), v.end(), less_than_10)) {
    cout << "Yay!";
  }
}

Besides all_of, in you can also find any_of and none_of.

Bonus: do you find that initializer list hideous? Just use std::iota, from stl too:

vector<int> v(100, 0);
iota(v.begin(), v.end(), 0);

3 comments:

  1. Inequality symbols are rendered as < and > in your post. I checked with Chrome and Explorer.

    ReplyDelete
  2. I meant as & l t ; (without spaces) etc.

    ReplyDelete
  3. Well, it's quite ironic that your coment was more successful than my post. Thanks for the heads up, I'll try to fix it as soon as I get a chance!

    ReplyDelete