草庐IT

标准ACL

全部标签

c++ - 使用标准输入重定向输入

我正在编写一个短程序来对整数数组进行排序。我在打开输入文件“prog1.d”时遇到问题。作业要求在程序目录中创建符号链接(symboliclink),我在创建对象和可执行文件后,按如下方式调用程序...prog1.exeprog1.out我知道我的冒泡排序正确且高效地工作,因为我使用了我自己的测试“txt”文件。作业说:Yourprogramgetstherandomintegersfromstdinandputstheminanarray,sortstheintegersinthearrayinascendingorder,andthendisplaysthecontentsofth

c++ - 使用标准构造函数传递临时对象

我想将一个临时对象(例如std::string)传递给我对象的构造函数:classMyClass{public:MyClass(stringa):a(a){}stringa;};intmain(intargc,char*argv[]){MyClassa(string());cout但是我收到这个错误:main.cpp:Infunction‘intmain(int,char**)’:main.cpp:28:11:error:requestformember‘a’in‘a’,whichisofnon-classtype‘MyClass(std::string(*)()){akaMyClas

c++ - 将 float 与零进行比较的标准方法是什么?

可能的问题是:比较float和零的标准方法是什么?据我所知直接比较:if(x==0){//xiszero?}else{//xisnotzero??可能会因浮点变量而失败。我曾经用过floatx=......if(std::abs(x)我找到了相同的方法here.但我看到两个问题:随机魔数(MagicNumber)1e-7f(或上面链接中的0.00005)。代码更难阅读这是一个很常见的比较,我想知道是否有一个标准的简短方法可以做到这一点。喜欢x.is_zero(); 最佳答案 要将浮点值与0进行比较,只需比较它:if(f==0)//w

c++ - 根据标准,std::vector 是否受静态初始化顺序问题的影响?

我可以在非Pod静态数据成员构造函数的构造函数中安全地将内容存储在vector中吗?示例:classFoo{public:staticFoo&instance(){staticFooinst;returninst;}voidstore(intx){numbers.push_back(x);}private:Foo(){}std::vectornumbers;};classBar{public:Bar(){Foo::instance().store(5);}};classThing{public:staticBarbar;};//inthing.cpp:BarThing::bar;上述代

c++ - 在标准 (C++11) 的哪个地方说余数运算符仅适用于整数类型?

我想要标准(n3242/3291/3290)中关于定义余数运算符不适用于浮点类型的位置的指针。余数运算符%定义在5.6.2Thebinary/operatoryieldsthequotient,andthebinary%operatoryieldstheremainderfromthedivisionofthefirstexpressionbythesecond.Ifthesecondoperandof/or%iszerothebehaviorisundefined.Forintegraloperandsthe/operatoryieldsthealgebraicquotientwit

c++ - For循环与使用相对较旧的编译器的标准库算法

我知道没有任何混淆的代码会更好for在其中循环。尽可能重用标准库算法总是好的。但是,我发现迭代器和算法的语法看起来真的很困惑。我想举一个我当前项目的真实例子:我想复制vector>in的内容进入vectorout.我看不出两者之间的区别:for(inti=0;i还有:std::transform(in[0].begin(),in[0].end(),out.begin(),[](constQString&a)->QVariant{if(a.isNull()||a.isEmpty())return"NONE";elsereturna;});因为我们有visualstudio2012,我什至

C++ 标准 : return by copy to initialize a reference without RVO: is there any copy?

让我们考虑下一个示例:structbig_type{};//Returnbycopyautofactory(){returnbig_type{};}voidany_scope_or_function(){big_type&&lifetime_extended=factory();}假设RVO被禁止或根本不以任何方式存在,big_type()是否会或可以被复制?还是将引用直接绑定(bind)到return语句中构造的临时对象?我想确保big_type析构函数仅在any_scope_or_function结束时被调用一次。我使用C++14,以防某些行为在标准版本之间发生变化。

c++ - 标准异常邀请不安全使用?

建议您总是抛出从std::exception派生的东西,并且有一些预定义的特化,例如std::runtime_errorstd::exception的接口(interface)是根据非抛出访问器给出的。伟大的。现在查看std::runtime_error的构造函数classruntime_error:publicexception{public:explicitruntime_error(conststring&);};所以如果我这样做try{foo();}catch(...){throwstd::runtime_error("bang");}foo完全有可能因为内存不足而抛出,在这种

c++ - 函数异常规范和标准异常 - foo() throw(Exception)

在C++中,您可以像这样声明具有异常规范的函数:intfoo()constthrow(Exception);我找到了这两个链接:http://www.cplusplus.com/doc/tutorial/exceptions/和http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8l.doc%2Flanguage%2Fref%2Fcplr156.htm但有几件事最终没有答案......问题1:为什么要添加异常规范?它会带来任何性能提升吗?编译器会有什么不同?因为

c++ - 替换标准 C++ 分配器?

我想用更健壮的分配器替换标准分配器(C++标准只需要对vector::resize进行溢出检查)。许多库提供的各种C++分配器在进行负面self测试时会一败涂地。我可以使用更强大的分配器。ESAPI的分配器不仅检查溢出,它还有调试仪器来帮助发现错误。http://code.google.com/p/owasp-esapi-cplusplus/source/browse/trunk/esapi/util/zAllocator.h.是否有一种标准方法可以轻松替换程序中使用的C++分配器?我还想确保它在库代码中被替换,我可能无法访问源代码。 最佳答案