• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • 安全教程
  • 安全设置
  • 杀毒防毒
  • 病毒查杀
  • 脚本攻防
  • 入侵防御
  • 工具使用
  • 业界动态
  • Exploit
  • 漏洞分析
  • 加密解密
  • 手机安全
  • 区块链
您的位置:首页 > 网络安全 >区块链 > EOS开发的c++指南——迭代器和Lambda表达式

EOS开发的c++指南——迭代器和Lambda表达式

作者:区块网 字体:[增加 减小] 来源:互联网

区块网向大家分享了EOS开发的c++指南——迭代器和Lambda表达式,其中包含EOS开发,c++指南等知识点,遇到此问题的同学们可以参考下

让我们来讨论迭代器,这是一个非常有用的工具,在整个EOS代码库中都被大量使用。如果您处于JavaScript的背景下,您可能已经熟悉迭代器,就像循环中使用的迭代器一样。迭代器的关键概念是提供一种更好的遍历项集合的方法。额外的好处是,您可以为任何定制类实现迭代器接口,使迭代器成为遍历数据的通用方法。


// @url: https://repl.it/@MrToph/CPPBasics-Iterators
#include <iostream>
#include <vector>
using namespace std;
int main()
{
  vector<int> v{2, 3, 5, 8};
  // old way to iterate
  for (int i = 0; i < v.size(); i++)
  {
    cout << v[i] << "\n";
  }
  // using Iterators
  // begin() returns an iterator that points to the beginning of the vector
  // end() points to the end, can be compared using != operator
  // iterators are incremented by using the + operator thanks to operator-overloading
  for (vector<int>::iterator i = v.begin(); i != v.end(); i++)
  {
    // iterators are dereferenced by * like pointers
    // returns the element the iterator is currently pointing to
    cout << *i << "\n";
  }
  // auto keyword allows you to not write the type yourself
  // instead C++ infers it from the return type of v.begin
  for (auto i = v.begin(); i != v.end(); i++)
  {
    cout << *i << "\n";
  }
  // can use arithmetic to "jump" to certain elements
  int thirdElement = *(v.begin() + 2);
  cout << "Third: " << thirdElement << "\n";
  // end is the iterator that points to the "past-the-end" element
  // The past-the-end element is the theoretical element that would follow the last element in the vector.
  // It does not point to any element, and thus shall not be dereferenced.
  int lastElement = *(v.end() - 1);
  cout << "Last: " << lastElement << "\n";
  // do not go out of bounds by iterating past the end() iterator
  // the behavior is undefined
  // BAD: v.end() + 1, v.begin() + 10
}


在现代c++中,迭代器是迭代元素集合(向量、列表、映射)的首选方法。另外,可以避免键入冗长的类型,但可能会导致缺乏表现力的代码。


Lambda表达式


有了迭代器,我们可以开始研究现代c++的函数式编程概念。标准库中的许多函数都将由两个迭代器(开始和结束)和一个匿名函数(lambda函数)表示的元素范围作为参数。然后将这个匿名函数应用于范围内的每个元素。它们之所以被称为匿名函数,是因为它们不绑定到变量,而是绑定于短逻辑块中,作为内联参数传递给高阶函数。通常,它们对于传递给它们的函数是唯一的,因此不需要拥有名称。
有了它,我们可以实现类似于排序、映射、过滤等结构,这些结构在JavaScript等语言中很容易实现:


[1,2,3,4].map(x => x*x).filter(x => x % 2 === 1).sort((a,b) => b - a)


c++中的代码并不简洁,但是结构是一样的。STD库中的许多函数式编程助手都是在半开区间上操作的,这意味着包含了较低的区间,而排除了较高的区间。


// @url: https://repl.it/@MrToph/CPPBasics-Lambdas
#include <iostream>
#include <vector>
// for sort, map, etc.
#include <algorithm>
using namespace std;
int main()
{
  vector<int> v{2, 1, 4, 3, 6, 5};
  // first two arguments are the range
  // v.begin() is included up until v.end() (excluded)
  // sorts ascending
  sort(v.begin(), v.end());
  // in C++, functions like sort mutate the container (in contrast to immutability and returning new arrays in other languages)
  for (auto i = v.begin(); i != v.end(); i++)
  {
    cout << *i << "\n";
  }
  // sort it again in descending order
  // third argument is a lambda function which is used as the comparison for the sort
  sort(v.begin(), v.end(), [](int a, int b) { return a > b; });

您可能想查找下面的文章:

  • EOS开发的c++指南——迭代器和Lambda表达式

相关文章

  • 比特币背后的机制:POS和POW全解析
  • INT的理论价值——梅特卡夫定律
  • Userfeeds: 为什么网络需要一套全新的信息排名系统
  • 如何安全地存储数字加密货币
  • 如何在Linux系统建立自己的闪电网络节点和通道
  • 比特币钱包地址、私钥和公钥:到底是啥关系?
  • 区块链技术六大核心算法
  • 为什么比特币矿工要花时间挖空块
  • 分片技术(sharding)——区块链扩容问题的良方
  • 数字货币与加密货币之间的关系

文章分类

  • 安全教程
  • 安全设置
  • 杀毒防毒
  • 病毒查杀
  • 脚本攻防
  • 入侵防御
  • 工具使用
  • 业界动态
  • Exploit
  • 漏洞分析
  • 加密解密
  • 手机安全
  • 区块链

最近更新的内容

    • 如何玩转以太坊养猫游戏
    • 比特币钱包是如何帮助用户完成一笔交易的?
    • 以太坊经典ETC简史
    • 寻找小丑鱼Nemo:一文读懂区块链工作量证明机制
    • 智能合约攻击:我们都应该从DAO中吸取经验
    • 区块链VS Linux 深度剖析开源系统商业价值如何变现
    • 区块链如何帮助品牌识别超级用户并最大化社交参与度
    • 萌奈币(MonaCoin)挖矿教程
    • 区块链性能测评实战案例
    • 从技术现实理解区块链:基于SQL模型创建BQL

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有