- Boost / Library Documentation / Range / Adaptors / indexed
- boost/range/adaptor/indexed.hpp
#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()) { /* ... */ }
参考:
- teratail / 2016-03-02: C++ 範囲ベースforの中でインデックス番号を取り出す方法
タグ
コメントをかく