C++ 如何使用 auto 获取 const_iterator?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15233188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get a const_iterator using auto?
提问by virtualPN
First question: is it possible to "force" a const_iterator
using auto?
For example:
第一个问题:是否可以“强制”const_iterator
使用 auto?例如:
map<int> usa;
//...init usa
auto city_it = usa.find("New York");
I just want to query, instead of changing anything pointed by city_it
, so I'd like to have city_it
to be map<int>::const_iterator
. But by using auto, city_it
is the same to the return type of map::find()
, which is map<int>::iterator
. Any suggestion?
我只想查询,而不是更改 指向的任何内容city_it
,所以我想必须city_it
是map<int>::const_iterator
。但是通过使用 auto,city_it
与 的返回类型相同map::find()
,即map<int>::iterator
。有什么建议吗?
采纳答案by Andy Prowl
Sorry, but I just think the best suggestion is notusing auto
at all, since you want to perform a (implicitly valid) type conversion. auto
is meant for deducing the exact type, which is not what you want here.
抱歉,但我只是认为最好的建议是根本不使用auto
,因为您想执行(隐式有效)类型转换。auto
用于推断确切的类型,这不是您在这里想要的。
Just write it this way:
只需这样写:
std::map<std::string, int>::const_iterator city_it = usa.find("New York");
As correctly pointed out by MooingDuck, using type aliases can improve the readability and maintainability of your code:
正如 MooingDuck 正确指出的那样,使用类型别名可以提高代码的可读性和可维护性:
typedef std::map<std::string, int> my_map;
my_map::const_iterator city_it = usa.find("New York");
回答by jhasse
Since C++17 you can use std::as_const
like this:
从 C++17 开始,您可以std::as_const
像这样使用:
#include <utility>
// ...
auto city_it = std::as_const(usa).find("New York");
回答by Xeo
This isn't a drastically different take on conversion to const
in comparision to @Jollymorphic's answer, but I think that having a utility one-liner function like this is handy:
const
与@Jollymorphic 的答案相比,这并不是完全不同的转换方式,但我认为拥有这样的实用单行函数很方便:
template<class T> T const& constant(T& v){ return v; }
Which makes the conversion much more appealing to the eye:
这使得转换更吸引眼球:
auto it = constant(usa).find("New York");
// other solutions for direct lengths comparision
std::map<std::string, int>::const_iterator city_it = usa.find("New York");
auto city_it = const_cast<const std::map<std::string, int>&>(usa).find("New York");
Well, I'd say, bigger isn't always better. You can of course choose the name of the function according to your preferences - as_const
or just const_
are possible alternatives.
好吧,我想说,更大并不总是更好。您当然可以根据自己的喜好选择函数的名称 -as_const
或者只是const_
可能的替代方案。
回答by rustyx
A clean solution is to work with a constreference to the otherwise modifiable map:
一个干净的解决方案是使用对其他可修改映射的const引用:
const auto &const_usa = usa;
auto city_it = const_usa.find("New York");
This will make sure you can't modify const_usa
, and will use const iterators.
这将确保您无法修改const_usa
,并将使用 const 迭代器。
回答by ChetS
Another variation using auto (keeping both a mutable usa and a const usa):
使用 auto 的另一个变体(同时保留可变 usa 和 const usa):
map<std::string, int> usa;
//...init usa
const auto &const_usa = usa;
auto city_it = const_usa.find("New York");
If you don't need the map to be mutable at all after init there are some other options.
如果您在 init 之后根本不需要地图可变,那么还有一些其他选项。
you can define usa as const and init it with a function call:
您可以将 usa 定义为 const 并使用函数调用对其进行初始化:
const map<std::string, int> usa = init_usa();
auto city_it = usa.find("New York");
or using a lambda to init a const map:
或使用 lambda 来初始化 const 映射:
const auto usa = [&]()->const map<std::string, int>
{
map<std::string, int> usa;
//...init usa
return usa;
}();
auto city_it = usa.find("New York");
回答by snips-n-snails
In C++11, you can do this:
在 C++11 中,你可以这样做:
decltype(usa)::const_iterator city_it = usa.find("New York");
回答by Jollymorphic
I'm not in a position to test this right now, but I think it'll do the trick:
我现在无法对此进行测试,但我认为它可以解决问题:
auto city_it = const_cast< const map<int> & >(usa).find("New York");
回答by Sid Sarasvati
You can use auto to "track" a type or "deduce" a type:
// deduce
auto city_it = usa.find("New York");
您可以使用 auto 来“跟踪”一种类型或“推断”一种类型:
// deduce
auto city_it = usa.find("New York");
// track
auto city_it = std::map<int>::const_iterator( usa.find("New York"));
// track
auto city_it = std::map<int>::const_iterator( usa.find("New York"));
Also, watch is modern c++ style talks by Herb Sutter, which covers most of these type deductions guidance. https://youtu.be/xnqTKD8uD64
此外, watch 是 Herb Sutter 的现代 C++ 风格演讲,其中涵盖了大多数此类类型推导指南。 https://youtu.be/xnqTKD8uD64