草庐IT

operation_type

全部标签

c++ - 为什么data()和c_str()返回的是char const*,而operator[]返回的是char&?

为什么std::string::data和std::string::c_str()返回指向const字符的指针,而std::string::operator[]返回对可变字符的引用?std::stringstring("eightfoldisthegreatest");autos=string.data();*s='r';//illegalautot=&string[0];*t='r';//totallyfineauto&c=string[0];c='r';//totallyfine为什么std::string::data()和std::string::c_str()不返回char*,

java - 鸭子类型(duck typing)和泛型编程

我在SO里找了一会儿,没有找到一个明确的、笼统的答案,只有一些矛盾的、特殊的意见。[1]所以我想知道鸭子类型(ducktyping)和泛型编程之间有什么关系?(DTGP)。通过泛型编程,我特别提到了C++模板或Java泛型,但如果可能的话,与概念相关的一般性答案将受到欢迎。我知道泛型编程将在编译时处理,而duck类型将在运行时处理,但我不知道如何定位它们。最后,我不想展开辩论,所以我更喜欢诸如理由、理由之类的答案。[1]What'stherelationshipbetweenC++templateandducktyping? 最佳答案

C++11 decltype : How to declare the type that a pointer points to?

我有以下代码:#includeintmain(){int*a=newint(2);std::unique_ptrp(a);}导致这些错误信息:Infileincludedfroma.cpp:1:Infileincludedfrom/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/memory:81:/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/

c++ - 根据 value_type 调用适当的构造函数 : integer or float

我有一个函数,它使用均匀分布将最小值和最大值之间的随机值填充到容器中。#include#include#include#includetemplatevoiduniform_random(TContainer&container,consttypenameTContainer::value_typemin,consttypenameTContainer::value_typemax){std::random_devicerd;std::mt19937gen(rd());//Belowlinedoesnotworkwithintegerscontainerstd::uniform_rea

c++ - 在哪里可以找到 C++ STL 中 vector 的 size_type 定义?

将vector的size()函数的结果转换为unsignedint似乎是安全的。不过,我怎么能确定呢?我的文档不清楚size_type是如何定义的。 最佳答案 不要假定容器大小的类型(或在其中键入的任何其他内容)。今天?目前最好的解决方案是使用:std::vector::size_type其中T是您的类型。例如:std::vector::size_typei;std::vector::size_typej;std::vector>::size_typek;(使用typedef可以帮助使其更好地阅读)对于迭代器和“内部”STL容器中的

Qt : has initializer but incomplete type 中的 C++ 错误

voidFindWords::getTextFile(){QFilemyFile(":/FindingWords2.txt");myFile.open(QIODevice::ReadOnly);QTextStreamtextStream(&myFile);QStringline=textStream.readAll();myFile.close();ui->textEdit->setPlainText(line);QTextCursortextCursor=ui->textEdit->textCursor();textCursor.movePosition(QTextCursor::S

C++:为什么为字符串定义了 'operator+=' 而不是 'operator+'?

这个问题在这里已经有了答案:Whatarethebasicrulesandidiomsforoperatoroverloading?(8个答案)关闭8年前。为什么operator+=是为std::string定义的,但是operator+没有定义?请参阅下面我的MWE(http://ideone.com/OWQsJk)。#include#includeusingnamespacestd;intmain(){stringfirst;first="Day";first+="number";cout

c++ - operator>> 可以读取 int hex AND decimal 吗?

我可以说服C++中的operator>>读取hex值AND和decimal值吗?下面的程序演示了读取十六进制是如何出错的。我希望相同的istringstream能够读取hex和decimal。#include#includeintmain(intargc,char**argv){intresult=0;//std::istringstreamis("5");//thisworksstd::istringstreamis("0x5");//thisfailswhile(is.good()){if(is.peek()!=EOF)is>>result;elsebreak;}if(is.fai

c++ - 为什么 operator = 不从模板类继承

我有以下模板代码:classClassName{};templateclassTemplatePtr{public:voidoperator=(T*p){}};classTemplatePtr_ClassName:publicTemplateePtr{public:~TempaltePtr_ClassName();};voidTest(){TemplatePtr_ClassNamedata;data=newClassName;}但编译失败并显示错误消息(VS2008):errorC2679:binary'=':nooperatorfoundwhichtakesaright-handop

c++ - "' void* ' is not a pointer-to-object type"在没有 void* 的代码中?

我的代码有问题。在Xcode或使用C++11编译器中,此代码运行良好。但是,当我将此代码提交给在线法官时,判决显示“编译错误”。我认为他们使用的是C++4.7.1编译器,当我尝试编译它(使用Ideone)时,它说:prog.cpp:Infunction'voidprintArray(int)':prog.cpp:27:error:'void*'isnotapointer-to-objecttypeprog.cpp:27:error:'void*'isnotapointer-to-objecttypeprog.cpp:27:error:'void*'isnotapointer-to-ob