草庐IT

const_iterators

全部标签

c++ - const char myVar* 与 const char myVar[]

这个问题在这里已经有了答案:关闭11年前。PossibleDuplicate:Differencebetweenusingcharacterpointersandcharacterarrays有什么区别:constchar*myVar="HelloWorld!";constcharmyVar[]="HelloWorld!";如果有的话?

c++ - const_cast 不适用于 C++?

这个问题在这里已经有了答案:Twodifferentvaluesatthesamememoryaddress(7个答案)关闭5年前。我有以下代码:constintk=1;int*p=const_cast(&k);cout(&k)=12;cout输出是:kbefore=1kafter=1为什么constcast在这里不起作用?

c++ - std::is_const 将 const 指针标识为非常量

我很困惑std::is_const识别const的行为指针为非const.我自己的实现is_const做完全一样的事情。我不确定为什么更通用的模板化结构正在挑选版本。gcc4.7和clang3.1-svn表现出相同的行为。任何人都可以解释发生了什么事吗?代码如下:#include#include#includeclassCEmptyClass{};namespacejbc{templatestructis_const:std::false_type{};templatestructis_const:std::true_type{};}intmain(intargc,char*argv[

c++ - 为什么当 typedef const 指针与 extra const 一起使用时编译器不报错?

以下给出了预期的错误:int*constconstp=newint;//g++error:duplicatecv-qualifier但下面没有给出任何错误,即使它等同于上面的错误:typedefint*constintp_const;intp_constconstp=newint;//ok!//^^^^^duplicate?为什么编译器会忽略额外的const?[注:intp_constconst与constchar*const不同,因为*p=;是可能的。] 最佳答案 在7.1.5[dcl.type](C++03)中,规定在通过typ

c++ - 通过引用使用 vector<int>::iterator 但有错误

#include#includeusingnamespacestd;intmain(){vectorvec={1,2,3,4};for(auto&it=vec.begin();it!=vec.end();++it){cout大家好,在C++中,我通过引用使用迭代器,例如“auto&it”,编译器返回错误"error:invalidinitializationofnon-constreferenceoftype'__gnu_cxx::__normal_iterator>&'fromanrvalueoftype'std::vector::iterator{aka__gnu_cxx::__n

c++ - 指向 const 的指针与通常的指针(对于函数)

指向const的指针和通常的函数指针之间有什么区别吗?什么时候适合对独立函数使用const限定符?我写了简短的示例来说明我的问题:#includeusingnamespacestd;intsum(intx,inty){returnx+y;}typedefintsum_func(int,int);intmain(){constsum_func*sum_func_cptr=∑//constfunctionsum_func*sum_func_ptr=∑//non-constfunction?//Whatisthedifferencebetweensum_func_cptran

c++ - 将 System::String 转换为 Const Char *

这个问题在这里已经有了答案:howtoconvertSystem::Stringtoconstchar*?(2个答案)关闭7年前。我正在使用VisualC++2008的GUI创建器制作用户界面。单击按钮时,将调用以下函数。内容应该创建一个文件,并以文本框“文本框”的内容命名该文件,末尾带有“.txt”。但是,这导致我出现转换错误。这是代码:私有(private):System::VoidButton_Click(System::Object^sender,System::EventArgs^e){ofstreammyfile(Textbox->Text+".txt");我的文件.clo

c++ - const lambda 是什么意思?

#includeintfoo(inti){constautoa=[&i](){i=7;returni*i;};a();returni;}intmain(){std::cout这会编译(g++-std=c++11-Wall-Wextra-Wpedanticmain.cpp)并返回49。这让我感到惊讶,因为通过将a声明为常量对象,我会期望i被引用为constint&。显然不是,为什么? 最佳答案 Lambda就像非lambda一样,除了它们的实现细节是隐藏的。因此,使用非lambda仿函数可能更容易解释:#includeintfoo(i

c++ - 为什么通过 int& 捕获 const int& 有效?

在下面的代码中,我抛出一个int,将其作为constint&捕获,重新抛出并再次捕获它,将其作为int&捕获。#includeintmain(){try{try{intx=1;throwx;}catch(constint&e){std::cout以上程序编译成功并打印InnercatchOutercatch另一方面,我试图通过constint&初始化int&的以下程序甚至无法编译。#includeintmain(){intx=0;constint&y=x;int&z=yreturn0;}我得到了预期的以下错误binding‘constint’toreferenceoftype‘int&

c++ - 删除 std::list::iterator 不会使迭代器无效并销毁对象吗?

为什么下面打印2?listl;l.push_back(1);l.push_back(2);l.push_back(3);list::iteratori=l.begin();i++;l.erase(i);cout我知道erase返回什么,但我想知道为什么这样可以?或者它是未定义的,还是取决于编译器? 最佳答案 是的,这是未定义的行为。您正在取消引用一种野指针。在erase之后,您不应该使用i的值。是的,erasedestructs指向的对象。但是,对于POD类型,销毁不会执行任何操作。erase不会为被删除的迭代器分配任何特殊的“空”