0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Semantic action内でmultimapに格納したデータをmulti-indexに格納

Last updated at Posted at 2020-10-17

Boostには、multi-indexというスペックがあるらしい。

↓ テンプレートテクニックのポリシーのところを読んでいたら発見した。
https://www.sbcr.jp/product/4797376685/

multi-indexの部分を見てみる。。

35typedef multi_index_container<
36  session,
37    indexed_by<
38      ordered_unique<tag<linenumber>,   member<session, int, &session::linenumber> >,
39      ordered_unique<tag<source_ipaddr>, member<session, std::string, &session::source_ipaddr> >,
40      ordered_unique<tag<dest_ipaddr>, member<session, std::string, &session::dest_ipaddr> >,
41      sequenced<tag<seq> >
42      >
43  > dictionary;

今週、Boost Semantic Actionからマルチマップ(multimap)に値を格納する
https://qiita.com/Ruo_Ando/items/c10cbb551f1ce616d139
を書いたが、semantic actionからmulti-indexへのインサートがうまくいかなさげなので、(*´Д`)
マルチマップに格納したあとにマルチインデックスに格納することにした。


   149    for (auto& x:m) {
   150      // std::cout << x.first << " => " << x.second << std::endl;                                                                                                                     
   151
   152          for (auto& y:m) {
   153            if(x.first == y.first && x.second != y.second)
   154              {
   155                // std::cout << x.first << "," << x.second << "," << y.second << std::endl;                                                                                           
   156                dict.insert(session(x.first, x.second,  y.second));
   157              }
   158          }
   159    }
   161    for (auto & it: dict.get<0>()) {
   162      std::cout << it.linenumber << "," << it.source_ipaddr << "," << it.dest_ipaddr << std::endl;
   163    }

コードはこちら
https://github.com/RuoAndo/qiita/blob/master/ipaddress6.cpp

実行してみる。。。(IPアドレスはランダムに生成)


$ cat random_data.txt 
"2019/07/02 02:02:00.839","2019/07/02 02:02:00","2019/07/02 02:02:00","841","255.178.167.132","25846","iy","69.215.218.225","51321","bu","8MP","VX13Gpdkt","drN","deETa","gyAY4gdZ","8","TJoPPQuOKvrxzAjCd11rpqqSqs","912","198","336","769","278","554","rand-pa1"
"2019/07/02 02:02:52.006","2019/07/02 02:02:52","2019/07/02 02:02:52","478","104.40.197.93","41214","ol","76.213.36.241","23907","Vu","OYF","H6zQlnN5X","yV3","P2VPw","9D3viFsS","5","cogAhSIycmvdYl7RaZNjGCsWqj","953","917","636","718","142","607","rand-pa1"
"2019/07/02 12:12:16.086","2019/07/02 12:12:16","2019/07/02 
$ ./a.out random_data.txt 
line:0 255.178.167.132:0
文字数:14
 内容:69.215.218.225,0
line:0 69.215.218.225:0
文字数:13
 内容:104.40.197.93,1
line:1 104.40.197.93:0
文字数:13
 内容:76.213.36.241,1
line:1 76.213.36.241:0
文字数:14
 内容:120.175.225.23,2
line:2 120.175.225.23:0
文字数:13
 内容:55.156.36.246,2
line:2 55.156.36.246:0
Displaying elements...
0,255.178.167.132,69.215.218.225
1,104.40.197.93,76.213.36.241
2,120.175.225.23,55.156.36.246

(`ー´)b

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?