什么是C ++ 98等效的自动迭代器参考? [英] What's the C++98 equivalent of the auto iterator reference?

查看:195
本文介绍了什么是C ++ 98等效的自动迭代器参考?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的开发环境有RHEL 5.8,它不支持GCC 4.8+现代(C ++ 11 +)编译器。我预计有一天我们会到那里,所以我有一个头文件,其中我定义宏基于C ++ 11支持级别,所以我可以这样做:

My development environment has RHEL 5.8 which does not support GCC 4.8+ modern (C++11+) compilers. I anticipate that someday we'll get there, so I have a header file where I define macros based on C++11 support levels so I can do something like this:

#if defined(CPP11_auto_type_inference) && defined(CPP11_range_based_for_loops)
  for (auto vit : args)
#else
  std::vector<std::string>::const_iterator vit, vend;
  for (vend=args.end(),vit=args.begin(); vit != vend; ++vit)
#endif
  { // process arguments...
    std::cout << "Processing \"" << *vit << '"' << std::endl;
    . . .
  } // end "process arguments" loop

在C ++ 98中,相当于迭代器引用(或者更准确地说dereferenced iterator?),如下所示:

So, what I'm trying to do in C++98 is the equivalent of the iterator reference (or is it more accurate to say "a dereferenced iterator"?), like below:

for (auto& it : args)
  std::cout << "Processing \"" << it << '"' << std::endl;

对于我的生活,我不知道如何获得一个解引用的迭代器在C ++ 98。我可以模拟,如下:

For the life of me, I cannot figure out how to get a dereferenced iterator (or iterator reference) in C++98. I can simulate, as below:

#if defined(CPP11_auto_type_inference) && defined(CPP11_range_based_for_loops)
  for (auto& it : args) {
#else
  std::vector<std::string>::const_iterator vit, vend;
  for (vend=args.end(),vit=args.begin(); vit != vend; ++vit) {
    std::string it(*vit);
#endif
    std::cout << "Processing \"" << it << '"' << std::endl;
    . . .
  }

...但我真的希望这不是答案。

... but I'm really hoping that is not the answer.

的C ++ 98等价物是(auto& it:vec),还是不可能?是否只能模拟它,但是取消引用迭代器并在每次迭代中创建一个副本?

What is the C++98 equivalent of for (auto& it : vec), or is it not possible? Is it only possible to "simulate" it but dereferencing the iterator and creating a copy each iteration?

如果是这种情况,覆盖C ++ 11 auto& 语法? (我不得不相信这是不是。)无论如何,使用(auto& it:vec)

And if that be the case, is that what is going on "under the covers" with C++11 auto& syntax? (I have to believe this is not the case.) In any case, is it more costly to use for (auto& it : vec) than for (auto it : vec)?

推荐答案

我想你将要花费太多的精力来模拟C ++ 11的功能。现在可以使用C ++ 98/03方法。

I think you are going into too much trouble to emulate C++11 functionality. You can use C++98/03 method for now. They will continue to work when you are able to use a C++11 compiler.

这样,你可以使用:

#if defined(CPP11_auto_type_inference) && defined(CPP11_range_based_for_loops)
  for (auto vit : args)
#else
  std::vector<std::string>::const_iterator viter, vend;
  for (vend=args.end(),viter=args.begin(); viter != vend; ++viter)
  {
     std::string vit = *viter;
#endif
     { // process arguments...
       std::cout << "Processing \"" << vit << '"' << std::endl;
       . . .
     } // end "process arguments" loop
#if defined(CPP11_auto_type_inference) && defined(CPP11_range_based_for_loops)  
#else
  }
#endif

如果你想仿效

  for (auto& vit : args)

您可以使用:

     std::string const& vit = *viter;

这篇关于什么是C ++ 98等效的自动迭代器参考?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆