推断的类型“auto it = unordered_map.find(key)”? [英] Deduced type of "auto it = unordered_map.find(key)"?

查看:267
本文介绍了推断的类型“auto it = unordered_map.find(key)”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

随着C ++ 11的出现,我们有unordered_map.cbegin / cend专门返回了const_iterator的值。因此在表达式auto it = unordered_map.cbegin()中推导出的it类型是const_iterator。



但是,当涉及到unordered_map.find )函数,我认为可能会缺少一个cfind()对应,具体返回一个const_iterator。



有人说我们可以使用const auto it = unordered_map但是我强烈怀疑const iterator是相同的const_iterator,其中const iterator限制了改变迭代器本身的能力,而const_iterator限制了改变迭代器引用的内容的能力。



所以,真的,如果我们想充分利用auto类型的演绎auto类型的扣除 - auto,auto& amp; const auto& amp;等)的混淆或变化,我怎样才能有unordered_map.find(key)返回一个const_iterator,而不必明确指定const_iterator -



下面是一个简单的示例代码演示编译器的行为:

  #includestdafx.h
#include< unordered_map>

int _tmain(int argc,_TCHAR * argv [])
{
typedef std :: unordered_map< int,int& umi
umiit umii;

auto it0 = umii.find(0);
it0-> second = 42;
const auto it1 = umii.find(0);
it1-> second = 42;
umiit :: const_iterator it2 = umii.find(0);
it2-> second = 42; //预期的编译器错误:赋值给const

return 0;
}


解决方案

我不知道任何接受 const_iterator 的地方,你不能简单地传递一个迭代器,所以这个缺陷可能不会干扰与日常代码写作。然而,我更喜欢使用 const_iterator s(和一般来说 const )无论我不需要改变,一般沟通的利益,所以我想添加一个 cfind()可能是对未来标准库的一个有用的补充。



我认为这个代码可以作为一个简单的解决方法,你想要实现的:

  template<类型名T> 
auto use_as_const(T const& t) - > T const& {
return t;
}

这是一个简单的转换封装函数,类似于 move() forward (),以提供(并记录)对象的各个用法的约束。你可以这样使用它:

  auto it1 = use_as_const(umii).find(0); 

这也可以用来代替 cbegin c $ c>和 cend()。或者,它可以在基于范围的循环中使用:

  for(auto& element:use_as_const(some_vector_of_string)){ 
cout<<元件;
// element =; //这行不应该编译。
}

在上面的循环示例中,虽然我通常喜欢 auto const& element:... ,我相信这是不必要的,元素仍然会被推导为一个常量引用。 / p>

With the advent of C++11, we have unordered_map.cbegin/cend to specifically return us values of const_iterator. so the deduced type of 'it' in the expression "auto it = unordered_map.cbegin()" is const_iterator.

However, when it comes to unordered_map.find(key) function, I think there may be missing a "cfind()" counterpart, which returns a const_iterator specifically.

Some say that we can use "const auto it = unordered_map.find(key)" to obtain a "const iterator", but I have a strong suspicion that "const iterator" is the same "const_iterator", where "const iterator" limits the ability to change the iterator itself, while "const_iterator" limits the ability to change the content the iterator is referring to.

So, really, if we want to take advantage of "auto" type deduction fully (with the knowledge of the confusions or the variations of "auto" type deduction - auto, auto&, const auto&, etc.), how can I have unordered_map.find(key) to return a "const_iterator" without me having to explicitly specify "const_iterator" - that's after all the best use case for auto!

Below is a simple example code that demonstrates the compiler behavior:

#include "stdafx.h"
#include <unordered_map>

int _tmain(int argc, _TCHAR* argv[])
{
    typedef std::unordered_map<int, int> umiit;
    umiit umii;

    auto it0 = umii.find(0);
    it0->second = 42;
    const auto it1 = umii.find(0);
    it1->second = 42;
    umiit::const_iterator it2 = umii.find(0);
    it2->second = 42; // expected compiler error: assigning to const

    return 0;
}

解决方案

I'm not aware of any place that takes a const_iterator where you couldn't simply pass an iterator instead, so this deficiency may not interfere much with day-to-day code writing. However, I do prefer to use const_iterators (and const in general) wherever I don't need mutating, in the interests of general communication, so I think adding a cfind() might be a useful addition to the future standard library.

I think this code could function as a simple workaround for what you're trying to achieve, though:

template<typename T>
auto use_as_const( T const &t ) -> T const & {
    return t;
}

This is a simple casting wrapper function, similar in style to move() and forward<T>(), to provide (and document) a constraint on individual usages of the object. You could then use it like this:

auto it1 = use_as_const( umii ).find(0);

This could also be used instead of leaning on cbegin() and cend(). Or, it could be used in range-based for loops:

for ( auto &element : use_as_const( some_vector_of_string ) ) {
    cout << element;
    // element = ""; // This line shouldn't compile.
}

In the above loop example, although I would generally prefer auto const &element : ..., I believe it would be unnecessary and element would still be deduced to be a const reference.

这篇关于推断的类型“auto it = unordered_map.find(key)”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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