草庐IT

new_string

全部标签

C++——应该使用 "new Car"还是 "new Car()"?

这个问题在这里已经有了答案:关闭11年前。PossibleDuplicate:Dotheparenthesesafterthetypenamemakeadifferencewithnew?大家好,classCar{public:Car():m_iPrice(0){}Car(intiPrice):m_iPrice(iPrice){}private:intm_iPrice;};int_tmain(intargc,_TCHAR*argv[]){Carcar1;//Line1Carcar2();//Line2,thisstatementdeclaresafunctioninstead.Car*

c++ - 将 vector<string> 转换为 vector<double> 的便捷方法

除了使用变换算法和istringstream之外,C++中是否有任何方便的方法将vector转换为vector?非常感谢您的帮助! 最佳答案 lexical_cast相当“方便”。for(size_ti=0;i(vec[i]));}当然,使用std::transform会变得更加有趣:std::transform(strings.begin(),strings.end(),std::back_inserter(doubles),boost::lexical_cast);//Notethetwotemplatearguments!at

c++ - 如何在 Visual Studio C++ 2010 中将 BSTR 转换为 std::string?

我正在处理COMdll。我希望将BSTR转换为std::string以传递给采用常量引用参数的方法。似乎使用_com_util::ConvertBSTRToString()来获取BSTR的char*等价物是一种合适的方法。但是,API文档很少,而且实现可能存在问题:http://msdn.microsoft.com/en-us/library/ewezf1f6(v=vs.100).aspxhttp://www.codeproject.com/Articles/1969/BUG-in-_com_util-ConvertStringToBSTR-and-_com_util例子:#inclu

c++ - 释放 : A** mat = new A*[2]; 的内存

我定义了:A**mat=newA*[2];但是我怎样才能删除它呢?使用delete[]mat;或delete[]*mat;? 最佳答案 它是delete[]mat;仅当您不进行额外分配时。但是,如果您在数组数组中分配了数组,则还需要删除它们:A**mat=newA*[2];for(inti=0;i!=2;i++){mat[i]=newA[5*(i+3)];}...for(inti=0;i!=2;i++){delete[]mat[i];}delete[]mat; 关于c++-释放:A**m

c++ - C++中使用String-To-Class Lookup table实例化类

寻找一种方法来避免大量IF/ELSE并使用查找表将字符串解析为特定类以进行实例化,所有这些类都派生自基类。这样的事情是否可能,如果可能,如何实现?typedefstructBaseClass{}BaseClass;typedefstructDerivedClassOne:BaseClass{}DerivedClassOne;typedefstructDerivedClassTwo:BaseClass{}DerivedClassTwo;typedefstruct{constchar*name;BaseClassclass;}LookupList;LookupListlist[]={{"C

c++ - strcmp 无法将 ‘std::string {aka std::basic_string<char>}’ 转换为‘const char*

这个问题在这里已经有了答案:strcmporstring::compare?(6个答案)关闭8年前。提前为问题的基本性质道歉。我正在尝试使用strcmp函数来测试两个字符串的匹配字符。我将问题简化为下面的简单代码:#include#includeusingnamespacestd;voidcompareStrings(string,string);intmain(){stringstring1="testString",string2="testString";compareStrings(string1,string2);return0;}voidcompareStrings(str

简单易行的matplotlib中英文混排(设置中文为宋体,英文为times new roman)

先看效果:普通混排支持tex文本的混排:以下是代码:普通混排importmatplotlib.pyplotaspltfrommatplotlib.font_managerimportFontProperties#设置字体plt.rcParams['font.family']=['SimSun','TimesNewRoman']#设置字体族,中文为SimSun,英文为TimesNewRomanplt.rcParams['mathtext.fontset']='stix'#设置数学公式字体为stix#绘制图像plt.plot([1,2,3,4,5],[1,4,9,16,25])plt.title(

c++ - 为什么 COW std::string 优化在 GCC 5.1 中仍然启用?

根据GCC5发布更改页面(https://gcc.gnu.org/gcc-5/changes.html):Anewimplementationofstd::stringisenabledbydefault,usingthesmallstringoptimizationinsteadofcopy-on-writereferencecounting我决定检查一下并写了一个简单的程序:intmain(){std::stringx{"blah"};std::stringy=x;printf("0x%X\n",x.c_str());printf("0x%X\n",y.c_str());x[0]=

c++ - 使用 std::string 调用类方法

假设我有以下类(class):(可能是元生成的)classMyClass{public:myMethod();...}这里假设一些事情:1.)Ihavetheclassnamefromsomewhere(let'spretend)2.)Ihavethenamesoftheclassmethodssomewhere(std::map...perhaps?)所以...因为我可能直到运行时才知道myMethod()的名称,有没有办法使用std::string调用它?这是假设我在某处存储了一个类函数的名称。MyClassexample;std::stringfuncName{findMyMet

c++ - 如何使用 placement new 确定对象是否已放置

使用placementnew语法,我应该能够做这样的事情:char*buffer=newchar[sizeof(MyClass)];//pre-allocatedbufferMyClass*my_class=new(buffer)MyClass;//putdaclassthere现在假设我只做第一行,而不做第二行。有没有一种方法可以在代码中确定是否已正确分配缓冲区,但那里尚未实例化MyClass类型的对象? 最佳答案 该语言没有提供任何内置机制来提供该信息,至少我所知道的没有。您必须添加自己的簿记代码来跟踪此类信息。