草庐IT

auto_mapping

全部标签

c++ - STL 允许使用指向不同 map 的迭代器删除 map 的键/值吗?

所以,我偶然发现的是:std::mapmap1;std::mapmap2;map1[2.5]=11;map1[3.5]=12;map2[2.5]=21;map2[3.5]=22;std::map::iteratoriterMap1=map1.find(2.5);//Iwillnowtrytoeraseakey/valuepairinmap2withaniterator//thatpointstomap1.Thisisbad/wrong.ButIamsurprised//thisisallowed.map2.erase(iterMap1);//whatdoyouthinkwouldbep

c++ - 通过 operator[] 访问静态成员 unordered_map

作为C++的新手,我遇到了一些我不太理解的行为,并且即使通过大量谷歌搜索也无法找到解释,所以我希望有人能解释这里到底出了什么问题。//test.h#includetypedefstd::unordered_maptest_type;classtest{public:staticconsttest_typetmap;};//test.cpp#include"test.h"consttest_typetest::tmap={{1,1}};//main.cpp#include"test.h"intmain(){//Attempt1:accesskeyviaoperator[]std::cou

C++ : double iteration through map

我想遍历一个map,但内部循环只会遍历元素的上半部分。使用vector它看起来像这样:for(autoelement1=myVector.begin();element1!=myVector.end();++element1){for(autoelement2=element1+1;element2!=myVector.end();++element2){//mystuff}}但是对于mapelement1+1返回错误nooperatormatchesthisoperand..我相信这是因为元素在map中没有排序.那么我怎样才能正确地做到这一点呢?我目前正在使用这种需要在每个循环中进行

c++ - 使用两个对象作为 unordered_map 或替代方案的哈希键

定义对象myType后,我需要存储这些对象之间的关系。这些关系存储在矩阵中。事先不知道元素的数量,并非所有元素都有关系(element1可以与element3有关系,但可能与5没有关系)并且内存是一个问题。例如它可能看起来像:element45与:具有特征[3,1;1,4]的元素3具有特征[1,1;1,1]的元素12具有特征[8,1;1,4]的元素1780element1661连接到:具有特征[3,1;6,4]的元素3具有特征[1,1;1,9]的元素1具有特征[8,1;1,1]的元素1780拥有:myType*element1;myType*element2;我想要类似的东西(正确指出

C++ : Different deduction of type auto between const int * and cont int &

这里是代码示例。a.intii=0;b.constintci=ii;c.autoe=&ci;-->eisconstint*d.auto&f=42;-->invalidinitializationofnon-constreferenceoftype‘int&’fromanrvalueoftype‘int’e.constauto&g=42-->ok观察:1.对于c)子句,自动推导类型const2.对于子句d),不会自动推导出类型const3.对于条款e),必须手动添加类型const才能使其工作。为什么子句c而不是d会自动推导类型const? 最佳答案

c++ - 'for(auto &str : vec)' 内部 for 循环的目的是什么?

我是C++的新手,正在尝试学习vector的概念。我在网上看到这段代码。我的问题是,'for(auto&str:vec)'中的内部for循环的目的是什么?为什么作者要对第一个引用(&str)创建第二个引用(&c)?intmain(){vectorvec;for(stringword;cin>>word;vec.push_back(word)){}for(auto&str:vec){for(auto&c:str){c=toupper(c);}}for(inti=0;i!=vec.size();++i){if(i!=0&&i%8==0)cout 最佳答案

c++ - operator[] for std::map 什么情况下可以返回0?

我正在使用LLVM,但我遇到了以下我没有编写的代码段的问题:staticstd::mapNamedValues;...//LotsofothercodeValue*V=NamedValues["Demostring"];returnV?V:ErrorV("VisnotinNamedValuesmap.");根据我对std::map的理解,它永远不应该返回空指针(除非它内存不足?),所以我很难理解V为0如何表示V不在映射中。照原样,我的程序总是在这里出错,但我不明白为什么。对这里发生的事情有什么帮助吗? 最佳答案 std::map::

c++ - 直接放置在 pair 的 std::map 中

为什么这段代码无法编译?std::map>m;m.emplace(1,1,1);假设我们可以编辑std::map::emplace的代码,是否可以更改它以使之前的代码有效? 最佳答案 无效的原因与无效的原因完全相同:std::pair>p{1,1,1};因为上面本质上就是map的emplace归结为。要使其正常工作,您可以使用piecewise_constructconstructorofstd::pair,正是为了这个目的而引入的:m.emplace(std::piecewise_construct,std::forward_as

c++ - 带有 if 语句的 auto 函数不会返回值

我制作了一个模板和一个auto函数,用于比较2个值并返回最小值。这是我的代码:#includeusingnamespacestd;//Templatewithavaluereturningfunction:PrintSmallertemplateautoPrintSmaller(TNumOne,UNumTwo){if(NumOne>NumTwo){returnNumTwo;}else{returnNumOne;}}intmain(){intiA=345;floatfB=23.4243;cout但它无法编译,我在VS2015上遇到此错误:错误C3487“int”:所有返回表达式必须推导出

c++ - 检测类型是否为 "mapping"

我想使用它们的::iterator成员类型将C++容器解析为另一个对象。迭代器成员类型指向单一类型(vector、队列等)的对象的容器将变成类列表对象,迭代器成员类型的容器指向一个std::pair将变成一个类似map的对象。我正在尝试编写一个成员函数来检测后一种容器,但它不起作用。这是我到目前为止所拥有的:#include#include#includetemplatestructis_pair:std::false_type{};templatestructis_pair>:std::true_type{};templateconstexprboolis_pair_v=is_pai