草庐IT

remove_copy_if

全部标签

c++ - 为什么标准没有提供 erase-remove-idiom 的便利助手?

从STL中的集合中删除项目需要一种经常使用的技术,该技术已成为一种习语:theerase-remove-idiom这个习语最常见的用法之一是删除T类型的项目来自vectorstd::vectorwidget_collection;Widgetwidget;widget_collection.erase(std::remove(widget_collection.begin(),widget_collection.end(),widget),widget_collection.end());这显然非常冗长,并且违反了DRYprinciple-有问题的vector在那里需要4次。所以我的问

c++ - 避免在静态 bool 值上使用 if 语句进行逻辑决策

我有一个类,其成员itemType仅设置一次且从未修改过,但在许多if语句中使用它来决定调用哪个函数。由于itemType仅设置一次,因此有办法避免类中其他地方的if语句。这将简化和清理代码,并且作为奖励还将节省if检查的开销。我正在考虑一个指针函数,我可以根据itemType值在构造函数中初始化它。有没有更好的替代方法?请注意原始类和代码库很大,我无法根据项目类型创建子类。enumItemTypes{ItemTypeA,ItemTypeB,};classItemProcessing{public://ThisfunctioniscalledhundredsoftimesvoidPro

C++ - 为什么程序在映射迭代器中使用 if 语句崩溃?

我是C++的新手,我试图在传递if语句的同时遍历映射。但是程序崩溃了。请帮我修复程序。#include#include#include#include#includeusingnamespacestd;intmain(){std::maph;std::map::iteratorit;h[1]=2;h[4]=5;for(it=h.begin();it!=h.end();it++){if(it->second>4){h.erase(it->first);}} 最佳答案 您正在删除for循环内的元素,指向已删除元素(即it)的迭代器将失效

c++ - constexpr 递归函数是否使用 if constexpr

使用gcc(HEAD7.0.0201612)我惊讶地发现这有效:constexprlongvalue(constchar*definition){if(definition&&*definition){return*definition+value(definition+1);}return*definition;}intmain(){longl{};std::cin>>l;switch(l){casevalue("AAAA"):f1();break;casevalue("BBBB"):f2();break;default:error();break;}return0;}文字字符串"A

c++ - enable_if 检查迭代器的值类型是否是一对

我想为值类型为一对的迭代器编写一个专门的模板函数。我的期望是这应该匹配std::map的迭代器。为了检测对:templatestructis_pair:std::false_type{};templatestructis_pair>:std::true_type{};//alsotriedthis,butitdidn'thelptemplatestructis_pair>:std::true_type{};然后我在函数声明中使用enable_if:templatedecltype(auto)do_stuff(std::enable_if::value,ITR>itr){//access

c++ - 使用 "if constexpr"防止元组越界

以下代码在GCC和Clang下编译良好,但在VisualStudio(/std:c++latest)的最新更新中停止工作:#includetemplatevoidcheck_tuple(T...types){ifconstexpr(pos>::type;}}intmain(){check_tuple(1.0,1.0);check_tuple(1.0,1.0);}在最新版本的VisualStudio(/std:c++latest)中,编译失败,元组索引越界(std::tuple_element>)。是否可以像这样使用constexpr来防止元组越界? 最佳答案

c++ - 为什么在 std::copy 期间使用 std::back_inserter 而不是 end()?

我见过std::copy()使用std::back_inserter但我使用了std::end()并且两者都有效.我的问题是,如果std::end()工作正常,为什么还需要std::back_inserter?#include#include#include#includeusingnamespacestd;intmain(){//Declaringfirstcontainervectorv1={1,2,3};//Declaringsecondcontainerfor//copyingvaluesvectorv2={4,5,6};//Usingstd::back_inserterins

c++ - 使用 enable_if 的模板特化在 Clang 中失败,适用于 GCC

我正在尝试删除基于模板类型的成员函数。问题是在未删除的情况下,使以后的模板特化与我的函数的类型签名匹配。我尝试了以下代码,它使用GCC(9.0.1)编译,但在Clang(9.0.0)中出错。我认为它也无法在MSVC++中构建代码。#include#includetemplatestructmy_type{templatestd::enable_if_t::value,my_type>my_fun(constmy_type&v){std::couttemplatestd::enable_if_t::value,my_type>my_type::my_fun(constmy_type&v)

c++ - `enable_if` 与 `enum` 模板特化问题

我在GCC编译时遇到问题enable_ifs应用于模板类方法的返回值。使用Clang,我可以在enable_if中使用表达式在enum上模板参数,而GCC拒绝编译此代码。这里是问题描述、初始代码及其后续修改,试图让我和编译器满意(不幸的是,不是同时)。我有一个非模板类Logic包含模板化类方法computeThings()它有一个enumStrategy作为其模板参数的之一。computeThings()中的逻辑取决于编译时间Strategy,所以ifconstexpr是一种合理的实现方式。变体1#includeclassLogic{public:enumStrategy{strat_

c++ - 我如何调整 erase-remove 习语来处理 vector 元组?

我有一个包含成员j、k和l的元组vector。我正在尝试调整erase-remove习语,以便在.k成员的值满足特定条件时我可以删除整个元组。我尝试使用标准的.erase(removeif())方法,其中谓词引用元组成员位置,但被告知相关vector类没有成员“k”。vec_list_iter_exp_out.erase(std::remove_if(vec_list_iter_exp_out.begin(),vec_list_iter_exp_out.end(),vec_list_iter_exp_out.k我希望如果vec_list_iter_exp_out包含以下假设值:vec_