Replace BOOST_FOREACH with “pure” C++11 alternativ

2019-04-03 20:30发布

问题:

Is it possible to replace the BOOST_FOREACH in this example with a "pure" C++11 equivalent?

#include <map>
#include <functional>
#include <boost/foreach.hpp>
#include <iostream>

int main() {
  std::map<int, std::string> map = {std::make_pair(1,"one"), std::make_pair(2,"two")};
  int k;
  std::string v;
  BOOST_FOREACH(std::tie(k, v), map) {
    std::cout << "k=" << k << " - " << v << std::endl;
  }
}

The key feature being keeping the key/value pair in the references to k and v.

I tried:

for(std::tie(k,v) : map)
{
  std::cout << "k=" << k << " - " << v << std::endl;
}

and

auto i = std::tie(k,v);
for(i : map)
{
  std::cout << "k=" << k << " - " << v << std::endl;
}

But none of the ranged based for loop ideas seemed to work. Presumably the ranged based for loop is required to have a declaration before the :, since even:

std::vector<int> test;
int i;
for (i : test);

Isn't valid.

The closest equivalent I can find is:

for (auto it = map.begin(); it!=map.end() && (std::tie(k,v)=*it,1); ++it)
{
  std::cout << "k=" << k << " - " << v << std::endl;
}

which isn't quite as succinct as the BOOST_FOREACH version!

Is there a way to express the same thing succinctly without boost in C++11?

回答1:

for (auto & i : map)
{
    std::tie(k,v) = i;
    // your code here
}


回答2:

This produces the same output as the Boost macro

for( auto const& k : map ) {
  std::cout << "k = " << k.first << " - " << k.second << std::endl;
}