簡體   English   中英

為什么在C ++函數boost :: algorithm :: join_中拋出std :: bad_cast異常?

[英]Why in C++ function boost::algorithm::join_if a std::bad_cast exception is thrown?

我在代碼中發現了問題。 當我使用boost :: algorithm :: join時,它可以正常工作,但是當我使用boost :: algorithm :: join_if時,會拋出bad_cast。 我的代碼如下:

#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
}

我的程序的輸出是:

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)

我曾經使用過boost :: algorithm函數來處理文本,幾次沒有使用謂詞 ,但是沒有發生過類似的問題。

我什至嘗試將const char *替換為std :: string:

cout << boost::algorithm::join_if(players, string(", "), boost::is_alpha()) << endl;

但是問題還是一樣。

編輯:我想一個解決方案,也可以在比C ++ 11早的C ++中使用

boost::is_alpha用於字符

使用方式如下:

cout << boost::algorithm::join_if(players, ", ",
                          [](const std::string & s){
                          return boost::all(s,boost::is_alpha()); 
                          }) << endl;

這里很明顯,你不會得到任何輸出空間' '和數字存在於players

使用boost::alnum()代替。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM