草庐IT

-std=gnu99

全部标签

c++ - std 容器的模板 typedef(没有专门化)?

是否可以在std容器上使用typedef而无需专门化它?这样的代码有效:typedefstd::vectorintVector;但是对于这段代码:templatetypedefstd::vectorDynamicArray;我得到一个错误:templatedeclarationof'typedef'在C++中可以做到这一点吗?? 最佳答案 是的,在C++11中。templateusingDynamicArray=std::vector;(并不是说您应该使用这个确切的别名。) 关于c++-

c++ - 为什么在 std::vector 中使用索引超出范围的运算符 [] 时没有出现异常?

为什么当我使用下面的代码时我没有得到超出范围的异常?std::vectorv;v.resize(12);intt;try{t=v[12];}catch(std::exceptione){std::cout 最佳答案 通过使用operator[],您实际上是在告诉编译器“我知道我在做什么。相信我。”如果您访问数组之外​​的某些元素,那是您的错。你违反了这种信任;你不知道你在做什么。另一种方法是使用at()方法。在这里,您要求编译器对您的访问进行健全性检查。如果它们超出范围,您将获得异常。这种健全性检查可能代价高昂,尤其是在某些深度嵌套

c++ - std::hash 是否保证 "equal" float 的哈希值相等?

关于almost-equality,std::hash的浮点特化(例如,double或float)是否可靠??也就是说,如果两个值(例如(1./std::sqrt(5.)/std::sqrt(5.))和.2)应该比较相等但不会使用==运算符这样做,std::hash将如何表现?那么,我能否依靠double作为std::unordered_map键来按预期工作?我看过“Hashingfloatingpointvalues”,但那是关于提升的问题;我问的是C++11保证。 最佳答案 std::hash对所有类型都有相同的保证被实例化:如

c++ - 是否可以使 std::string 始终包含小写字符串?

是否可以使std::string始终包含小写字符串?下面是我将如何使用它:typedefstd::basic_stringlowercase_string;voidmyfunc(){lowercase_strings="HelloWorld";//noticemixedcaseprintf(s.c_str());//prints"helloworld"inlowercasestd::strings2=s;printf(s2.c_str());//prints"helloworld"inlowercase} 最佳答案 您可以编写自己的

c++ - 在 C 中模拟 std::bind

我使用std::bind提供回调,同时通过首先绑定(bind)一些参数来抽象一些逻辑。即voidstart(){intsecret_id=43534;//Bindthesecret_idtothecallbackfunctionobjectstd::functioncb=std::bind(&callback,secret_id,std::placeholders::_1);do_action(cb);}voiddo_action(std::functioncb){std::stringresult="helloworld";//Dosomethings...//Callthecall

c++ - 将 QByteArray 转换为 std::vector<unsigned char>

我尝试转换QByteArray至std::vector使用此代码:unsignedchar*buffer=(unsignedchar*)byteArrayBuffer.constData();std::vector::size_typesize=strlen((constchar*)buffer);std::vectorbufferToCompress(buffer,buffer+size);但是,假设byteArrayBuffer是QByteArray充满了数据,我认为它在线上效果不佳unsignedchar*buffer=(unsignedchar*)byteArrayBuffer

C++ - 迭代从 find_if 返回的 std::vector<>

我正在学习C++,所以我觉得这应该是一个非常简单的答案-但我似乎找不到它。所以,如果它是幼稚的,我提前道歉。我有一个std::vector的值,我试图找到奇数值的索引。我正在遵循here中的代码:(在下面重复)://find_ifexample#include//std::cout#include//std::find_if#include//std::vectorboolIsOdd(inti){return((i%2)==1);}intmain(){std::vectormyvector;myvector.push_back(10);myvector.push_back(25);my

c++ - 为什么 std::bitset 以小端方式公开位?

当我使用std::bitset::bitset(unsignedlonglong)时这构建了一个位集,当我通过operator[]访问它时,这些位似乎以小端方式排序。示例:std::bitsetb(3ULL);std::cout打印1100而不是0011即结尾(或LSB)位于小(低)地址,索引0。查找标准,它说initializingthefirstMbitpositionstothecorrespondingbitvaluesinval程序员自然会想到从LSB到MSB(从右到左)的二进制数字。因此,前M位位置可以理解为LSB→MSB,因此位0将位于b[0]。.然而,在不断变化的情况下

c++ - std::list 线程 push_back、front、pop_front

std::list线程安全吗?我假设它不是,所以我添加了我自己的同步机制(我想我有正确的术语)。但是我还是遇到了问题每个函数都由一个单独的线程调用。Thread1不能等待,它必须尽可能快std::listg_buffer;boolg_buffer_lock;voidthread1(CFooframe){g_buffer_lock=true;g_buffer.push_back(frame);g_buffer_lock=false;}voidthread2(){while(g_buffer_lock){//Wait}//CMSTP_Send_Frame*pMSTPFrame=NULL;w

c++ - 是否值得在生产中使用 std::tr1 ?

我正在使用MSVC2008和一些项目的英特尔C++编译器11.0。是否值得在生产中使用tr1特性?他们会保持新标准吗?例如,现在我使用stdext::hash_map。TR1定义了std::tr1::unordered_map。但在MS实现中unordered_map只是他们的stdext::hash_map,以另一种方式模板化。 最佳答案 Yes,everythingthat'sintr1willstaythere.Somethingswillbeacceptedinstd::,buttheywillstayintr1also.S