模板字符/wchar_t,字符串/w字符串,cout/wcout,regexp / wregex(或任何可能的解决方法)
template char/wchar_t, string/wstring, cout/wcout, regexp/wregex (or any possible workaround)
我正在处理char
和wchar_t
。
我正在编写一个辅助字符串类,该类对某些字符串进行了一些正则表达式(带有 boost),但我既有string
又有wstring
。现在我有 2 个函数,每个函数都有重复的代码。
int countFoo(const char *s, const char *foo) {
string text(s);
boost::regex e(foo);
int count = 0;
boost::smatch match;
while ( boost::regex_search( text, match, e ) ) {
text = match.suffix();
count++;
}
return count;
}
int countFoo(const wchar_t *s, const wchar_t *foo) {
wstring text(s);
boost::wregex e(foo);
int count = 0;
boost::wsmatch match;
while ( boost::regex_search( text, match, e ) ) {
text = match.suffix();
count++;
}
return count;
}
它有效,但我正在寻找一些优雅的方法(模板?一些魔术?函数指针?)来删除重复的代码。
您可以将其编写为模板,如下所示:
template <typename charT>
int countFoo(const charT *s, const charT *foo) {
basic_string<char> text(s);
boost::basic_regex<charT> e(foo);
int count = 0;
boost::match_results<typename basic_string<charT>::const_iterator> match;
while ( boost::regex_search( text, match, e ) ) {
text = match.suffix();
count++;
}
return count;
}
相关文章:
- 在C++中获取命名空间名称的任何可移植技巧
- 如何从 pjsip 发送电子邮件.是否有任何可用于发送电子邮件的默认方法
- C 功能接受给定模板签名的任何可呼叫
- VS 静态字符串是否可写
- QT C++函数,具有字符串的可选参数
- std::函数,返回任何可求和类型
- 如何从任何可调用的函数中获得有意义的函数签名
- 在c++ 11中,是否有一种方法可以在调用以任何可调用对象(包括绑定方法)作为参数的函数时不需要模板参数
- 如何将任何可迭代类型作为函数参数传递
- 用于MFC应用程序的任何可测试的架构或设计模式
- 字符串化可用于USERNAME:PASSWORD,但不适用于SERIAL:TOKEN
- c++ std::sort():仅使用一个比较函数的任何可比较成员的对象向量
- 字符串::交换可忽略,因为 std::swap(s1, s2) 就足够了
- 任何可在facebook上共享/发送邮件的cocos2d-X功能
- 在c++中打印一个空字符串的可观察行为
- 用宏生成文件,不生成任何可执行文件
- 有任何可观察的Qt容器类吗?
- C++系统头是否有任何可保证的依赖性
- 是否有一种统一的方法来存储和传递任何可调用的对象
- 我是否应该声明任何可以为const的方法为const方法?