Notes from:
// Function template to return product of two
// values of unknown types:
template<typename T, typename U>
auto product(const T &t, const U &u) -> decltype (t * u)
{
return t * u;
}
// findNull: Given a container of pointers, return an
// iterator to the first null opointer (or the end
// iterator if none is found)
template<typename Cont>
auto findNull(const Cont &c) -> decltype(begin(c))
//in C++14, don't need decltype
{
auto it = begin(c);
for (; it != end(c); ++it)
if (*it == nullptr)
break;
return it; //in C++14, return type deduced here
}
//C++98
pair<set<string>::iterator, bool> result = myset.insert("Hello");
if(result.second) use(result.first);
//C++11
auto result = myset.insert("Hello");
if(result.second) use(result.first);
//C++11
tie(**iter, success**) = myset.insert("Hello");
if(success) use(iter);
vector<int> foo()
{
vector<int> v {10, 20, 30};
v.insert(end(v), {40, 50, 60});
return {100, 200, 300};
}
struct S
{
int i, j;
};
int main()
{
for (auto x: foo())
cout << x << " ";
cout << endl;
S s1 = {5, 10};
S s2{5, 10};
}
[variable1, &variable2][=] or [&] default mode[=, &variable1] default plus special casesint main()
{
vector<double> v {1.2, 4.7, 5, 9, 9.4};
double target = 4.9;
double epsilon = .3;
auto endMacheds = partition(begin(v), end(v),
[target, epsilon](double val)
{ return fabs(target - val) < epsilon; });
for_each(begin(v), endMaches, [](double d)
{ cout << d << ' '; });
}
vector<shared_ptr<string>> vps;
sort(begin(vps), end(vps), []
(const auto &p1, const auto &p2) { return *p1 < *p2 });
auto getsize = []
(auto const& c) { return c.size(); };