我正在使用C ++ 11,我想知道处理现有C ++字符串最优雅的是什么,它只包含下面这些有效字符。效率也是一个问题,但最重要的是寻找优雅。
" 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ _-&#34 ;;
谢谢你, 维吉尔。
答案 0 :(得分:9)
我在这里:
void removeDisallowed(std::string& in) {
static const std::string allowed = "01234...";
in.erase(
std::remove_if(in.begin(), in.end(), [&](const char c) {
return allowed.find(c) == std::string::npos;
}),
in.end());
}
如果你想提高效率,可以制作一套:
std::unordered_set<char> allowedSet(allowed.begin(), allowed.end());
并将支票更改为:
return !allowedSet.count(c);
[更新]基于很多好的评论和答案,我建议只写一个:
template <typename F>
void erase_if(std::string& in, F func) {
in.erase(std::remove_if(in.begin(), in.end(), func));
}
然后实际上尝试使用所有提议的func
来运行它,并查看哪一个最适合您的用例。这不会与Dietmar的答案一起工作,所以你必须单独尝试那个,但他们可能都值得一试。
答案 1 :(得分:7)
似乎最优雅的方法是使用正则表达式(注意括号方括号):
std::regex const filter("[^0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-]");
str = std::regex_replace(str, filter, "");
根据对绩效的评论,我制定了一个快速基准,并在github上进行了检查。它比较了一些建议。以下是使用高优化选项的最新版gcc和clang在MacOS笔记本上运行的结果摘要。显示的数字是以μs为单位处理冗长文本文档所用的时间:
benchmark gcc clang
regex (build 186131 552697
regex (prebuild) 177959 566353
use_remove_if_str_find 44802 40644
use_remove_if_find 88377 123237
use_remove_if_binary_search 54091 64065
use_remove_if_ctype 13818 12901
use_remove_if_hash 81341 58582
use_remove_if_table 9033 10203
前两个基准测试使用上面发布的正则表达式方法,而其他基准测试使用Barry's std::remove_if()
使用不同的方法在lambda中实现谓词。为了澄清名称,概述了所做的事情(在lambda内部,根据需要与erase()
结合使用等):
text = std::regex_replace(text, std::regex("[^" + allowed + "]"), "")
text = std::regex_replace(text, filter, "")
(构建正则表达式不在时机内)std::remove_if(... a.find(c))
std::remove_if(... std::find(a.begin(), a.end(), c) == a.end())
std::remove_if(... std::binary_search(a.begin(), a.end(), c))
std::remove_if(... isalnum(c) || c == '-' || c == '_')
std::remove_if(... unordered_set.count(c))
std::remove_if(... table[c])
有关详细信息,请查看source。
答案 2 :(得分:6)
这可能过于简单,但我会考虑使用适合几个缓存行的常量时间查找表。
void remove_disallowed(std::string &str)
{
static const char disallowed[] = {
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
};
str.erase(std::remove_if(str.begin(), str.end(), [&](char c) {
return disallowed[static_cast<unsigned char>(c)];
}), str.end());
}
答案 3 :(得分:2)
#include <array>
#include <string>
#include <limits>
#include <iostream>
#include <algorithm>
#include <unordered_set>
void keep_chars_in_set(std::string &s, const std::unordered_set<char> &chars) {
s.erase(
std::remove_if(s.begin(), s.end(), [&chars](const char c) {
return !chars.count(c);
}),
s.end());
}
void keep_sorted_chars(std::string &s, const std::string &sorted_chars) {
s.erase(
std::remove_if(s.begin(), s.end(), [&sorted_chars](const char c) {
return !std::binary_search(sorted_chars.begin(), sorted_chars.end(), c);
}),
s.end());
}
using lookup_table = std::array<bool, std::numeric_limits<unsigned char>::max()>;
lookup_table make_lookup_table(const std::string &s) {
lookup_table t = {};
for (auto c : s) {
t[static_cast<size_t>(c)] = true;
}
return t;
}
void keep_chars_in_lookup_table(std::string &s, const lookup_table &table) {
s.erase(
std::remove_if(s.begin(), s.end(), [&table](const char c) {
return !table[static_cast<size_t>(c)];
}),
s.end());
}
int main() {
using namespace std;
string s1 = "abcdefxabc";
string s2 = "abcdefyabc";
string s3 = "abcdefzabc";
const unordered_set<char> set_of_chars = {'a', 'b', 'c', 'd', 'e', 'f'};
keep_chars_in_set(s1, set_of_chars);
cout << s1 << endl;
keep_sorted_chars(s2, "abcdef");
cout << s2 << endl;
const lookup_table &char_lookup_table = make_lookup_table("abcdef");
keep_chars_in_lookup_table(s3, char_lookup_table);
cout << s3 << endl;
}
注意:
binary_search
应该比find
更快:O(lg N)vs O(N),完整解是O(M lg N)vs O(M * N)。unordered_set
不是连续的数据结构,因此,即使搜索是O(1)(使用完整解决方案(O(M))),它也可能不是缓存友好的,因此您应该进行配置。