草庐IT

c++ - std::deque 什么时候需要重新分配?

据我所知,std::deque以block的形式存储它的元素(虽然它依赖于实现,但这是我在大多数来源中读到的)而不是std::vector在大多数情况下使用单个内存块。因此,std::vector在插入过程中遇到重新分配是很合理的。但是,我无法将需要为std::deque重新分配的任何情况联系起来,因为当电流耗尽时,它只是从新的内存块开始。任何人都可以向我提供一个案例,其中std::deque由于对其执行了某些操作而需要重新分配吗? 最佳答案 Cananyoneprovidemewithcasewherestd::dequeneed

c++ - 为什么 std::max 由 const 返回?

我想找到最大值Foo并对其调用inc(),这是一个非常量方法。当然,在寻找最大值时,我不想创建任何拷贝或移动,即我不想要Foofoo=std::max(foo1,foo2)。我尝试编写自己的max,但g++坚持要我返回一个const&。#includeclassFoo{public:Foo(intx):x_(x){std::cout(constFoo&foo)const{returnx_>foo.x_;}voidinc(){++x_;}intx_;};/**Doesn'tcompile.MustreturnconstT&ormustacceptnon-constT&*templatei

c++ - 使用 std::get 作为 std::transform 的参数

我可能在这里遗漏了一些明显的东西——为什么我不能以这种方式使用std::get?#include#include#include#include#includeintmain(){std::mapsome_map;std::setset_of_ints;std::transform(some_map.begin(),some_map.end(),std::inserter(set_of_ints,set_of_ints.begin()),std::get);return0;}我试过的编译器是VS2010以及Ideone.com用于C++14的任何编译器(一些最近的GCC?)。这是后者的

c++ - 关联 std::tuple 容器

是否有可能定义(以一种简单的方式,可能重新使用std容器)“associativestd::tuple”,或者换句话说“variadiacstd::map”。类似这样的东西(这个接口(interface)只是为了解释,欢迎其他可能的接口(interface)):AssociativeTupleat;//std:stringisthekeytypeat.insert("my_float",3.14);//1.at.insert("my_int",42);at.insert("my_bool",true);at.insert("xyz",0);at.insert("my_string","

c++ - std::string::reserve() 和 std::string::clear() 难题

这道题从一段代码开始,只是因为我觉得这样更容易看出我在追求什么:/*static*/voidUrl::Split(std::list&url,conststd::string&stringUrl){std::stringcollector;collector.reserve(stringUrl.length());for(autoc:stringUrl){if(PathSeparator==c){url.push_back(collector);collector.clear();//Sabotagesmyoptimizationwithreserve()above!}else{col

c++ - {} 是传递给需要迭代器(代表某个容器的 std::end() )的函数的有效参数吗?

在boostdirectory_iteratorexample-howtolistdirectoryfilesnotrecursive(参见thisanswer)中是示例代码#include#include#include...usingnamespaceboost::filesystem;for(auto&entry:boost::make_iterator_range(directory_iterator(p),{})){std::cout(p是boost::filesystem::path类型。)在查看documentationformake_iterator_range时,我认

c++ - 为异质宽度的整数编写 std::min/std::max

我的C++程序使用不同宽度的无符号整数来表示对可以表示的数据的约束。例如,我有一个大小为uint64_t的文件,我希望使用大小为size_t的缓冲区以block的形式读取它。block是缓冲区大小和(剩余)文件大小中较小的一个:uint64_tfile_size=...;size_tbuffer_size=...;size_tchunk_size=std::min(buffer_size,file_size);但这失败了,因为std::min要求两个参数具有相同的类型,所以我必须向上转换然后再向下转换:size_tchunk_size=\static_cast(std::min(sta

当我用单引号括起一个字符串时,C++ std::cout 打印奇怪的字符

这个问题在这里已经有了答案:WhatdosinglequotesdoinC++whenusedonmultiplecharacters?(5个答案)关闭7年前。当我尝试std::cout我得到了11296,我知道我应该用","把它括起来,但为什么我会得到号码?

c++ - 使用带有参数包扩展和附加值的静态存储持续时间初始化 std::array

在询问时anotherquestion最近,在用参数包扩展后跟另一个元素初始化std::array时,我偶然发现了GCC的一些奇怪行为。我已经与Jarod42简要讨论过这个问题inthecommentsthere但我认为最好将其作为一个新问题提出。例如,考虑下面的代码,它应该提供一个实用程序make_array函数,该函数接受任意数量的参数并将它们std::forward发送到std::array初始化。前导标记参数选择数组是否应以默认构造的T(通过std::true_type选择)或不(通过std::选择)终止false_type).然后我创建一个整数数组,一次使用静态,一次使用自动

c++ - 排序 vector 上 std::lower_bound 的时间复杂度

我正在研究http://www.cplusplus.com/reference/algorithm/upper_bound/的std::upper_bound我发现这可能会在非随机访问迭代器上以线性时间运行。我需要将其用于排序vector。现在我不知道什么是非随机访问迭代器以及它是否会在排序后的vector上以对数时间运行。谁能帮我解决这个问题。 最佳答案 §23.3.6.1[vector.overview]/p1:Avectorisasequencecontainerthatsupportsrandomaccessiterator