草庐IT

back_emplace_iterator

全部标签

c++ - 在 C++17 中,为什么关联容器有一个 `erase` 成员函数(非 -`const` ) `iterator` ?

见,例如,http://en.cppreference.com/w/cpp/container/map/erase在C++03中有三个重载:voiderase(iteratorpos);voiderase(iteratorfirst,iteratorlast);size_typeerase(constkey_type&key);在C++11中,第一个和第二个重载被更改为采用const_iterator以便可以使用iterator或const_iterator调用它们>。第一个重载也得到了改进,它让迭代器在删除后将迭代器返回到元素:iteratorerase(const_iterator

C++ istream_iterator 不是 std 的成员

谁能告诉我为什么我在编译时写的下面这段代码一直在提示istream_iteratorisnotamemberofstd请你告诉我吗?谢谢大家#include#include#include#include#include#include#include//#includestructfield_reader:std::ctype{field_reader():std::ctype(get_table()){}staticstd::ctype_base::maskconst*get_table(){staticstd::vectorrc(table_size,std::ctype_bas

c++ - 从迭代器获取 const_iterator

这个问题在这里已经有了答案:关闭11年前.PossibleDuplicate:Obtainingconst_iteratorfromiterator我想写一个返回相应const_iterator的元函数来自iteratortemplatestructget_const_iterator{typedef???type;};get_const_iterator::type必须是constint*get_const_iterator::type必须是constint*get_const_iterator::type必须是constint*或constint*const,我不在乎get_con

c++ - 为什么优先级队列需要底层容器的front()、pop_back()而不是back()、pop_back()?

来自C++Primer以及https://en.cppreference.com/w/cpp/container/priority_queue,我知道:Apriority_queuerequiresrandomaccessinadditiontothefront,push_back,andpop_backoperations;我也读过blogpost来自Google并知道:push:addanewelementtothequeue,pop:removethelargestelementofthequeue,top:accessthelargestelementofthequeue.pu

c++ - 为什么 std::count(_if) 返回 iterator::difference_type 而不是 size_t?

这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:WhydoestheC++standardalgorithm“count”returnaptrdiff_tinsteadofsize_t?标准C++中有std::count/std::count_if算法。templatetypenameiterator_traits::difference_typecount(InputIteratorfirst,InputIteratorlast,constT&value);templatetypenameiterator_traits::difference_typec

c++ - 为什么 vector::push_back 和 emplace_back 调用 value_type::constructor 两次?

我有这门课:classFoo{public:Foo(){}Foo(constFoo&){cout然后我插入一个vector:Foofoo{};vf.push_back(foo);输出令人惊讶:constructedbylvaluereference.constructedbylvaluereference.我假设它在传递参数时被复制了,所以我尝试了:vf.push_back(move(foo));和vf.push_back(forward(foo));由于移动语义,输出略有不同,但仍然调用了两次构造函数:constructedbyrvaluereference.constructedb

c++ - 检查迭代器的类型是否为 reverse_iterator

有没有办法检查作为arg传递给fnc的迭代器是否是reverse_iterator?我可以使用任何迭代器特征函数吗? 最佳答案 用偏特化来写很简单:#include#includetemplatestructis_reverse_iterator:std::false_type{};templatestructis_reverse_iterator>:std::true_type{};尽管如下所述,这并不能处理“反向-反向”迭代器的(恕我直言不太可能)情况。Bathsheba的答案中稍微不那么琐碎的版本正确处理了这种情况。

c++ - boost::filesystem::recursive_directory_iterator 带过滤器

我需要递归地从目录及其子目录中获取所有文件,但不包括几个目录。我知道他们的名字。是否可以使用boost::filesystem::recursive_directory_iterator? 最佳答案 是的,在遍历目录时,您可以测试排除列表中的名称并使用递归迭代器的no_push()成员来防止它进入这样的目录,例如:voidselective_search(constpath&search_here,conststd::string&exclude_this_directory){usingnamespaceboost::filesy

C++:指针 vector 在 push_back() 之后失去引用

在我的代码中,a有一个Node对象的全局vector和一个Node指针的局部vector:#include#include#includeusingnamespacestd;classNode{intn;public:Node(inti):n(i);intgetN(){returnn;}};vectorv;intmain(){vectorp;v.push_back(Node(1));p.push_back(&v[0]);printf("firstnodeid:%d\n",(*p[0]).getN());return0;}我将一个节点对象插入到全局vector中,并将该对象的指针插入到本

c++ - 在 vector::push_back 内存明智的情况下会发生什么?

我的问题是关于vector::push_back的效果,我知道它在vector末尾添加了一个元素,但是在引擎盖下会发生什么?IIRC内存对象是按顺序分配的,所以我的问题是vector::push_back是否只是在vector之后立即分配更多内存,如果是这样,如果没有足够的可用内存会发生什么情况在那个位置?或者也许在“结束”中添加了一个指针以使vector“跳跃”到它继续的位置?或者它只是通过将其复制到另一个有足够空间的位置而重新分配并且旧拷贝被丢弃?还是别的什么? 最佳答案 如果已经分配了足够的空间,则对象是从就地参数构造的拷贝。