草庐IT

c++ - 使用 for_each 和 bind 反转 vector 中的字符串

我在想如何在一个“简单的"行。是的,我知道使用自定义仿函数很容易,但我不能接受,它不能使用bind完成(至少我做不到)。#include#include#includestd::vectorv;v.push_back("abc");v.push_back("12345");std::for_each(v.begin(),v.end(),/*callstd::reverseforeachelement*/);编辑:非常感谢那些有趣的解决方案。但是,我的解决方案是不使用tr1::bind随VisualStudio2008功能包/SP1一起提供。我不知道为什么它不能像预期的那样工作,但事实就

c++ - 在 std::for_each 中返回 std::move(f)

我正在编写标准C++库的实现以供研究。C++11标准规定for_each返回std::move(f)。templateFunctionfor_each(InputIteratorfirst,InputIteratorlast,Functionf);Returns:std::move(f).我认为函数作用域局部变量在返回时是move构造的。我应该显式返回move(f)吗? 最佳答案 来自Josuttis的C++标准库你不必也不应该move()返回值。根据语言规则,标准规定对于以下代码Xfoo(){Xx;...returnx;}保证以下

c++ - std::for_each 忽略默认函数参数

我偶然发现了一个奇怪的编译问题。我想使用std::for_each处理字符串列表。下面的简化代码说明了这个问题:#include#include#includeusingnamespacestd;voidf(wstring&str){//processstrhere}voidg(wstring&str,intdummy=0){//processstrhere,sameasf,justaddedaseconddefaultdummyargument}intmain(int,char*[]){listtext;text.push_back(L"foo");text.push_back(L"

c++ - std::list 和 std::for_each:我的终点在哪里?

考虑以下最小示例:#include#include#includeintmain(){std::list>list;list.push_back([&list](){list.push_back([](){throw;});});std::for_each(list.cbegin(),list.cend(),[](auto&&f){f();});}它编译并在运行时抛出异常。我的猜测是只有第一个lambda由std::for_each执行,但显然我错了:如果我在列表末尾附加另一个lambda,迭代也会到达那个lambda。让我们还原示例(push_front而不是push_back和cr

c++ - 如何将 dynamic_cast 与 for_each 一起使用

我有以下代码:vector::iteratoritr=vec.begin();for(;itr!=vec.end();++itr){C2*c=dynamic_cast(*itr);c->f();}我想知道是否可以使用一行for_each来替换它。我尝试了以下方法:for_each(vec.begin(),vec.end(),bind2nd(mem_fun(&C2::f),dynamic_cast));但是我得到一个编译错误,expectedunqualified-idbefore'dynamic_cast'那正确的应该是什么?[编辑]我不能使用c++11。看来我必须定义一个额外的仿函数

c++ - 修改 std::for_each 中的容器

标准是否明确禁止修改std::for_each中的容器?更具体地说,在std::list的情况下,当列表被修改时,迭代器不会失效。因此,以下代码有效:std::listlist;list.push_front(5);list.push_front(10);autoit=list.end();it--;//pointto5std::for_each(list.begin(),list.end(),[&](inti){/*thelinebelowwillremovethelastelementinthelist;*listwillhaveonlyoneelement(thecurrentl

c++ - 为什么不能将函数内部定义的结构用作 std::for_each 的仿函数?

以下代码无法编译。编译器提示*没有匹配函数来调用for_each*。为什么会这样?#include#includestructElement{voidflip(){}};voidflip_all(std::mapinput){structFlipFunctor{voidoperator()(std::pair&item){item.second->flip();}};std::for_each(input.begin(),input.end(),FlipFunctor());}当我将structFlipFunctor移动到函数flip_all之前时,代码会编译。完整错误信息:nomat

c++ - 可以用 std::for_each 改变对象吗?

for_each接受输入迭代器://fromc++standardtemplateFunctionfor_each(InputIteratorfirst,InputIteratorlast,Functionf);是否可以像这样更改函数f中的对象:structAddOne{voidoperator()(int&x){x=x+1;}};std::vectorvec(10);std::for_each(vec.begin(),vec.end(),AddOne());此代码适用于VC++2008,也适用于GCC,但它也是可移植(合法)代码吗?(InputIterators只保证可用作右值,在这

c++ - 我将如何使用 for_each 删除 STL 映射中的每个值?

假设我有一个STL映射,其中的值是指针,我想将它们全部删除。我将如何表示以下代码,但使用std::for_each?我很高兴使用Boost的解决方案。for(stdext::hash_map::iteratorir=myMap.begin();ir!=myMap.end();++ir){deleteir->second;//deleteallthe(Foo*)values.}(我找到了Boost的checked_delete,但我不确定如何将它应用到迭代器代表的pair)。(此外,出于这个问题的目的,请忽略将需要删除的原始指针存储在STL容器中不是很明智的事实)。注意:我随后找到并在下

c++ - std::for_each 优于 std::set,C++11

遍历一个vector有效:std::vectorcollection={2,3,4,5435345,2};std::for_each(collection.begin(),collection.end(),[](int&i){cout但没有超过一个集合(编译错误):std::setcollection={2,3,4,5435345,2};std::for_each(collection.begin(),collection.end(),[](int&i){cout为什么我不能用std::for_each遍历std::set?奖金问题:另外,我想把lambda参数中的int&改成auto