Skip to content

Instantly share code, notes, and snippets.

@jingzhehu
Created June 13, 2018 03:20
Show Gist options
  • Select an option

  • Save jingzhehu/940d4dc8851b456e7d14b45b3a778fa6 to your computer and use it in GitHub Desktop.

Select an option

Save jingzhehu/940d4dc8851b456e7d14b45b3a778fa6 to your computer and use it in GitHub Desktop.
Sort and remove
//
// Created by Jingzhe Hu on 6/12/2018 Tuesday.
//
#include <string>
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main(){
// for_each wrapper - iterate all the range
auto for_each_full = [](auto& iter, auto& lambda) {
for_each(iter.begin(), iter.end(), lambda);
cout << endl;
};
auto cout_lambda = [](auto& el) { cout << el << " "; };
auto str_tolower = [](auto& str) {
transform(str.begin(), str.end(), str.begin(), ::tolower);
return str;
};
vector<string> strs{"Jack", "jack", "john", "Valaria"};
strs.emplace_back("john");
strs.emplace_back("john");
strs.emplace_back("John");
sort(strs.begin(), strs.end());
for_each_full(strs, cout_lambda);
auto elFount = binary_search(strs.begin(), strs.end(), "John");
if (elFount) {
cout << "Found: " << elFount << endl;
}
auto newEnd = unique(strs.begin(), strs.end());
strs.erase(newEnd, strs.end());
sort(strs.begin(), strs.end(),
[&](auto lhs, auto rhs) {
return str_tolower(lhs) < str_tolower(rhs);
});
for_each_full(strs, cout_lambda);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment