且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

与&QUOT替换BOOST_FOREACH;纯QUOT; C ++ 11的替代?

更新时间:2022-10-23 15:55:24

 为(自动&安培; I:图)
{
    的std ::领带(K,V)= I;
    //你的code在这里
}

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?

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