Skip to content

Instantly share code, notes, and snippets.

@fabiogaluppo
Last active January 1, 2016 12:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fabiogaluppo/3a5b24cde3afc1dfed19 to your computer and use it in GitHub Desktop.
Save fabiogaluppo/3a5b24cde3afc1dfed19 to your computer and use it in GitHub Desktop.
std::vector<std::string> makewords(std::string&& s)
{
std::vector<std::string> temp;
std::string buffer;
for (char ch : s)
{
if (is_ascii_lower(ch) || is_ascii_upper(ch))
{
buffer += ch;
}
else if (!buffer.empty())
{
temp.emplace_back(std::move(buffer));
}
}
return std::move(temp);
}
std::vector<std::string> lowercase(std::vector<std::string>&& ss)
{
for (auto& s : ss)
{
for (char& ch : s)
{
if (is_ascii_upper(ch))
{
ch = ch - 'A' + 'a';
}
}
}
return std::move(ss);
}
std::vector<std::string> sort(std::vector<std::string>&& ss)
{
std::sort(ss.begin(), ss.end());
return std::move(ss);
}
std::vector<std::string> unique(std::vector<std::string>&& ss)
{
ss.resize(std::distance(ss.begin(),
std::unique(ss.begin(), ss.end())));
return std::move(ss);
}
std::vector<std::string> mismatch(const char* dic_path, std::vector<std::string>&& ss)
{
std::unordered_set<std::string> dic;
std::ifstream ifs;
ifs.open(dic_path, std::ifstream::in);
if (ifs.is_open())
{
std::string buffer;
while (std::getline(ifs, buffer))
{
dic.emplace(std::move(buffer));
}
}
if (!dic.empty())
{
auto erase_point = std::remove_if(ss.begin(), ss.end(),
[&dic](std::string& s){ return dic.find(s) != dic.end(); });
ss.erase(erase_point, ss.end());
}
return std::move(ss);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment