草庐IT

const_set

全部标签

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++ - 指向 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++ - 引用 set for c++ 中的最大/最小 int

假设我有以下使用C++中的集合的示例:seta;for(inti=0;i如何找到上面显示的集合示例的最大值和最小值?理想情况下,我认为以下内容会起作用,但会出现以下错误:error:cannotconvert'std::_Rb_tree_const_iterator'to'int'inassignment我正在使用以下函数来尝试获取最大值/最小值:min=a.begin();max=a.end(); 最佳答案 首先,begin和end返回迭代器,需要对(*)进行间接寻址,得到它们指向的元素在.其次,end返回尾后迭代器,因此实际上并

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::set、std::map 等的 begin/end/rbegin/rend?

对于std::set和std::map等以对数时间查找的数据类型,实现是否需要维护开始和结束迭代器?访问begin和end是否意味着查找可能以对数时间发生?我一直假设开始和结束总是在常数时间内发生,但是我在Josuttis中找不到任何对此的证实。既然我正在做一些我需要对性能有所了解的事情,我想确保涵盖我的基础。谢谢 最佳答案 它们发生在常数时间内。我正在查看ISO/IEC14882:2003标准的第466页:表65-容器要求a.开始();(恒定的复杂性)a.end();(恒定的复杂性)表66-可逆容器要求a.rbegin();(恒定