草庐IT

-std=gnu99

全部标签

c++ - 如何在 std::map 中使用结构作为键

我想使用std::map其键和值元素是结构。我收到以下错误:errorC2784:'boolstd::operator&,const_Elem*)':couldnotdeducetemplateargumentfor'conststd::basic_string&'from'constGUID我知道我应该重载operator对于那种情况,但问题是我无法访问我想使用的结构的代码(VC++中的GUID结构)。这是代码片段://.h#includeusingnamespacestd;mapmapGUID;//.cppGUIDtempObj1,tempObj2;mapGUID.insert(p

c++ - 编译错误 - std::set with const 成员

我这辈子都弄不明白这段代码有什么问题:ClassA&doSomething(std::setconst>const&someSet){std::set>secondSet;for(std::setconst>::const_iteratorit=someSet.begin();it!=someSet.end();it++){if(checkSomething(*it))secondSet.insert(boost::const_pointer_cast(*it));}}当我尝试编译时,在g++的第4行(for循环的开始)出现以下错误:/usr/include/c++/4.4/ext/n

c++ - 为什么编译器推迟 std::list 释放?

我有以下代码使用std::list容器测试内存释放:#include#include#include#include/*countofelementtoputintocontainer*/staticconstunsignedlongSIZE=50000000;/*elementusefortest*/classElement{public:Element():mId(0){}Element(longid):mId(id){}virtual~Element(){}inlinelonggetId()const{returnthis->mId;}inlinebooloperatormIdm

c++ - 如何将 `std::vector<uchar>` 保存到 `std::ostream` 中?

我们已经创建并填充了一些std::vector与openCVimencode例如。现在我们想将它流式传输到一些http_lib中,它可以采用某种ostream。(ostringstream)例如,或者我们只是想在使用ofstream调试我们的程序时保存。所以我想知道如何把std::vector进入std::ostream? 最佳答案 使用write:voidsend_data(std::ostream&o,conststd::vector&v){o.write(reinterpret_cast(v.data()),v.size())

c++ - 在 std::for_each 中调用 std::function

我本质上有以下代码:typedefstd::functionfnGlobalChangeEvent;typedefstd::vectorGlobalTriggers;inlinevoidExecuteGlobal(fnGlobalChangeEventev){ev();}GlobalTriggerstriggers;std::for_each(triggers.begin(),triggers.end(),std::bind(&ExecuteGlobal,_1));ExecuteGlobal的使用在这里感觉完全多余,但我找不到正确的语法来退出调用。std::for_each(trigg

c++ - std::ostringstream 未返回有效字符串

我正在尝试使用std::ostringstream将数字转换为字符串(char*),但它似乎不起作用。这是我的代码:#include#includeintmain(){std::ostringstreamout;out生成的消息框内根本没有文本。这让我相信对out.str().c_str()的调用返回了一个无效的字符串,但我不确定。由于到目前为止我已经对这个程序进行了缩减,但问题仍然存在,所以我一定是犯了一个令人尴尬的简单错误。感谢您的帮助! 最佳答案 out.str()返回一个std::string按值,这意味着您正在调用.c_s

c++ - 临时 std::strings 返回垃圾

我还在学习c++,所以请多多包涵。我正在围绕boost文件系统路径编写一个简单的包装器——我在返回临时字符串时遇到了奇怪的问题。这是我的简单类(这不准确,但非常接近):typedefconstchar*CString;typedefstd::stringString;typedefboost::filesystem::pathPath;classFileReference{public:FileReference(constchar*path):mPath(path){};//returnsapathStringpath()const{returnmPath.string();};//

c++ - std::endl 是否适用于 cout 和 wcout?

我想知道std::endl是否适用于std::cout和std::wcout?有人清楚吗? 最佳答案 是的。事实上,std::endl是一个函数模板,它将作为std::basic_ostream的任何特化的操纵器模板。更多细节:27.7.3.6规定std::basic_ostream模板包含operator的重载如下:basic_ostream&operator&(*pf)(basic_ostream&));在合适的函数上调用此重载的效果是returnpf(*this).所以当你说std::cout,这实际上变成了std::endl

c++ - 堆栈 std::vector 作用域

我是C++的新手,我发现很难理解某些vector行为。我试图实现一个函数来返回一个int数组,我发现了很多使用这样的vector的建议:vectormyFunc(){vectormyVector;//addelementstovectorhere...returnmyVector;}但据我所知,“myVector”是在堆栈上创建的对象,所以当函数结束时它不会超出范围吗?它的析构函数什么时候被调用?我知道关于返回vector的其他问题很少,但我需要澄清这一点,希望不会重复一个问题。 最佳答案 是的,因为myVector是在堆栈上分配的

c++ - 如何使用 std::greater 对 C++ 映射键进行排序?

我正在创建一个std::map在C++中,我更愿意让它们的键从最高到最低排序,而不是默认的排序顺序。我的研究让我找到了std::greater看起来很有前途,但在尝试使用它时出现编译错误:invalidtypeargumentofunary‘*’(have‘int’)我的map声明是:std::map>numMap;这个函数会抛出错误:voidRow::addNumber(intnum,intpos){numMap.insert(num,pos);}类似问题的答案,例如this在声明中包含括号,即std::greater()-但是当我包含这些括号时,我会收到关于函数返回函数的多个错误。