[英]Why in C++ function boost::algorithm::join_if a std::bad_cast exception is thrown?
I found a problem in my code. 我在代码中发现了问题。 When I use boost::algorithm::join it works normally, but when I use boost::algorithm::join_if a bad_cast is thrown.
当我使用boost :: algorithm :: join时,它可以正常工作,但是当我使用boost :: algorithm :: join_if时,会抛出bad_cast。 My code is below:
我的代码如下:
#include <iostream>
#include <string>
#include <list>
#include <boost/algorithm/string.hpp>
using namespace std;
main(int argc, char **argv)
{
list<string> players;
players.push_back("ProPlayer98");
players.push_back("King of Darkness");
players.push_back("Noob999");
players.push_back("Daily Queen");
cout << boost::algorithm::join(players, ", ") << endl; // it works
cout << boost::algorithm::join_if(players, ", ", boost::is_alpha()) << endl; // bad_cast
}
The output of my program is: 我的程序的输出是:
ProPlayer98, King of Darkness, Noob999, Daily Queen
terminate called after throwing an instance of 'std::bad_cast'
what(): std::bad_cast
Abort trap (core dumped)
I have used some times boost::algorithm functions to play with text, few times I was using predicates , but none of problems like that ever occurred. 我曾经使用过boost :: algorithm函数来处理文本,几次没有使用谓词 ,但是没有发生过类似的问题。
I even tried to replace const char* to std::string: 我什至尝试将const char *替换为std :: string:
cout << boost::algorithm::join_if(players, string(", "), boost::is_alpha()) << endl;
but problem is still the same. 但是问题还是一样。
EDIT: I would like a solution which works also in C++ older than C++11 编辑:我想一个解决方案,也可以在比C ++ 11早的C ++中使用
boost::is_alpha
is for characters boost::is_alpha
用于字符
Use like following:- 使用方式如下:
cout << boost::algorithm::join_if(players, ", ",
[](const std::string & s){
return boost::all(s,boost::is_alpha());
}) << endl;
Here obviously, you won't get any output as space ' '
and numerals are present in players
. 这里很明显,你不会得到任何输出空间
' '
和数字存在于players
。
Use boost::alnum()
instead. 使用
boost::alnum()
代替。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.