草庐IT

prefix-operator

全部标签

C++ std::stringstream operator<< 重载

我有以下类(class)(原型(prototype)):classToken{public://members,etc.friendstd::stringstream&operator运算符是这样实现的:std::stringstream&operator现在,我尝试像这样使用它:std::stringstreamout;Tokent;//initialization,etc.outVS给我报错,说 最佳答案 std::stringstream&operator应该是std::ostream&operator

c++ - 为什么 iostream 对象不重载 operator bool?

在thisanswer我谈到了使用std::ifstream对象到bool的转换来测试流是否仍处于良好状态。我查看了Josuttis的书以获取更多信息(如果您感兴趣,请参阅第600页),事实证明iostream对象实际上重载了operatorvoid*。当流是错误的(可以隐式转换为false)时它返回一个空指针,否则返回一个非空指针(隐式转换为true)。他们为什么不直接重载operatorbool? 最佳答案 看起来C++0x标准部分27.4.4.3有答案(强调我的)。operatorunspecified-bool-type()

c++ - 错误 : passing 'const T' as 'this' argument of 'bool T::operator<(T)' discards qualifiers

#include#include#includeclassMyData{public:intm_iData;booloperatormyvector(2,MyData());myvector[0].m_iData=2;myvector[1].m_iData=4;std::sort(myvector.begin(),myvector.end());}尝试编译这个给出:error:passing'constMyData'as'this'argumentof'boolMyData::operator 最佳答案 比较运算符将在类实例的常量引

c++ - 在 C++11 中编写 Copy/Move/operator= 三重奏的 "correct"方法是什么?

至此,复制构造函数和赋值运算符对的编写就定义好了;快速搜索将使您找到大量有关如何正确编码这些内容的信息。既然移动构造函数已经加入进来,是否有新的“最佳”方式? 最佳答案 最好,它们只是=default;,因为成员类型应该是对您隐藏移动细节的资源管理类型,比如std::unique_ptr。只有那些“低级”类型的实现者才应该费心去处理它。请记住,如果您持有外部(对您的对象)资源,您只需要费心移动语义。它对“平面”类型完全没用。 关于c++-在C++11中编写Copy/Move/operat

c++ - "operator char*"问题

下面的代码预计会打印“kevin”但是,它正在打印垃圾值。我已经检查了调试器。“operatorchar*”调用返回的指针无效。有什么想法吗?classWrapper{private:char*_data;public:Wrapper(constchar*input){intlength=strlen(input)+1;_data=newchar[length];strcpy_s(_data,length,input);}~Wrapper(){delete[]_data;}operatorchar*(){return_data;}};intmain(){char*username=Wr

c++ - 显式指定通用 lambda 的 operator() 模板参数是否合法?

以下C++代码是否符合标准?#includeintmain(){[](autov){std::cout(42);}clang++3.8.0和g++7.2.0compilethiscodefine(编译器标志为-std=c++14-Wall-Wextra-Werror-pedantic-errors)。 最佳答案 这确实符合标准。该标准指定必须有一个成员operator(),并且它在其paramater-declaration-clause中为每次出现的auto提供一个模板参数。没有禁止明确提供这些内容的措辞。行的底部:lambda的

C++ operator+ 和 operator+= 重载

我正在用C++实现自己的矩阵类,以帮助我加深对该语言的理解。我在某处读到,如果您有一个有效的+=运算符,请在您的+运算符中使用它。这就是我所拥有的:templateconstMatrix&Matrix::operator+(constMatrix&R){Matrixcopy(*this);returncopy+=R;}这里是+=运算符重载:templateconstMatrix&Matrix::operator+=(constMatrix&second_matrix){//Learnhowtothrowerrors....if(rows!=second_matrix.getNumRow

【解决】No toolchains found in the NDK toolchains folder for ABI with prefix: arm-linux-androideabi

在调试代码过程中,遇到了ndk报错的问题,这里记录下原因和解决方法。首先明确什么是NDK全名:NativeDevelopmentKit,是Android的一个工具开发包NDK是属于Android的,与Java并无直接关系。作用:快速开发C、C++的动态库,并自动将so和应用一起打包成APK即可通过NDK在Android中使用JNI与本地代码(如C、C++)交互应用场景:在Android的场景下使用JNI即Android开发的功能需要本地代码(C/C++)实现相对于Android编程来讲,NDK编程属于更偏向底层的编程。在程序编译过程中出现了“NotoolchainsfoundintheNDKt

c++ - string::at 和 string::operator[] 有什么区别?

我在学校学过string::at,但通过探索字符串库,我看到了string::operator[],这是我以前从未见过的。我现在正在使用operator[]并且从那以后就没有使用过at,但是有什么区别呢?这是一些示例代码:std::stringfoo="myredundantstringhassometext";std::cout它们在输出方面基本相同,但是否存在一些我不知道的细微差别? 最佳答案 是的,有一个主要区别:使用.at()在operator[]时对传递的索引进行范围检查,如果超出字符串的末尾则抛出异常在那种情况下只会带来

c++ - 字符串匹配 : Computing the longest prefix suffix array in kmp algorithm

KMPalgorithmforstringmatching.以下是code我在网上找到了计算最长前缀-后缀数组的方法:定义:lps[i]=thelongestproperprefixofpat[0..i]whichisalsoasuffixofpat[0..i].代码:voidcomputeLPSArray(char*pat,intM,int*lps){intlen=0;//lengthofthepreviouslongestprefixsuffixinti;lps[0]=0;//lps[0]isalways0i=1;//theloopcalculateslps[i]fori=1toM