Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace BOOST_FOREACH with "pure" C++11 alternative?

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?

like image 881
Flexo Avatar asked May 09 '12 20:05

Flexo


2 Answers

for (auto & i : map)
{
    std::tie(k,v) = i;
    // your code here
}
like image 116
Benjamin Lindley Avatar answered Oct 17 '22 06:10

Benjamin Lindley


This produces the same output as the Boost macro

for( auto const& k : map ) {
  std::cout << "k = " << k.first << " - " << k.second << std::endl;
}
like image 45
Praetorian Avatar answered Oct 17 '22 06:10

Praetorian