草庐IT

std-ranges

全部标签

c++ - 使用 boost 检查 std::string 是否是有效的 uuid

我想使用boost检查给定的字符串是否是有效的UUID。这是我通过查看boost网站上的文档得出的结论:voidvalidate_uuid(conststd::string&value){try{boost::uuids::string_generatorstringGenerator;(void)stringGenerator(value);}catch(conststd::exception&ex){//...}}但是,这并不总是有效。如果我使用对于有效UUID来说太短的字符串调用该函数,则会按预期抛出异常。但是,如果我使用无效的UUID(例如00000000-0000-0000-

c++11 可变参数模板和 std::endl

我尝试使用C++11可变参数模板做记录器,但它不适用于std::endl,因为std::endl是模板函数,编译器不知道std::endl的特化选择。有什么办法可以强制始终选择std::endl>?如果可能的话,我想直接使用std::endl。编辑:看起来目前在C++11中是不可能的,最好的方法是使用#define或者vsoftco回答了什么。#include#includeclassLogger{public:templatevoidlog(Tval);templatevoidlog(Tval,Args...args);};//explicitspecializationnotwor

c++ - 我应该使用 operator+= 而不是 operator+ 来连接 std::string 吗?

我经常看到这个结构是有原因的吗:std::stringmyString=someString+"text"+otherString+"moretext";...而不是这个(我很少看到):std::stringmyString;myString+=someString+="text"+=otherString+="moretext";阅读std::stringAPI,在我看来operator+创建了很多临时对象(可能被编译器RVO优化掉了?),而operator+=变体仅append文本。在某些情况下,operator+变体将是可行的方法。但是,当您只需要将文本append到现有的非常量

c++ - std::find 和 std::map.find 都是 O(logN) 吗?

std::find和std::map.find都是O(logN)吗?如果是,std::find如何在对数时间内实现对std::map的搜索?std::find的实现是否专门用于std::map用例? 最佳答案 不,std::find是O(N),与容器无关。它不知道“容器”,没有针对std::map的专门化。std::find仅使用迭代器,它没有关于底层容器的信息。根据cppreference.com,实现等同于:templateInputItfind(InputItfirst,InputItlast,constT&value){fo

c++ - 不完整类型 struct std::hash 与 unordered_map 的无效使用,其中 std::pair of enum class 作为键

我想使用unordered_map,std::uint8_t>用于管理一些像素图格式。这里是最少的代码:#include#include#include#include#includeenumclassPNM:std::uint8_t{PBM,PGM,PPM};enumclassFormat:bool{BIN,ASCII};structpair_hash{public:templatestd::size_toperator()(conststd::pair&x)const{returnstd::hash()(x.first)^std::hash()(x.second);}};intma

c++ - std::pow 在 32 位和 64 位应用程序中产生不同的结果

我发现一些复杂计算的结果不匹配。当我彻底观察中间结果时,是std::pow函数造成了不匹配。以下是输入/输出。longdoubledvalue=2.7182818284589998;longdoubledexp=-0.21074699576017999;longdoubleresult=std::powl(dvalue,dexp);64bit->result=0.80997896907296496and32bit->result=0.80997896907296507我正在使用VS2008。我已经尝试使用pow函数的其他变体,它接受longdouble并返回longdouble,但仍然

c++ - std::weak_ptr:锁或 shared_ptr 构造函数?

似乎有两种方法可以暂时获取weak_ptr指向的资源的所有权:使用lock()将weak_ptr传递给shared_ptr构造函数这两者都会产生一个shared_ptr,如果weak_ptr为空并且锁返回一个nullptrshared_ptr构造函数抛出异常。所以问题是:什么时候应该使用一个或另一个?是否有与此相关的一般准则或最佳做法? 最佳答案 复制自http://en.cppreference.com/w/cpp/memory/weak_ptr/lockBoththisfunctionandtheconstructorofstd

c++ - 在编译时填充 std::array 和 const_cast 可能的未定义行为

已知std::array::operator[]由于C++14是constexpr,请参见下面的声明:constexprconst_referenceoperator[](size_typepos)const;但是,它也是const限定的。如果您想使用std::array的下标运算符以便在编译时为数组赋值,这会产生影响。例如考虑以下用户文字:templatestructFooLiteral{std::arrayarr;constexprFooLiteral():arr{}{for(inti(0);i如果您尝试声明类型为FooLiteral的constexpr变量,上述代码将无法编译。这

c++ - 理解 std::future::then 的延续

谁能用C++中的示例解释async([](){x();y();})和async([]()之间的区别{x();}).then([](){y();})?我的理解是,在后一种情况下,x、y中的每一个都可能会立即在不同的线程中启动,并且只会在get时阻塞(在它们各自的线程中)()在未来作为输入传递时被调用。 最佳答案 ...whatisthedifferencebetweenasync([](){x();y();})andasync([](){x();}).then([](){y();})?真的不多-那么为什么要有呢?一言以蔽之可组合性。它

c++ - 具有与 std::function 不同签名的 Lambda 函数

我不明白为什么第三种情况没问题(即使lambda的参数类型不同于std::function类型)而编译器却提示第四种情况:functionidInt=[](inti){returni;};//OKfunctionidInt=[](int&i){returni;};//OKfunctionidInt=[](inti){returni;};//OKfunctionidInt=[](int&i){returni;};//ERROR! 最佳答案 当你写的时候:functionidInt=[](int&i){returni;};//ERROR