草庐IT

std-ranges

全部标签

c++ - 如何使用 -std=c++17(可选、任意、string_view、变体)在 g++ 6.2.0 中包含 C++ 17 header

std::optional在C++17中,它是std::experimental::optional之前。我尝试编译一个包含的文件,使用命令:g++-std=c++17.cpp(在Bash终端中)。我收到以下错误:.cpp:5:20fatalerror:optional:Nosuchfileordirectory#include^compilationterminated但我可以#include就好了。我是否缺少一些头文件?如何包含optional标题?我也不能包含,或,得到同样的错误。 最佳答案 你不能。GCC6.2'ssuppo

c++ - 为什么遍历比合并两个排序的 std::list 更耗时?

令我惊讶的是,遍历比合并两个已排序的std::list花费的时间多12%。由于合并可以被认为和实现为连续的元素比较、列表拼接和迭代器遍历两个分离的排序链表。因此,遍历不应比合并它们慢,尤其是当两个列表足够大时,因为迭代元素的比例正在增加。但是,结果好像和我想的不符,我是这样验证上面的想法的:std::listlist1,list2;for(intcnt=0;cntdiff=std::chrono::system_clock::now()-start;std::cout附言。icc足够聪明,可以消除选项2。尝试sum+=num;并打印出sum。这是perf的输出:(测量的时间在不使用pe

c++ - 传递给 std::for_each 的函数是否允许复制序列元素?

我最近偶然发现了cppreference中的这个措辞:Unliketherestofthealgorithms,for_eachisnotallowedtomakecopiesoftheelementsinthesequenceeveniftheyaretriviallycopyable.这个说法对吗?我没有在标准中找到任何依据。我是否理解得很好,它会顺便说一句。暗示来自同一页面的以下示例无效?structSum{Sum():sum{0}{}voidoperator()(intn){sum+=n;}intsum;};intmain(){std::vectornums{3,4,2,8,1

c++ - 使用 c++ 11 constexpr 进行 std::map 初始化

我想用constexpr键初始化一个std::map。考虑以下C++11MWE:#includeusingstd::map;constexprunsignedintstr2int(constchar*str,constinth=0){return!str[h]?5381:(str2int(str,h+1)*33)^str[h];}constmapvalues={{str2int("foo"),"bar"},{str2int("hello"),"world"}};intmain(){return0;}当代码编译最近的clang和gcc时,生成的二进制文件将包含key类型的字符串:为什么k

c++ - 没有 ExecutionPolicy 的 std::transform 或 std::generate 可以并行吗?

在C++17中引入了并行标准算法(使用ExecutionPolicy参数重载),其中定义了执行顺序、交错和并行化的严格规则,例如([algorithm.parallel.exec/3]):Theinvocationsofelementaccessfunctionsinparallelalgorithmsinvokedwithanexecutionpolicyobjectoftypeexecution::sequenced_policyalloccurinthecallingthreadofexecution.[Note:Theinvocationsarenotinterleaved;s

c++ - 优化 std::visit 可能吗?

在使用std::visit时/std::variant我在探查器输出中看到std::__detail::__variant::__gen_vtable_impl函数花费的时间最多。我做了这样的测试://3classfamilies,alllikethisclassElementDerivedN:publicElementBase{...std::variantGetVariant()override{returnthis;}}std::vectorelements;std::vectorvisitors;std::vectorthirds;//prepareahacktogetdyna

c++ - std::integral_constant<T, v>::value 总是有定义吗?

在C++14标准中,std::integral_constant模板定义如下:templatestructintegral_constant{staticconstexprTvalue=v;typedefTvalue_type;typedefintegral_constanttype;constexproperatorvalue_type()constnoexcept{returnvalue;}constexprvalue_typeoperator()()constnoexcept{returnvalue;}};它没有说明静态数据成员是否有相应的外联定义,即,templateconst

c++ - 为什么 std::ssize 被强制为其带符号大小类型的最小大小?

在C++20中,std::ssize正在引入以获得通用代码容器的签名大小。(并解释了添加的原因here。)有点奇怪的是,那里给出的定义(结合common_type和ptrdiff_t)具有强制返回值是“ptrdiff_t的效果”>或容器的size()返回值的签名形式,以较大者为准。P1227R1间接为此提供了理由(“将60,000的大小变成-5,536的大小对于std::ssize()来说将是一场灾难”)。然而,在我看来,这是一种尝试“修复”该问题的奇怪方法。有意定义uint16_t大小且已知永远不会超过32,767个元素的容器仍将被迫使用比所需更大的类型。对于分别使用uint8_t大

c++ - 在抛出 'std::length_error' 实例后调用终止

这是我在这里的第一篇文章。由于我是新手,这个问题可能很愚蠢。当显示以下错误消息时,我正在编写一段代码,在抛出“std::length_error”的实例后调用终止什么():basic_string::_S_create/home/gcj/finals/home/gcj/quals中止的地方以下是有问题的代码,尤其是第39行到第52行。这对我来说很奇怪,因为这段代码几乎与第64行到第79行相同。intmain(){std::vectordirs,need;std::stringtmp_str;std::ifstreamfp_in("small.in");std::ofstreamfp_o

c++ - 了解 std::string 的效率

我正在努力学习更多关于C++字符串的知识。考虑constchar*cstring="hello";std::stringstring(cstring);和std::stringstring("hello");假设在应用程序的.data部分存储“hello”,然后将字节复制到堆上的另一个区域,由std::string管理的指针可以访问它们,我是否正确?我怎样才能有效地存储一个非常非常长的字符串?我正在考虑一个从套接字流中读取数据的应用程序。我害怕连接很多次。我可以想象使用一个链表并遍历这个列表。字符串让我害怕太久了!任何链接、提示、解释和更多详细信息都将非常有帮助。