我正在寻找 map 的 C++ 类似物或 filter来自 Python 编程语言。它们中的第一个对 iterable 的每个项目应用一些函数并返回结果列表,第二个从函数返回 true 的 iterable 的那些元素构造一个列表。
我想在 C++ 中使用类似的功能:
Python 的 map 和 filter 在 C++ 中有没有很好的实现?
在这个简短的示例中,我尝试使用 boost::bind 等工具来解决这个问题和 std::for_each我面临着困难。 std::vector<std::string> result应包含所有字符串 std::vector<std::string> raw该字典顺序高于标准输入的最后一个字符串。但实际上 result返回点的容器仍然是空的。
#include <iostream>
#include <vector>
#include <algorithm>
#include <boost/bind.hpp>
void filter_strings(std::string& current, std::string& last, std::vector<std::string>& results)
{
if (current > last)
{
results.push_back(current);
std::cout << "Matched: " << current << std::endl;
}
}
int main()
{
std::vector<std::string> raw, result;
std::string input, last;
//Populate first container with a data
while(std::getline(std::cin, input))
raw.push_back(input);
last = raw.back();
//Put into result vector all strings which lexicographically higher than the last one
std::for_each(raw.begin(), raw.end(), boost::bind(&filter_strings, _1, last, result));
//For some reason the resulting container is empty
std::cout << "Results: " << result.size() << std::endl;
return 0;
}
输入和输出:
[vitaly@thermaltake 1]$ ./9_boost_bind
121
123
122
120 //Ctrl+D key press
Matched: 121
Matched: 123
Matched: 122
Results: 0
我们将不胜感激。
最佳答案
正如@juanchopanza 所建议的那样,<algorithm> 中的模板函数STL header 是您的最佳选择。
#include <iostream>
#include <vector>
std::vector<std::string> filter(std::vector<std::string> & raw) {
std::vector<std::string> result(raw.size());
std::string last = raw[raw.size() - 1];
auto it = std::copy_if(raw.begin(), raw.end(), result.begin(),
[&](std::string s) { return s.compare(last) > 0; });
result.resize(std::distance(result.begin(), it));
return result;
}
int main(int argc, const char *argv[])
{
std::vector<std::string> raw, result;
std::string input;
while (std::getline(std::cin, input)) {
raw.push_back(input);
}
result = filter(raw);
for (size_t i = 0; i < result.size(); i++) {
std::cout << "Matched: " << result[i] << std::endl;
}
std::cout << "Results: " << result.size() << std::endl;
return 0;
}
编译运行:
$ clang++ -std=c++11 -o cppfilter main.cpp && ./cppfilter
121
123
122
120 // Ctrl + D pressed
Matched: 121
Matched: 123
Matched: 122
Results: 3
关于python - 具有与 Python 的过滤器和映射相同功能的 C++ 工具,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21865981/
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
我想安装一个带有一些身份验证的私有(private)Rubygem服务器。我希望能够使用公共(public)Ubuntu服务器托管内部gem。我读到了http://docs.rubygems.org/read/chapter/18.但是那个没有身份验证-如我所见。然后我读到了https://github.com/cwninja/geminabox.但是当我使用基本身份验证(他们在他们的Wiki中有)时,它会提示从我的服务器获取源。所以。如何制作带有身份验证的私有(private)Rubygem服务器?这是不可能的吗?谢谢。编辑:Geminabox问题。我尝试“捆绑”以安装新的gem..
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
只是想确保我理解了事情。据我目前收集到的信息,Cucumber只是一个“包装器”,或者是一种通过将事物分类为功能和步骤来组织测试的好方法,其中实际的单元测试处于步骤阶段。它允许您根据事物的工作方式组织您的测试。对吗? 最佳答案 有点。它是一种组织测试的方式,但不仅如此。它的行为就像最初的Rails集成测试一样,但更易于使用。这里最大的好处是您的session在整个Scenario中保持透明。关于Cucumber的另一件事是您(应该)从使用您的代码的浏览器或客户端的角度进行测试。如果您愿意,您可以使用步骤来构建对象和设置状态,但通常您
我正在使用Rails3.1并在一个论坛上工作。我有一个名为Topic的模型,每个模型都有许多Post。当用户创建新主题时,他们也应该创建第一个Post。但是,我不确定如何以相同的形式执行此操作。这是我的代码:classTopic:destroyaccepts_nested_attributes_for:postsvalidates_presence_of:titleendclassPost...但这似乎不起作用。有什么想法吗?谢谢! 最佳答案 @Pablo的回答似乎有你需要的一切。但更具体地说...首先改变你View中的这一行对此#
这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:Pythonconditionalassignmentoperator对于这样一个简单的问题表示歉意,但是谷歌搜索||=并不是很有帮助;)Python中是否有与Ruby和Perl中的||=语句等效的语句?例如:foo="hey"foo||="what"#assignfooifit'sundefined#fooisstill"hey"bar||="yeah"#baris"yeah"另外,类似这样的东西的通用术语是什么?条件分配是我的第一个猜测,但Wikipediapage跟我想的不太一样。
什么是ruby的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht
是否有简单的方法来更改默认ISO格式(yyyy-mm-dd)的ActiveAdmin日期过滤器显示格式? 最佳答案 您可以像这样为日期选择器提供额外的选项,而不是覆盖js:=f.input:my_date,as::datepicker,datepicker_options:{dateFormat:"mm/dd/yy"} 关于ruby-on-rails-事件管理员日期过滤器日期格式自定义,我们在StackOverflow上找到一个类似的问题: https://s
无论您是想搭建桌面端、WEB端或者移动端APP应用,HOOPSPlatform组件都可以为您提供弹性的3D集成架构,同时,由工业领域3D技术专家组成的HOOPS技术团队也能为您提供技术支持服务。如果您的客户期望有一种在多个平台(桌面/WEB/APP,而且某些客户端是“瘦”客户端)快速、方便地将数据接入到3D应用系统的解决方案,并且当访问数据时,在各个平台上的性能和用户体验保持一致,HOOPSPlatform将帮助您完成。利用HOOPSPlatform,您可以开发在任何环境下的3D基础应用架构。HOOPSPlatform可以帮您打造3D创新型产品,HOOPSSDK包含的技术有:快速且准确的CAD
华为OD机试题本篇题目:明明的随机数题目输入描述输出描述:示例1输入输出说明代码编写思路最近更新的博客华为od2023|什么是华为od,od薪资待遇,od机试题清单华为OD机试真题大全,用Python解华为机试题|机试宝典【华为OD机试】全流程解析+经验分享,题型分享,防作弊指南华为o