Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 是C+中映射标准的at()常量访问器+;11?_C++_Stl_Map_C++11 - Fatal编程技术网

C++ 是C+中映射标准的at()常量访问器+;11?

C++ 是C+中映射标准的at()常量访问器+;11?,c++,stl,map,c++11,C++,Stl,Map,C++11,我试图找出如何在const方法中从map返回值,但在GCC4.6中偶然发现了map的at()方法 当我查看这个时,我意识到它是非标准的: 但它肯定比find()方法要简单得多。我想知道C++11是否已经纠正了这一点-是新标准的映射部分吗?是的std::map在C++11中有一个at成员函数,其规格如下(23.4.4.3/9): 返回:对应于*this中x的映射_类型的引用 抛出:如果不存在类型为的异常对象,则该异常对象超出\u范围 复杂性:对数 但是请注意,此成员函数已专门添加到std::ma

我试图找出如何在const方法中从map返回值,但在GCC4.6中偶然发现了map的at()方法

当我查看这个时,我意识到它是非标准的:


但它肯定比find()方法要简单得多。我想知道C++11是否已经纠正了这一点-是新标准的映射部分吗?

是的
std::map
在C++11中有一个
at
成员函数,其规格如下(23.4.4.3/9):

返回:对应于
*this
x
的映射_类型的引用

抛出:如果不存在类型为
的异常对象,则该异常对象超出\u范围

复杂性:对数

但是请注意,此成员函数已专门添加到
std::map
。更一般的关联容器要求不需要它。如果要编写需要某种关联容器类型的泛型代码,则不能在
处使用此新的
。相反,您应该继续使用
find
,这是关联容器概念的一部分,或者编写自己的非成员帮助程序:

template <typename AssociativeContainer>
typename AssociativeContainer::mapped_type&
get_mapped_value(AssociativeContainer&                          container,
                 typename AssociativeContainer::key_type const& key)
{
    typename AssociativeContainer::iterator it(container.find(key));
    return it != container.end() ? it->second : throw std::out_of_range("key");
}

template <typename AssociativeContainer>
typename AssociativeContainer::mapped_type const&
get_mapped_value(AssociativeContainer const&                    container,
                 typename AssociativeContainer::key_type const& key)
{
    typename AssociativeContainer::const_iterator it(container.find(key));
    return it != container.end() ? it->second : throw std::out_of_range("key");
}

(或者类似的东西;关于C++11的一个有趣的事情是,没有两个编译器有相同的bug,并且似乎都接受稍微不同的有效-和无效-C++11代码子集。)

我想说,所有编译器都在接近C++11,但这可能是我乐观的自我:)[挑剔]。'at'方法不仅被添加到std::map,还被添加到std::unordered_map。这是仅有的两个标准容器,使用这种方法确实有意义。此外,您的代码不仅适用于AssociateveContainers,也适用于无序AssociateveContainers,但set和unordered_set除外,而且对于multimap和unordered_multimap来说,它几乎没有意义。@Konstantin:是的,也许
uniqueAssociateVeContainer
是模板参数的更好名称(
uniqueAssociationContainerRunniqueUnderedAssociationContainer
有点太笨拙了:-O)重点是,除了C++标准库容器之外,还有容器满足容器概念的要求,并且应该与C++代码库容器在通用代码中互换。代码依赖于< <代码> > <代码>不能利用其他容器。@马蒂厄:我不知道…SualC++ 2010接受了这个答案,但是我没有把G++ 4.6提交给它。我有一个很大的兴趣代码库,它使用C++ 0x的特性;它用Visual C++编译得很好,但是我不能用G++编译它,因为有两个bug。(一个涉及在类模板中捕获lambda,另一个涉及模板实例化失败)或clang(根本不支持lambda)@James:是的,问题是每个编译器都根据自己的优先级攻击C++0x功能,因此它们都有自己的优势,没有其他编译器介入。这当然会使可移植性变得困难:)
template <typename AssociativeContainer>
typename AssociativeContainer::mapped_type&
get_mapped_value(AssociativeContainer&                          container,
                 typename AssociativeContainer::key_type const& key)
{
    typename AssociativeContainer::iterator it(container.find(key));
    return it != container.end() ? it->second : throw std::out_of_range("key");
}

template <typename AssociativeContainer>
typename AssociativeContainer::mapped_type const&
get_mapped_value(AssociativeContainer const&                    container,
                 typename AssociativeContainer::key_type const& key)
{
    typename AssociativeContainer::const_iterator it(container.find(key));
    return it != container.end() ? it->second : throw std::out_of_range("key");
}
template <typename AssociativeContainer, typename Key>
auto get_mapped_value(AssociativeContainer&& container, Key const& key)
    -> decltype(std::declval<AssociativeContainer>().begin()->second)&
{
    auto const it(container.find(key));
    return it != container.end() ? it->second : throw std::out_of_range("key");
}