草庐IT

c++ - 在 C++14 中编译

所以在我的CSE类(class)中,我们得到了一个头文件,可以立即用于我们正在编写的程序。不幸的是,我无法让终端使用该header进行编译,它给出了很多错误(仅使用“g++”进行编译)。另外,当我在我的大学里使用PuTTY时,我在使用这个header时遇到了同样的错误。但是,当我使用“g++-std=c++14”编译时,我没有收到错误。我已经尝试在我的mac终端上使用此命令进行编译,但它说它无法识别c++14部分。dhcp-10-202-147-243:hw1pr1Admin$g++-std=c++14hw1pr1.cpperror:invalidvalue'c++14'in'-std

c++ - 可以用 0 计数调用 std::search_n 吗?

std::search_n可以被“安全地”调用且count为0吗?具体来说,像下面这样的代码是否有效?#include#includeintmain(intargc,char*argv[]){constinttest[7]={1,2,3,4,5,6,7};constint*constlocation=std::search_n(test,test+7,0,8);if(location==test){std::puts("Founditatthebeginning!");}}我希望此代码到达std::puts语句,并且大多数std::search_n的描述似乎暗示它会。但是,我发现的大多

c++ - 如何使用 ifstream 从文件中正确读取无符号整型变量?

我的代码从文本文件Input_File_Name中读取无符号整型变量。unsignedintColumn_Count;//ColsunsignedintRow_Count;//Rowstry{ifstreaminput_stream;input_stream.open(Input_File_Name,ios_base::in);if(input_stream){//iffileisopenedinput_stream.exceptions(ios::badbit|ios::failbit);input_stream>>Row_Count;input_stream>>Column_Cou

c++ - std::array initializer list 在初始化列表中初始化

虽然我非常喜欢C++11中的新特性,但有时我觉得我遗漏了它的一些微妙之处。初始化int数组工作正常,初始化Element2vector工作正常,但初始化Element2数组失败。我认为正确的语法应该是未注释的行,但对我来说没有任何初始化尝试成功。#include#includeclassElement2{public:Element2(unsignedintInput){}Element2(Element2const&Other){}};classTest{public:Test(void):Array{{4,5,6}},Array2{4,5},//Array3{4,5,6}Array

c++ - 声明硬编码 std::string 会导致缓冲区溢出

我的程序中有以下行会导致运行时警告:if(!is_directory("C:\\NGFMS_Debug\\Files")&&!create_directories("C:\\NGFMS_Debug\\Files"))警告的文本​​是这样的:“XXX.exe中发生了缓冲区溢出,它破坏了程序的内部状态。”警告出现在对“is_directory(...)”的调用中。我猜字符串的空间没有分配,但我认为这样的语法是合法的。is_directory函数是boost/filesystem.hpp的一部分,我正在使用以下命名空间:usingnamespaceboost;usingnamespacebo

c++ - boost::program_options "polymorphic"参数

我想使用boost::program_options创建一个可以按如下方式调用的可执行文件:./example--nmax=0,10#nmaxischosenrandomlybetween0and10./example--nmax=9#nmaxissetto9./example#nmaxissettothedefaultvalueof10用最少的代码以类型安全的方式实现这一目标的最佳方法是什么? 最佳答案 Iwouldliketouseboost::program_optionstocreateanexecutablewhichca

c++ - std::locale::global 是否影响 printf() 函数?

我在我的电脑上使用俄语区域设置。如果我设置:classnumpunct_withpoint:publicnumpunct{protected:///Overridethefunctionthatgivesthedecimalseparator.chardo_decimal_point()const{return'.';}};...localeloc(std::locale::classic(),newnumpunct_withpoint);std::locale::global(loc);然后printf("%f",3.14);输出是:3,14小数点分隔符是“,”,不像do_decim

c++ - `std::memory_order_acquire` 的语义是否需要 x86/x86_64 上的处理器指令?

众所周知,在x86上,操作load()和store()内存屏障memory_order_consume,memory_order_acquire,memory_order_release,memory_order_acq_rel不需要缓存和流水线的处理器指令,汇编代码始终对应于std::memory_order_relaxed,这些限制仅对编译器的优化是必要的:http://www.stdthread.co.uk/forum/index.php?topic=72.0这段反汇编代码为store()(MSVS2012x86_64)确认了这一点:std::atomica;a.store(0,

c++ - std::move_if_noexcept 的基本原理仍在 move 抛出仅 move 类型?

move_if_noexcept将:返回一个右值——促进move——如果move构造函数是noexcept或者如果没有复制构造函数(仅move类型)返回一个左值——强制复制——否则我发现这相当令人惊讶,因为具有抛出move-ctor的仅move类型仍将由使用move_if_noexcept的代码调用此move-ctor。是否对此给出了详尽的理由?(也许直接或在N2983的两行之间?)代码不编译而不是仍然不得不面对不可恢复的move场景会不会更好?N2983中给出的vector示例很好:voidreserve(size_typen){......new((void*)(new_begin

c++ - std::stringstream 的默认 `fill character` 是什么?

它是实现定义的还是标准建议流的默认填充字符?示例代码:#include#include#includeintmain(){std::stringstreamstream;stream使用clang++--stdlib=libstdc++$clang++--stdlib=libstdc++test.cpp$./a.out|hexdump0000000202020202020202032350a0a000000c$使用clang++--stdlib=libc++$clang++--stdlib=libc++test.cpp$./a.out|hexdump0000000fffffffffff