Skip to content

Instantly share code, notes, and snippets.

std::string mirror_ends(const std::string& in)
{
// mismatch takes two ranges as input, (first1,last1,first2,last2)
// if last2 is not given, it considers first2+(last1-first1) as last2
return std::string(in.begin(), std::mismatch(in.begin(), in.end(), in.rbegin()).first);
}
int main()
{
std::cout << mirror_ends("abXYZba") << '\n'
std::vector<int> collection={1,9,9,4,2,6};
// How many 9s are there in collection?
int nines=std::count(begin(collection),end(collection),9);
// How many elements of the collection are even?
int evens=std::count_if(begin(collection),end(collection),[](int x) {
return x%2==0;
});
// nines equals 2, evens equals 3
std::vector<int> collection={2,4,4,1,1,3,9};
// notice that we pass x as reference!
std::for_each(begin(collection),end(collection),[](int &x) {
x=x*26;
});
std::vector<int> collection = {3, 6, 12, 6, 9, 12};
// Are all numbers divisible by 3?
// divby3 should equal 1
bool divby3 = std::all_of(begin(collection), end(collection), [](int x) {
return x % 3 == 0;
});
// Is any number divisible by 2?
// divby2 equals true
body .gist .highlight {
background: #272822;
}
body .gist .blob-num,
body .gist .blob-code-inner,
body .gist .pl-s2,
body .gist .pl-stj {
color: #f8f8f2;
}
body .gist .pl-c1 {