草庐IT

mapped_type

全部标签

c++ - 前向声明的类型和 "non-class type as already been declared as a class type"

我对以下代码有疑问:templatevoidfoo(structbar&b);structbar{};intmain(){}它在GCC上编译成功,但在MSVC(2008)上编译失败并出现以下错误:C2990:“bar”:已声明为类类型的非类类型是代码错误还是MSVC中的错误?如果我在模板定义之前添加structbar;就可以了。 最佳答案 我们有我们的赢家:https://connect.microsoft.com/VisualStudio/feedback/details/668430/forward-declared-type-

c++ - 复制一张 map 有多贵?

我有一个map>在我的EntityRepresentation类(class)。我有点想为表示创建一个Builder类,但我必须考虑复制map的成本。EntityState复制起来很便宜,因为它只是静态函数的集合;boost::weak_ptr复制也很便宜。整个map怎么样? 最佳答案 不要过早优化。在许多情况下,构建器类的运行时性能不会成为瓶颈。一般来说,复制一张map的复杂度是O(n)。从评论看来,n很小。如果您已经确定您确实需要优化,那么在这种情况下,使用两个vector在访问项目和复制方面都会更便宜。

c++ - 错误 C2678 : binary '=' : no operator found which takes a left-hand operand of type 'const Recipe' (or there is no acceptable conversion)

我正在尝试对每个元素中包含一个int和一个字符串的vector进行排序。它是一个称为vector食谱的类类型的vector。出现上述错误,这是我的代码:在我的Recipe.h文件中structRecipe{public:stringget_cname()const{returnchef_name;}private:intrecipe_id;stringchef_name;在我的Menu.cpp文件中voidMenu::show()const{sort(recipes.begin(),recipes.end(),Sort_by_cname());}在我的Menu.h文件中#include

c++ - 使用 std::map 的 Visual Studio 11 编译时错误

以下代码可以使用gcc-4.5.1编译,但不能在VisualStudio11中编译。#include#includetypedefstd::pair>pairus;intmain(){std::map>x;std::arraytroll={1,2,3,4};x.insert(pairus(1,troll));autoz=x[1];}1现在映射到std::arraytroll.插入效果很好,程序可以编译。但是,一旦我尝试autoz=x[1]->因此试图获得1的数组troll被映射到,程序不编译并出现以下错误:errorC2512:'std::array::array':没有合适的默认构造

c++ - std::is_convertible 用于 type_info

在C++11中,可以通过usingstd::is_convertible确定类型A的变量是否可以隐式转换为类型B.如果你真的知道类型A和B,这很有效,但我只有type_infos。所以我正在寻找的是这样的功能:boolmyIsConvertible(consttype_info&from,consttype_info&to);是否可以在C++中实现类似的东西?如果是,怎么办? 最佳答案 在可移植的C++中做你想做的事是不可能的。可能如果您将自己限制在给定的平台上,则有可能获得部分答案。例如那些遵守ItaniumABI的平台将实现此功

c++ - CRTP + 特征类 : "no type named..."

我尝试使用模板化类实现CRTP,但在使用以下示例代码时出现错误:#includetemplateclassTraits{public:typedeftypenameT::typetype;//'staticconstunsignedintm_const=T::m_const;staticconstunsignedintn_const=T::n_const;staticconstunsignedintsize_const=T::m_const*T::n_const;};templateclassCrtp{public:typedeftypenameTraits::typecrtp_typ

c++ - std::string 的 type_traits 段错误

从UsingSFINAEtocheckforglobaloperator收集信息和templates,decltypeandnon-classtypes,我得到了以下代码:http://ideone.com/sEQc87基本上,我将两个问题的代码结合起来,如果它有ostream声明,则调用print函数,否则调用to_string方法。摘自问题1namespacehas_insertion_operator_impl{typedefcharno;typedefcharyes[2];structany_t{templateany_t(Tconst&);};nooperatorstruct

c++ - 如何实现具有不同数据类型作为值的 map ?

我想将两种(不是更多)不同的数据类型作为值放入映射中,如下例所示:typeXA,B,...;typeYZ,Y,...;voidfunc(typeX){...}voidfunc(typeY){...}std::mapmap;map["a"]=A;map["z"]=Z;...std::vectorlist;//Thislistwillbesomethinglike"a","y",...for(unsignedinti=0;i显然这是行不通的,因为map只接受一种数据类型的值。但是,当遍历list时,对func()的调用应该是明确的,因为map[list[i]]的类型是已知的.我想避免显式转

c++ - 如何散列 unordered_map?

boost::hash具有适用于大多数内置类型(包括容器)的哈希函数。但如boost::hash_rangefunctiondescription中所述,范围的哈希算法issensitivetotheorderoftheelementssoitwouldn'tbeappropriatetousethiswithanunorderedcontainer因此,std::unordered_map和boost::unordered_map都没有boost::hash特化。问题是:是否有一种“简单有效”的方法来散列unordered_map而无需从头开始重新实现散列算法?

c++ - 对于具有默认分配器的标准容器,std::container::size_type 是否保证为 size_t?

喜欢:std::string::size_typestd::list::size_typestd::map::size_typestd::vector::size_type等等两者都是cplusplus.com和cppreference.com说他们通常是size_t,但它们是否真正、明确地保证为size_t的标准除非使用自定义分配器? 最佳答案 对于STL容器-不。[container.requirements.general]中标准的表96,其中列出了任何容器的容器要求X,解释得很清楚:但是,对于basic_string,siz