草庐IT

const_iterator

全部标签

c++ - 为什么 `T(const T&&)` 被称为 move 构造函数?

[C++11:12.8/3]:Anon-templateconstructorforclassXisamoveconstructorifitsfirstparameterisoftypeX&&,constX&&,volatileX&&,orconstvolatileX&&,andeithertherearenootherparametersorelseallotherparametershavedefaultarguments(8.3.6).[..]为什么采用const右值引用的构造函数被标准称为“move构造函数”?一定it'sself-evident那thisprohibitsmea

C++制作指向const对象的指针数组

我正在尝试创建一个指向常量对象的非常量指针的非常量数组。我的想法是我应该能够更改数组中的指针指向的内容,但它们指向的是常量对象。我在定义这个数组时遇到了问题(它是指向Person类型对象的指针数组-一个自定义类)。我目前正在这样声明数组:Person*people[10];此外,这并没有明确指出指针指向constPersons。所以当我做这样的事情时:people[i]=&p;其中p是对constPerson类型对象的引用,它失败了。 最佳答案 当有疑问时...使用typedef(因为它是显式的,添加了更多专门的语义并完全避免了混淆

c++ - 在自定义迭代器上应用 reverse_iterator 后引用失效

我实现了一个双向迭代器,但它不是对数据结构进行操作,而是返回一个可以在两个方向上迭代计算的数学序列。事实上,我正在迭代整数,使用++和--在int上。这意味着数据不会存储在不同的结构中,因此当迭代器超出范围时,值也会超出范围。尽管如此,我希望下一个代码(最小失败示例)示例能够工作,因为迭代器始终保持在范围内。但它不起作用:(#include#include#includeclassmy_iterator:publicstd::iterator{intd_val=12;public:my_iteratoroperator--(int){std::cout();int&i=*it;if(t

c++ - 从 back_insert_iterator 中提取容器的 value_type 的特征类

std::back_insert_iterator的value_type等于void,但它还有一个protected成员container包含指向底层Container的指针。我正在尝试编写一个traits类来提取容器的value_type,如下所示:#include#include#includetemplatestructoutit_vt:OutputIt{usingself_type=outit_vt;usingvalue_type=typenamestd::remove_pointer_t().container)>::value_type;};intmain(){std::v

c++ - 在可能的情况下,C++ 是否总是更喜欢右值引用转换运算符而不是 const 左值引用?

在编写转换运算符时,如果我同时提供到constT&和T&&的转换,C++是否会尽可能优先选择右值运算符?在这个小测试中似乎是这样:#include#includestructholds{operatorint&&(){printf("moving!\n");returnstd::move(i);}operatorconstint&()const{printf("copying!\n");returni;}private:inti=0;};intmain(){holdsh;intval=h;}打印:╰─▸./testmoving!但也许有人说的专业知识比我验证的要好?

c++ - 为什么 delete 运算符可以用在 const 上下文中?

这个问题不同于:Isadestructorconsideredaconstfunction?new-expressionanddelete-expressiononconstreferenceandconstpointerDeletingapointertoconst(Tconst*)我写了一个这样的类Test。classTest{private:int*p;public://constructorTest(inti){p=newint(i);}Test&operator=(constTest&rhs){deletep;p=newint(*(rhs.p));return*this;}}

c++ - 为什么 C++ 允许但忽略将 const 应用于函数类型?

我从探索C++的不寻常角落中得到真正的乐趣。从thisquestion了解了真正的函数类型而不是函数指针,我试着搞乱函数类型并想出了这个奇怪的案例:typedefintFunc(int);intFoo(intx){return1;}intmain(){constFunc*&f=&Foo;return0;}因为&Foo是Func*类型的右值,我想我应该可以把它放在一个const引用中,但是我从g++4.6得到这个错误:funcTypes.cpp:Infunction‘intmain()’:funcTypes.cpp:7:23:error:invalidinitializationofno

c++ - 错误 : passing const xxx as this argument of xxx discards qualifiers

我在将仿函数从Windows移植到Linux时遇到问题。(传递给STL::map以进行严格弱排序的仿函数)原文如下:structstringCompare{//Utilizedasafunctorforstl::mapparameterforstringsbooloperator()(stringlhs,stringrhs){//Returnstrueiflhs由于linux不支持_stricmp而是使用strcasecmp,我将其更改为:structstringCompare{booloperator()(stringlhs,stringrhs){//Returnstrueiflhs

c++ - 只读内存映射寄存器在 C 中定义为 `volatile const` 但在 C++ 中仅定义为 `volatile`

在使用AtmelSAM3X8E处理嵌入式系统项目时,我注意到某些CMSIS头文件中有以下代码。#ifndef__cplusplustypedefvolatileconstuint32_tRoReg;/**为什么C++的typedef不包含const?我在某处看到有人提到C++不会在运行时内存中存储整数const变量,如果为真,则意味着const需要被删除,因为微Controller寄存器是如何映射内存的,但我可以'似乎没有找到任何其他说明C++可以做到这一点的内容(尽管我的搜索确实非常简短)。没有太多的C++经验,我还认为可能是C++不允许const结构成员,因为这些typedef主要

C++ 连接字符串导致 "invalid operands of types ‘const char*’ 和 ‘const char"

我想连接两个字符串,但出现错误,我不知道如何克服这个错误。有什么方法可以将这个constchar*转换为char吗?我应该使用一些取消引用吗?../src/main.cpp:38:error:invalidoperandsoftypes‘constchar*’and‘constchar[2]’tobinary‘operator+’make:***[src/main.o]Error1但是,如果我尝试以这种方式组成“bottom”字符串,它会起作用:bottom+="|";bottom+=tmp[j];bottom+="";这是代码。#include#include#include#inc