草庐IT

c++ - 命名空间 'begin' 中没有名为 'std' 的成员

我在Windows上成功编译了一个应该是跨平台的代码。现在,当使用MacOSX在Xcode中编译它时,我得到:std::valarrayv(32);...std::sort(begin(v),end(v));#Useofundeclaredidentifier'begin'std::sort(std::begin(v),std::end(v));#Nomembernamed'begin'innamespace'std'std::sort(std::valarray::begin(v),std::valarray::end(v));#Idem,erroraswell为什么会发生错误Nom

c++ - 在调用之前检查 std::function 的有效性?

我正在尝试编写一个简单但灵活的事件系统(主要是作为练习,我知道现有的库具有非常好的事件处理程序),但我遇到了一个小绊脚石。如何检查作为委托(delegate)的std::function(可能通过lambda,可能通过std::bind)是否为有效函数/成员函数的对象是否仍然存在在打电话之前?我试过简单地使用std::function的bool运算符,但没有取得任何成功。理想情况下,我希望A.在委托(delegate)函数内部以外的地方进行检查,并且B.当被检查的std::function不是委托(delegate)时,代码仍然有效。有什么想法吗?编辑:这是我运行的测试的源代码#inc

c++ - 初始化 std::pair<double, std::array<std::pair<double, double>, 3>>

谁能建议以下代码中std::vector::push_back调用中std::make_pair调用的正确语法:#include#include#includeintmain(){typedefstd::pairPairType;std::vector>>myVector;doubleKey=0.0;PairTypePair1=std::make_pair(1.0,2.0);PairTypePair2=std::make_pair(3.0,4.0);PairTypePair3=std::make_pair(5.0,6.0);myVector.push_back(std::make_pa

c++ - std::call_once 是免费的吗?

我想知道std::call_once锁是否空闲。There是使用互斥锁的call_once实现。但是我们为什么要使用互斥体呢?我尝试使用atomic_bool和CAS操作编写简单的实现。代码线程安全吗?#include#include#include#includeusingnamespacestd;usingmy_once_flag=atomic;voidmy_call_once(my_once_flag&flag,std::functionfoo){boolexpected=false;boolres=flag.compare_exchange_strong(expected,tr

c++ - std::hex 和 std::setw 不适用于某些字符

我要做的是将字符串的字节转换为十六进制格式。基于thisanswer(和许多其他一致的)我试过代码:#include#include#includeintmain(){std::stringinputText=u8"A7°";std::stringstreamss;//printeverycharofthestringashexon2valuesfor(unsignedinti=0;i但对于一些以UTF8编码的字符,它不起作用。例如,在包含以UTF8编码的度数符号(°)的字符串中,结果是:ffffffc2ffffffb0而不是c2b0。现在我希望该算法能够处理单个字节,而不管它们的内容

c++ - 在 C++ 中返回多个矩阵( Armadillo 库)

我正在使用C++中的Armadillo库。首先我计算一个特殊矩阵(在我的代码中:P),然后我计算QR分解(在我的代码中:Q)。最后,我需要将P和Q以及另一个矩阵T返回到我的主函数。#include#includeusingnamespacestd;usingnamespacearma;doublephi(intn,intq){...matP(n,n);P=...matQ,R;qr(Q,R,P);returnP:returnQ;returnQ;...}intmain(){...intn,q;cout>n>>q;phi(n,q);...}我正在寻找一种无需使用指针和引用即可在Armadil

c++ - 在带有 std::unique_ptr 的 lambda 中使用 std::bind

//Byconstl-valuereferenceautofunc2=std::bind([](conststd::unique_ptr>&pw)//fine{std::coutsize()>(22,1));//Bynon-constl-valuereferenceautofunc3=std::bind([](std::unique_ptr>&pw)//fine{std::coutsize()>(22,1));//ByValueautofunc4=std::bind([](std::unique_ptr>pw)//error{std::coutsize()>(22,1));func4(

c++ - 如何为 std::unique_ptr 创建一个有效的 C++ 别名模板

我想为std::unique_ptr创建一个别名模板来提供我自己的删除函数。unique_ptr有一个标量和一个数组实现,它们是这样定义的:template>classunique_ptr//scalartemplateclassunique_ptr//array我在尝试覆盖unique_ptr的标量和数组版本时遇到了麻烦。只为一个版本创建别名很容易,如下所示:templatestructDeleter{voidoperator()(T*ptr){deleteptr;}};templateusingmy_unique_ptr=std::unique_ptr>;但是当我尝试添加第二个别名

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

有什么好的方法可以将shared_ptr转换为shared_ptr吗?我想到了以下但它看起来不是很干净。intmain(intargc,char**argv){std::shared_ptrp1=std::make_shared();std::shared_ptrp2=std::shared_ptr(reinterpret_cast(p1.get()),[p1](unsignedchar*){});} 最佳答案 你正在做的事情有一个现成的功能,reinterpret_pointer_cast:std::shared_ptrp2=st

c++ - C++向chrono::system_clock::time_point添加月份

如何将chrono::system_clock::time_point值加数月?谢谢! 最佳答案 概述这是一个非常有趣的问题,有几个答案。“正确”的答案是您必须针对特定应用程序决定的。使用月份,您可以选择按时间顺序进行计算或进行日历计算。按时间顺序的计算处理时间点和持续时间的常规单位,例如小时,分钟和秒。日历计算处理不规则的日历,该日历主要用来给日子起令人难忘的名字。年表计算如果问题是关于future几个月的物理过程,那么物理学并不关心不同的月份有不同的长度,因此按时间顺序计算就足够了:婴儿要在9个月内到期。从现在开始的6个月后,这