Tuesday, June 28, 2016

My first C++14 code

Here is my first C++14 code.

#include <iostream>
#include <vector>
#include <algorithm>
int main()
{
std::vector<int> vec {1,2,3};
if (std::all_of(std::begin(vec), std::end(vec), [](const auto& arg) { return arg < 10; }))
{
std::cout << "All less than 10" << std::endl;
}
else
{
std::cout << "Some greater than 10" << std::endl;
}
return 0;
}
view raw cpp14.cpp hosted with ❤ by GitHub
The code uses a function (a lambda function) whose argument is an auto.

To compile and run this code in Linux:

g++-5 -std=c++14 cpp14.cpp && ./a.out

Of course, you need to install g++-5 first in order to use C++14. In Ubuntu:

sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update
sudo apt-get install g++-5

No comments:

Post a Comment