• auto


定义变量时放在变量前,无需知道具体变量类型,系统可自行推断类型,减少编程工作,特别是在模板使用时,使用更方便。

下面简单例子:

  auto a=;
auto b='a';
auto s="abdc";
auto c;//这样使用时错误的,系统无法自动推断出变量类型
//下面为迭代指针使用,很方便
vector<int> vec;
auto it=vec.begin();

模板使用例子:

  template<typename InputIterator>
TreeNode *creatTree(InputIterator in_beg,InputIterator in_end...)
{
.....
auto inRootPos=find(in_beg,in_end,val);
......
}

Defined in header <iterator>

templateclass ForwardIt >

ForwardIt next( ForwardIt it, 


                       typename std::iterator_traits<ForwardIt>::difference_type n = 1 );

Return the nth successor of iterator it.

Parameters

it  -- 迭代指针

n  -- 向前进的元素个数,缺省默认为1

Return value

The nth successor of iterator it.(返回it的第n个后继迭代指针)

一种实现:

template<class ForwardIt>
ForwardIt next(ForwardIt it, typename std::iterator_traits<ForwardIt>::difference_type n = )
{
std::advance(it, n);
return it;
}

综合例子:

 #include <iostream>
#include <iterator>
#include <vector> int main()
{
std::vector<int> v{ , , }; auto it = v.begin(); auto nx = std::next(it, ); std::cout << *it << ' ' << *nx << '\n';
}

输出:


使用方法与next相似,不同的是prev返回的是it的第n个前驱迭代指针

template< class BidirIt >

BidirIt prev( BidirIt it, 

                     typename std::iterator_traits<BidirIt>::difference_type n = 1 );

一种实现:

 template<class BidirIt>
BidirIt prev(BidirIt it, typename std::iterator_traits<BidirIt>::difference_type n = )
{
std::advance(it, -n);
return it;
}

例子:

 #include <iostream>
#include <iterator>
#include <vector> int main()
{
std::vector<int> v{ , , }; auto it = v.end(); auto pv = std::prev(it, ); std::cout << *pv << '\n';
}

输出:

1

此外

std::advance的使用在上面有链接,方法与prev和next相似,只是无返回指针,这里不进行说明

c++11 std::prev、std::next、std::advance与auto 使用的更多相关文章

  1. C++11新特性,利用std::chrono精简传统获取系统时间的方法

    一.传统的获取系统时间的方法 传统的C++获取时间的方法须要分平台来定义. 相信百度代码也不少. 我自己写了下,例如以下. const std::string getCurrentSystemTime ...

  2. C++11右值引用和std::move语句实例解析

    关键字:C++11,右值引用,rvalue,std::move,VS 2015 OS:Windows 10 右值引用(及其支持的Move语意和完美转发)是C++0x将要加入的最重大语言特性之一.从实践 ...

  3. C++11并发——多线程条件变量std::condition_variable(四)

    https://www.jianshu.com/p/a31d4fb5594f https://blog.csdn.net/y396397735/article/details/81272752 htt ...

  4. [转载]如何在C++03中模拟C++11的右值引用std::move特性

    本文摘自: http://adamcavendish.is-programmer.com/posts/38190.htm 引言 众所周知,C++11 的新特性中有一个非常重要的特性,那就是 rvalu ...

  5. 【C/C++开发】C++11的模板类型判断——std::is_same和std::decay

    C++11的模板类型判断--std::is_same和std::decay 问题提出:有一个模板函数,函数在处理int型和double型时需要进行特殊的处理,那么怎么在编译期知道传入的参数的数据类型是 ...

  6. c++11 线程间同步---利用std::condition_variable实现

    1.前言 很多时候,我们在写程序的时候,多多少少会遇到下面种需求 一个产品的大致部分流程,由工厂生产,然后放入仓库,最后由销售员提单卖出去这样. 在实际中,仓库的容量的有限的,也就是说,工厂不能一直生 ...

  7. C++11新特性之二——std::bind std::function 高级用法

    /* * File: main.cpp * Author: Vicky.H * Email: eclipser@163.com */ #include <iostream> #includ ...

  8. std::array中的std::get<n>()

    模板函数std::get<n>()是一个辅助函数,它能够获取到容器的第 n 个元素.模板参数的实参必须是一个在编译时可以确定的常量表达式,编译时会对它检查. get<n>()模 ...

  9. gcc编译链接std::__cxx11::string和std::string的问题

    今天公司的小伙伴遇到一个问题,这里做一个记录. 问题是这样的,他编译了公司的基础库,然后在程序中链接的时候遇到点问题,报错找不到定义. 用到的函数声明大概是这样的: void function(con ...

随机推荐

  1. spring in action 学习笔记十:用@PropertySource避免注入外部属性的值硬代码化

    @PropertySource的写法为:@PropertySource("classpath:某个.properties文件的类路径") 首先来看一下这个案例的目录结构,重点看带红 ...

  2. TMS320F28379D 使用心得之 SCI

    原文地址https://blog.csdn.net/qq_39545674/article/details/82597106 一.SCI 简介SCI(Serial Communication Inte ...

  3. android 研究的环境搭建、高效工具、网站资源

    =================  2015 年 10 月 14 号 更新 著名的android开源社区xda有一个帖子,详细描述了android开发和研究环境的初始搭建过程: http://for ...

  4. 最简单的基于FFmpeg的AVDevice例子(读取摄像头)【转】

    转自:http://blog.csdn.net/leixiaohua1020/article/details/39702113 版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[- ...

  5. error LNK2019: 无法解析的外部符号 _deflate

    我的环境为: Win764 + VS2005 + zlib1.2.8 zlib1.2.8我使用VS2010来编译. ------------------------------------------ ...

  6. LVM更换硬盘

    #检测坏道 smartctl -a /dev/sdd #硬盘检测 e2fsck -f /dev/mapper/vg_root-lv_data #重新定义空间大小,将原来的大小上减去要移走的硬盘 res ...

  7. Shiro自定义realm实现密码验证及登录、密码加密注册、修改密码的验证

    一:先从登录开始,直接看代码 @RequestMapping(value="dologin",method = {RequestMethod.GET, RequestMethod. ...

  8. hbase异常:java.io.IOException: Unable to determine ZooKeeper ensemble

    项目中用到hbase,有时候可能会报一些异常,比如java.io.IOException: Unable to determine ZooKeeper ensemble 等等,当出现这个问题时,根据个 ...

  9. redis 单机模拟 cluster集群

    一.redis-cluster设计 Redis集群搭建的方式有多种,例如使用zookeeper等,但从redis 3.0之后版本支持redis-cluster集群,Redis-Cluster采用无中心 ...

  10. pythontips(1):打印模块的属性并执行

    import sys import site def print_all(module_): modulelist = dir(module_) length = len(modulelist) fo ...