草庐IT

const-string

全部标签

c++ - unordered_map<const T, int> 和 map<const T, int> 的区别

#include#include#includeusingnamespacestd;classSolution{public:private://unordered_mapmapStrInt;//Case1:OK//unordered_mapmapStrInt;//Case2:Fail//mapmapStrInt;//Case3:OK//mapmapStrInt;//Case4:OK};问题>为什么Case2不合法?template,//unordered_map::hasherclassPred=equal_to,//unordered_map::key_equalclassAllo

c++ - 为什么我不能用 const_iterator 调用模板基类构造函数?

这个问题在这里已经有了答案:WhereandwhydoIhavetoputthe"template"and"typename"keywords?(8个答案)关闭8年前。由于某些原因,下面的代码给出了错误Symbol'TemplateBase'couldnotberesolved.:templateclassTemplateBase{TemplateBase(std::map::const_iteratoranIterator){}};classSubClass:publicTemplateBase{SubClass(std::map::const_iteratoranIterator)

c++ - std::map 中的 std::string 导致 Valgrind 内存泄漏

这是Valgrind的输出:==6519==at0x4C25885:operatornew(unsignedlong)(vg_replace_malloc.c:319)==6519==by0x4EE65D8:std::string::_Rep::_S_create(unsignedlong,unsignedlong,std::allocatorconst&)(new_allocator.h:104)==6519==by0x4EE7CE0:char*std::string::_S_construct(charconst*,charconst*,std::allocatorconst&,s

c++ - 定期使用 const_cast 作为设计工具是否可以接受?

我查看了下面的代码类型,虽然我对问题(*)有个人答案,但我希望得到C++/设计专家的评论。出于某种原因,Data是一个具有不可修改标识符和可修改值的对象:classData{constIdm_id;//设计选择变成了语言选择,因为标识符在类级别(**)被声明为const,以避免它的(意外)修改,即使是在类成员函数内部.........但是如您所见,有一个复制赋值运算符,其实现方式为:Data&Data::operator=(constData&that){if(this!=&that){const_cast(this->m_id)=that.m_id;this->m_value=tha

c++ - 如何找到应该是 const 的 C++ 函数?

我有这个代码:#includeclassA{public:intdoit(){return5;}intdoit2()const{i++;returni;}inti;};intmain(){Aa;printf("%d\n",a.doit());return0;}使用g++-Wall-Wpedanticmain.cpp可以干净地编译。有没有办法让g++说“A::doit()应该标记为const”?g++4.8有-Wsuggest-attribute=const但在这种情况下它似乎不起作用。g++-Wall-Wpedantic-Wsuggest-attribute=constconst_ma

c++ - 复制初始化期间不会发生 std::string 的隐式构造

我正尝试在main()函数中复制初始化我的CObj类:#include#includeclassCObj{public:CObj(std::stringconst&str):m_str(str){std::cout但是,即使std::string是从charconst*隐式构造的,CObjobj="hello"行也无法编译>。根据我在这里的理解,这应该有效。有什么理由不这样做吗?如果我这样做,它会起作用:CObjobj=std::string("hello"); 最佳答案 文字"Hello"的类型为constchar[6]:为了调用

c++ - Lint 更喜欢 std :string rather than clear() 上的 erase() 方法

使用Lint,它反复反对在std字符串上使用clear,并希望建议使用不带参数的删除方法。查看文档并执行我想要的操作,即将字符串的大小调整为零个元素,以便该字符串为空且没有任何元素。此时我不想删除字符串,只需从中删除所有成员即可。我的问题是这两种方法有什么区别,有谁知道我应该使用erase而不是clear方法的原因吗? 最佳答案 我看不出任何原因。恰恰相反:我实际上不得不查看引用以查看erase()是否可以在没有参数的情况下调用。erase()用于从字符串中删除一些指定的字符。clear()用于删除字符串的全部内容(但不包括其容量)

c++ - 如何干净地使用:const char* 和 std::string?

tl:drHowcanIconcatenateconstchar*withstd::string,neatlyandelegantly,withoutmultiplefunctioncalls.Ideallyinonefunctioncallandhavetheoutputbeaconstchar*.Isthisimpossible,whatisanoptimumsolution?初始问题到目前为止,我在C++中遇到的最大障碍是它如何处理字符串。在我看来,在所有广泛使用的语言中,它处理字符串的能力最差。我见过其他与此类似的问题,这些问题的答案要么是“使用std::string”,要么只

c++ - Typedef、模板和 const 关键字

我在使用带有const关键字(用于函数参数类型)的模板时遇到问题,为了说明这一点,我创建了一个小代码:templatestructMethodCallerFactory{typedefReturnType(*Type)(ClassType*,Args...);templatestructMethod{staticTypecreateMethodCaller(){ReturnType(*caller)(ClassType*,Args...)=[](ClassType*obj,Args...args)->ReturnType{ReturnType(ClassType::*ptr)(Args

c++ - 这是 `` const_cast`` 的有效用法吗?

C++11标准更改了erase()的签名标准容器的方法:他们现在接受const_iterators而不是iterator秒。本文档解释了基本原理:http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2350.pdf现在,如果一个人执行std::vector直接用constT*就可以了和T*分别作为常量和可变迭代器类型。所以在erase()方法我们可能有这样的代码:iteratorerase(const_iteratorit){...for(;it!=end()-1;++it){//Destroythecurrenteleme