草庐IT

variable_heap_stack

全部标签

c++ - 将 std::stack 复制到 std::vector

标准是否保证以下代码可以工作(假设st不为空)?#include#includeintmain(){externstd::stack>st;int*end=&st.top()+1;int*begin=end-st.size();std::vectorstack_contents(begin,end);} 最佳答案 是的。std::stack只是一个容器适配器。可以看到.top()其实是(§23.3.5.3.1)referencetop(){returnc.back();}其中c是容器,在本例中是std::vector也就是说你的代码

c++ - 如何删除 VS 警告 C4091 : 'typedef ' : ignored on left of 'SPREADSHEET' when no variable is declared

此警告在我的代码中由同一个声明多次触发,内容如下://SpreadsheetstructuretypedefstructSPREADSHEET{intID;//IDofthespreadsheetUINTnLines;//NumberoflinesvoidCopyFrom(constSPREADSHEET*src){ID=src->ID;nLines=src->nLines;}};我不想只是关闭该警告,而是更改代码,以免出现警告!注意:我不想在这里声明任何变量(它是一个头文件),只定义结构'SPREADSHEET'应该包含的内容... 最佳答案

c++ - "local variables at the outermost scope of the function may not use the same name as any parameter"是什么意思?

我一直在阅读C++入门第5版。在第6.1章功能参数列表的第三段中。它写道“此外,函数最外层范围内的局部变量不得使用与任何参数相同的名称”。什么意思?我不是以英语为母语的人。我不明白函数的“最外层范围”的实际含义。 最佳答案 函数的最外层是定义函数体的block。您可以将其他(内部)block放入其中,并在该block的本地变量中声明变量。内部block中的变量可以与外部block中的变量或函数参数具有相同的名称;他们将名称隐藏在外部范围内。外部block中的变量不能与函数参数同名。演示:voidf(inta)//functionha

c++ - 有 C++ MinMax Heap 实现吗?

我正在寻找类似STL中的算法(push_heap、pop_heap、make_heap),除了能够同时弹出有效的最小值和最大值。AKA双端优先级队列。如所述here.双端优先级队列的任何干净实现也可以作为替代方案,但是这个问题主要是关于MinMaxHeap实现。我的google-fu没有结果,但肯定存在吧? 最佳答案 您是否有不能使用std::set的原因?听起来像这样,加上一些包装器来访问和删除set::begin()和--set::end()将解决问题。我想很难找到通常比set的默认实现更快地执行MinMaxHeap的东西。

C++11 可变参数模板 : return tuple from variable list of vectors

我想写一些类似于pythonzip(http://docs.python.org/2/library/functions.html)的东西。zip应该接受可变数量的不同类型的vector,并返回一个vector元组,截断到最短输入的长度。例如x=[1,2,3]v=['a','b']我希望输出是一个vector[,]如何在C++11中做到这一点? 最佳答案 急切地做到这一点并且只通过复制非常容易:#include#include#includetemplatestd::vector>zip(std::vectorconst&...vs

c++ - 如何方便地打印出 std::stack 或 std::queue 中的所有元素

如果我不想为此创建一个新容器? 最佳答案 我已经编写了一个片段来进行调试。例如:std::stacks;//workswithstd::queuealso!s.push(1);s.push(2);std::cout请原谅我的这种骇人听闻的代码!但这是我几个月前写的:#include#include#includetemplateStream&printOneValueContainer(Stream&outputstream,constContainer&container){typenameContainer::const_ite

c++ - scoped_lock 如何避免发出 "unused variable"警告?

boost::mutex::scoped_lock是一个方便的RAII包装器,用于锁定互斥锁。我对其他事情使用了类似的技术:一个RAII包装器,它要求数据接口(interface)从/重新连接到串行设备。不过,我想不通的是,为什么在下面的代码中只有我的对象mst(其实例化和销毁确实有副作用)会导致g++发出“未使用的变量”警告错误,而l设法保持沉默。你知道吗?你能告诉我吗?[generic@sentinel~]$cattest.cpp#include#include#includestructMyScopedThing;structMyWorkerObject{voida(){std:

c++ - 我是否需要同步 std::condition_variable/condition_variable_any::notify_one

是否需要同步std::condition_variable/condition_variable_any::notify_one?据我所知,如果丢失通知是可以接受的-可以调用未protectednotify_one(例如通过互斥锁)。例如,我看到了以下使用模式(抱歉,不记得在哪里):{{lock_guardl(m);//dowork}c.notify_one();}但是,我检查了libstdc++源代码,发现:condition_variable::notify_onevoidcondition_variable::notify_one()noexcept{int__e=__gthre

C++ 标准 : do namespace-scoped constexpr variables have internal linkage?

假设我们有一个标题foo.h包含以下内容:#ifndefFOO_H_#defineFOO_H_namespacefoo{constexprstd::string_viewkSomeString="blah";}#endif//FOO_H_foo::kSomeString是否保证在包含foo.h的任何翻译单元中具有内部链接?这在C++11和C++17之间是否有所不同?在标准草案中[basic.link]/3说Anamehavingnamespacescopehasinternallinkageifitisthenameof[...]anon-inlinevariableofnon-vol

c++ - 为什么 stack<const string> 不能在 g++ 中编译?

这个问题在这里已经有了答案:CanIuseconstinvectorstoallowaddingelements,butnotmodificationstothealreadyadded?(14个回答)DoesC++11allowvector?(5个回答)关闭6年前。我在为大型(相对于我们团队的规模)项目维护端口时遇到了这个问题,但创建一个小示例很简单。stackoverflow.cpp:#include#include#includeusingnamespacestd;intmain(intargc,char*argv[]){stackstrstack;stringstr("Hell