「boost::split」の版間の差分

提供: C++入門
移動: 案内検索
(Daemon がページ「Boost split」を「Boost::split」に、リダイレクトを残さずに移動しました)
行3: 行3:
 
-->
 
-->
  
C++で文字列を分割する場合には、 boost::split を利用すると簡単にできます。
+
C++で文字列を分割する場合には、 [[boost::split]] を利用すると簡単にできます。
  
 
読み方
 
読み方
行18: 行18:
 
どちらも実装しないといけないので、非常に面倒です。
 
どちらも実装しないといけないので、非常に面倒です。
  
もっと簡単にやる方法は、 boost::split を使うことです。
+
もっと簡単にやる方法は、 [[boost::split]] を使うことです。
  
また、文字列の前後のデミリタの処理には、[[Boost trim]]を利用するとよいでしょう。
+
また、文字列の前後のデミリタの処理には、[[Boost::trim]]を利用するとよいでしょう。
  
 
== ヘッダファイル ==
 
== ヘッダファイル ==
行235: 行235:
  
 
行頭、行末は、 trim_if / trim でスペースを取り除くしか無いようです。
 
行頭、行末は、 trim_if / trim でスペースを取り除くしか無いようです。
もしくは、 [[Boost tokenizer]] のほうが簡単にできるかもしれません。
+
もしくは、 [[Boost::tokenizer]] のほうが簡単にできるかもしれません。
  
詳しくは、[[Boost trim]] をご参照ください。
+
詳しくは、[[Boost::trim]] をご参照ください。
  
 
== 関連項目 ==
 
== 関連項目 ==
  
* [[Boost tokenizer]]
+
* [[Boost::tokenizer]]
* [[Boost trim]]
+
* [[Boost::trim]]
 
* [[Boost]]
 
* [[Boost]]
 
* [[C++ライブラリ]]
 
* [[C++ライブラリ]]

2013年3月10日 (日) 22:21時点における版


C++で文字列を分割する場合には、 boost::split を利用すると簡単にできます。

読み方

ぶーすと すぷりっと

概要

C言語の場合、文字列を分解するときに、strtok を使ったりします。 C++の場合は、string の find_first_of を使って、ループを回していくやりかたもあります。

どちらも実装しないといけないので、非常に面倒です。

もっと簡単にやる方法は、 boost::split を使うことです。

また、文字列の前後のデミリタの処理には、Boost::trimを利用するとよいでしょう。

ヘッダファイル

#include <boost/algorithm/string.hpp>   // boost:spplit

ソースコード

スペースで区切られた文字列をスペースで分割し、 list に入れる例です。

#include <boost/algorithm/string.hpp>
#include <string>
#include <list>
#include <iostream>
 
#include <boost/foreach.hpp>
using namespace std;
int
main ()
{
        string str ("192.168.0.1 192.168.0.2");
 
        list<string> list_string;
 
        boost::split(list_string, str, boost::is_space());
 
        BOOST_FOREACH(string s, list_string) {
                cout << s << endl;
        }
        return 0;
}

文字列を分割する文字(デミリタ)がカンマの場合は、以下のようになります。

#include <boost/algorithm/string.hpp>
#include <string>
#include <list>
#include <iostream>
 
#include <boost/foreach.hpp>
using namespace std;
int
main ()
{
        string str ("192.168.0.1,192.168.0.2");
        string delim (",");
        list<string> list_string;
 
        boost::split(list_string, str, boost::is_any_of(delim));
 
        BOOST_FOREACH(string s, list_string) {
                cout << s << endl;
        }
        return 0;
}

複数のデリミタを指定できます。

#include <boost/algorithm/string.hpp>
#include <string>
#include <list>
#include <iostream>
 
#include <boost/foreach.hpp>
using namespace std;
int
main ()
{
        string str ("127.0.0.1-192.168.0.1*192.168.0.2 192.168.0.3");
        string delim (" *-");
 
        list<string> list_string;
 
        boost::split(list_string, str, boost::is_any_of(delim));
 
        BOOST_FOREACH(string s, list_string) {
                cout << s << endl;
        }
        return 0;
}

コンパイル

clang++ -I/usr/local/include boost_split.cpp

デミリタを複数指定する

複数のデミリタを指定したいこともあるでしょう。

デミリタには、複数の文字を指定できます。

#include <iostream>
 
#include <boost/foreach.hpp>
using namespace std;
int
main ()
{
        string str ("127.0.0.1-192.168.0.1*192.168.0.2 192.168.0.3");
        string delim (" *-");
 
        list<string> list_string;
 
        boost::split(list_string, str, boost::is_any_of(delim));
 
        BOOST_FOREACH(string s, list_string) {
                cout << s << endl;
        }
        return 0;
}

実行例

127.0.0.1
192.168.0.1
192.168.0.2
192.168.0.3

空文字列を無視する

デミリタが複数重なった場合などは、空の文字列が list に入ってしまいます。

たとえばこういうときには

        string str ("127.0.0.1-----     192.168.0.1****   *192.168.0.2 192.168.0.3");
        string delim (" *-");
        list<string> list_string;
        boost::split(list_string, str, boost::is_any_of(delim) );

下記のようになってしまいます。

% ./a.out
127.0.0.1
 
 
 
 
 
 
 
 
 
192.168.0.1
 
 
 
 
 
 
 
192.168.0.2
192.168.0.3
%

空文字列は、不要なこともあるでしょう。

boost::split の第3引数にパラメータをセットすることで、空文字列を無視できます。

#include <boost/algorithm/string.hpp>
#include <string>
#include <list>
#include <iostream>
#include <boost/foreach.hpp>
using namespace std;
int
main ()
{
        string str ("127.0.0.1-----     192.168.0.1****   *192.168.0.2 192.168.0.3");
        string delim (" *-");
 
        list<string> list_string;
 
        boost::split(list_string, str, boost::is_any_of(delim), boost::algorithm::token_compress_on );
 
        BOOST_FOREACH(string s, list_string) {
                cout << s << endl;
        }
        return 0;
}

実行例

% ./a.out
127.0.0.1
192.168.0.1
192.168.0.2
192.168.0.3
%

先頭や末尾のデミリタは取り除くしかない

下記の場合、最初と最後に空白が入ってしまいます。

        string str ("    127.0.0.1-----     192.168.0.1****   *192.168.0.2 192.168.0.3    ");
        string delim (" *-");
        list<string> list_string;
        boost::split(list_string, str, boost::is_any_of(delim), boost::algorithm::token_compress_on );

行頭、行末は、 trim_if / trim でスペースを取り除くしか無いようです。 もしくは、 Boost::tokenizer のほうが簡単にできるかもしれません。

詳しくは、Boost::trim をご参照ください。

関連項目