草庐IT

const_iterators

全部标签

c++ - 为什么 VS 和 gcc 在这里调用不同的转换运算符(const vs non-const)?

这段代码当然很蠢,但我写它只是为了说明问题。在这里:#includeusingnamespacestd;structfoo{inta=42;templateoperatorT*(){cout(&a);}templateoperatorconstT*()const{cout(&a);}templateTget(){coutoperatorT();}};intmain(){foomyFoo;cout()使用VisualStudio2019(ISOC++17,/Ox)编译时的输出是:Tget()operatorconstT*()const42gcc8.3(-std=c++17,-O3)的输出

c++ - 通过 const 获取参数并通过 const 返回的函数的生命周期延长

在C++中,当你有以下情况时:std::stringget_string();std::stringconst&v=get_string();从get_string()返回的临时对象的生命周期延长为与引用v相同的生命周期;如果我有以下内容:std::stringconst&get_string(std::stringconst&p){returnp;}std::stringconst&v=get_string(std::string{"Hello"});临时的生命周期是否延长?或者这是悬空引用;我的理解是临时绑定(bind)到p的生命周期并且它只在函数的持续时间内存在,并且对临时对象的

c++ - 如何分配一个 std::pair 其中一个组件类型为 const?

我正在尝试编写一个与std::map兼容的关联容器。为此,我必须创建一个插入方法,该方法以std::pair的形式接受一个新项,其中第一个组件是const类型。例如:std::pairp.我遇到的问题是这样的对象不能分配给另一个对象。因此,在我的MapCompatibleContainer的内部代码中,我无法将新对复制到私有(private)变量(std::vector)。我该如何解决这个问题?谢谢 最佳答案 正如您所说,您不能分配给const对象。标准容器通过分配原始内存并就地构造对象来解决这个问题。复制构造仍然有效。此外,关联容

c++ - 字符串隐式转换运算符到 const char*/wchar_t *

我发现MFC/ATLCString类在Win32C++代码中非常方便;特别是我发现我们可以将CString的实例直接传递给Win32API的LPCWSTR(即constwchar_t*)参数很方便,感谢CString定义的隐式转换运算符。相反,当使用std::wstring时,需要显式调用.c_str()方法。那么,为什么STL字符串类(std::string和std::wstring)需要显式方法调用(c_str())而不是定义一个隐式的constchar*/constwchar_t*转换运算符?隐式转换运算符是否隐藏着严重的陷阱? 最佳答案

c++ - 重载 ‘pow(const double&, double)’的调用不明确

我是C++的新手,这是我的问题:我需要这个数量:h=pow(mesh.V()[i0],1.0/3);但是每次编译程序时都会收到此错误消息:callofoverloaded‘pow(constdouble&,double)’isambiguous如果我写doubleV=mesh.V()[i0];h=pow(V,1.0/3);我得到:callofoverloaded‘pow(double&,double)’isambiguous现在我想我明白了constdouble&和double&指的是什么,但是我怎样才能转换constdouble&到double?谢谢! 最

C++ 二进制文件和迭代器 : getting away with a 1:1 using ifstreambuf_iterator?

Thisanswer指出C++不太适合二进制文件的迭代这一事实,但这是我现在需要的,简而言之,我需要以“二进制”方式对文件进行操作,是的,所有文件都是二进制的,即使是.txt的,但我正在写一些对图像文件进行操作的东西,所以我需要读取结构良好的文件,数据是否以特定方式排列。我想读取数据结构中的整个文件,例如std::vector所以我几乎可以立即关闭文件并处理内存中的内容,而无需再关心磁盘I/O。目前,根据标准库对文件执行完整迭代的最佳方法是std::ifstreamifs(filename,std::ios::binary);for(std::istreambuf_iterator>i

c++ - const 引用在 C++ 中是否有外部链接?

根据C++1998标准第3.5节的第3条,const引用具有内部链接。Anamehavingnamespacescope(3.3.5)hasinternallinkageifitisthenameofanobject,reference,functionorfunctiontemplatethatisexplicitlydeclaredstaticor,anobjectorreferencethatisexplicitlydeclaredconstandneitherexplicitlydeclaredexternnorpreviouslydeclaredtohaveexternall

c++ - 将 const char** 与模板特化结合使用

我正在尝试为返回数值数组的最大值(通用版本)或C字符串数组的最长C字符串(特化)的函数编写模板特化。如果我不使用const-ness,我的函数原型(prototype)看起来像这样templateTmaxn(T*my_Tptr,unsignedintn);templatechar*maxn(char**my_cstrings,unsignedintn);并且我的代码可以编译。但是,如果我尝试使用const-ness,我的函数原型(prototype)将如下所示,templateTmaxn(constT*my_Tptr,unsignedintn);templatechar*maxn(co

c++ - Qt 5 : const char * QString cast

在Qt4中,可以自动将QString转换为“constchar*”,例如我可以将QString传递给需要“constchar*”的函数。voidmyFunction(constchar*parameter);QStringmyString;myFunction(myString);//worksinQT4然而,在Qt5中,我会收到“错误C2440:‘typecast’:无法从‘QString’转换为‘constchar*’”(这是VisualC++2008编译器,其他编译器会抛出类似的东西)。如果我对文档的理解正确,那是因为QT5中不再包含Qt3兼容层。当然,我可以更改函数调用myFu

c++ - 使用 C++11 auto 作为 const 函数对象的返回类型

我有一个const函数对象,暂时它返回void。但可以返回int或double。我正在用c++11风格编写代码,只是想使用auto作为返回类型。尽管代码可以编译,但我不确定它是否100%正确。这是代码。templatestructmy_func{public:my_func(){}my_func(graph_t&_G):G(_G){}templateautooperator()(edge_tedge)->voidconst{//dosomethingwiththeedge.}//operatorprivate:graph_t&G;};//callthefunctor:(passgrap