hack のためのネタ帳, etc,,,

Boost.Range の boost::adaptors::indexed を用いる方法

ドキュメント等

サンプル

#include <boost/range/adaptor/indexed.hpp>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>

int main(int argc, char *argv[])
{
  std::vector<std::string> args;
  args.assign(&argv[1], &argv[argc]);
  for (const auto &arg : args | boost::adaptors::indexed()) {
    std::cout << arg.index() << ": " << arg.value() << std::endl;
  }
  
  return EXIT_SUCCESS;
}
namespace をまじめに書くべきか悩むところだが、
boost::adaptors::indexed は構造体として宣言されているため
typedef か using namespace を用いて以下のいずれかの宣言しておくと
typedef boost::adaptors::indexed indexed;
using namespace boost::adaptors;
以下のように簡潔に書く事も出来る。
for (const auto &arg : args | indexed()) { /* ... */ }

参考:

コメントをかく


「http://」を含む投稿は禁止されています。

利用規約をご確認のうえご記入下さい

Wiki内検索

フリーエリア

管理人/副管理人のみ編集できます