• 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>

template< class 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. 2017 多校2 hdu 6053 TrickGCD

    2017 多校2 hdu 6053 TrickGCD 题目: You are given an array \(A\) , and Zhu wants to know there are how ma ...

  2. JavaScript各变量类型的判断方法

    我们很容易被漂亮的代码吸引,也不知不觉的在自己的代码库中加入这些.却没有冷静的想过它们的优劣.这不,我就收集了一系列形如 "是否为……?" 的判断的boolean函数. isNul ...

  3. oracle 查看表空间的脚本

    SELECT upper(f.tablespace_name) "表空间名", d.Tot_grootte_Mb "表空间大小(M)", d.Tot_groot ...

  4. Codeforces Round #352 (Div. 2) A

    A. Summer Camp time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  5. PAT L1 049 天梯赛座位分配

    天梯赛每年有大量参赛队员,要保证同一所学校的所有队员都不能相邻,分配座位就成为一件比较麻烦的事情.为此我们制定如下策略:假设某赛场有 N 所学校参赛,第 i 所学校有 M[i] 支队伍,每队 10 位 ...

  6. bzoj [Sdoi2014]数数 AC自动机上dp

    [Sdoi2014]数数 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1264  Solved: 636[Submit][Status][Discu ...

  7. jQuery UI-Draggable 参数集合

    ·概述    在任何DOM元素启用拖动功能.通过单击鼠标并拖动对象在窗口内的任何地方移动.    官方示例地址:http://jqueryui.com/demos/draggable/      所有 ...

  8. HDU1892 See you~

    Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission( ...

  9. Hibernate 中 get()和load()的区别

    一.1.程序检索数据库中不存在的OID:     load方式检索不到的话会抛出org.hibernate.ObjectNotFoundException异常     get方法检索不到的话会返回nu ...

  10. odp.net 读写oracle blob字段

    DEVELOPER: ODP.NET Serving Winning LOBs: http://www.oracle.com/technetwork/issue-archive/2005/05-nov ...