c++11 std::prev、std::next、std::advance与auto 使用
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);
......
}
std::next(英文原版)
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';
}
输出:
std::prev(英文原版)
使用方法与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';
}
输出:
c++11 std::prev、std::next、std::advance与auto 使用的更多相关文章
- C++11新特性,利用std::chrono精简传统获取系统时间的方法
一.传统的获取系统时间的方法 传统的C++获取时间的方法须要分平台来定义. 相信百度代码也不少. 我自己写了下,例如以下. const std::string getCurrentSystemTime ...
- C++11右值引用和std::move语句实例解析
关键字:C++11,右值引用,rvalue,std::move,VS 2015 OS:Windows 10 右值引用(及其支持的Move语意和完美转发)是C++0x将要加入的最重大语言特性之一.从实践 ...
- C++11并发——多线程条件变量std::condition_variable(四)
https://www.jianshu.com/p/a31d4fb5594f https://blog.csdn.net/y396397735/article/details/81272752 htt ...
- [转载]如何在C++03中模拟C++11的右值引用std::move特性
本文摘自: http://adamcavendish.is-programmer.com/posts/38190.htm 引言 众所周知,C++11 的新特性中有一个非常重要的特性,那就是 rvalu ...
- 【C/C++开发】C++11的模板类型判断——std::is_same和std::decay
C++11的模板类型判断--std::is_same和std::decay 问题提出:有一个模板函数,函数在处理int型和double型时需要进行特殊的处理,那么怎么在编译期知道传入的参数的数据类型是 ...
- c++11 线程间同步---利用std::condition_variable实现
1.前言 很多时候,我们在写程序的时候,多多少少会遇到下面种需求 一个产品的大致部分流程,由工厂生产,然后放入仓库,最后由销售员提单卖出去这样. 在实际中,仓库的容量的有限的,也就是说,工厂不能一直生 ...
- C++11新特性之二——std::bind std::function 高级用法
/* * File: main.cpp * Author: Vicky.H * Email: eclipser@163.com */ #include <iostream> #includ ...
- std::array中的std::get<n>()
模板函数std::get<n>()是一个辅助函数,它能够获取到容器的第 n 个元素.模板参数的实参必须是一个在编译时可以确定的常量表达式,编译时会对它检查. get<n>()模 ...
- gcc编译链接std::__cxx11::string和std::string的问题
今天公司的小伙伴遇到一个问题,这里做一个记录. 问题是这样的,他编译了公司的基础库,然后在程序中链接的时候遇到点问题,报错找不到定义. 用到的函数声明大概是这样的: void function(con ...
随机推荐
- BZOJ1233 [Usaco2009Open]干草堆tower 【单调队列优化dp】
题目链接 BZOJ1233 题解 有一个贪心策略:同样的干草集合,底长小的一定不比底长大的矮 设\(f[i]\)表示\(i...N\)形成的干草堆的最小底长,同时用\(g[i]\)记录此时的高度 那么 ...
- 静态区间第k大 树套树解法
然而过不去你谷的模板 思路: 值域线段树\([l,r]\)代表一棵值域在\([l,r]\)范围内的点构成的一颗平衡树 平衡树的\(BST\)权值为点在序列中的位置 查询区间第\(k\)大值时 左区间在 ...
- 《R语言实战》读书笔记--第一章 R语言介绍
1.典型的数据分析过程可以总结为一下图形: 注意,在模型建立和验证的过程中,可能需要重新进行数据清理和模型建立. 2.R语言一般用 <- 作为赋值运算符,一般不用 = ,原因待考证.用-> ...
- bzoj [Sdoi2014]数数 AC自动机上dp
[Sdoi2014]数数 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 1264 Solved: 636[Submit][Status][Discu ...
- hdu 6119 …&&百度之星 T6
小小粉丝度度熊 Problem Description 度度熊喜欢着喵哈哈村的大明星——星星小姐. 为什么度度熊会喜欢星星小姐呢? 首先星星小姐笑起来非常动人,其次星星小姐唱歌也非常好听. 但这都不是 ...
- UVA 10334 Ray Through Glasses
自己手动画了第三项发现f[3]=5;就猜斐波那契了.实际上光线分为两种距离外界有2面玻璃,1面玻璃 其分别时n-1次反射,n-2次反射形成的 故推出斐波那契. 手动一些f1,f2,f3就OK #inc ...
- c专家编程读书笔记
无论在什么时候,如果遇到malloc(strlen(str));,几乎可以直接断定他是错误的,而malloc(strlen(str)+1):才是正确的: 一个L的NUL哟关于结束一个ACSII字符串: ...
- ubuntu默认用户分析
harvey@ubuntu:/etc$ cat -b passwd root:x:::root:/root:/bin/bash daemon:x:::daemon:/usr/sbin:/bin/sh ...
- python爬虫beautifulsoup4系列1【转载】
本篇转自博客:上海-悠悠 原文地址:http://www.cnblogs.com/yoyoketang/tag/beautifulsoup4/ 前言 以博客园为例,爬取我的博客上首页的发布时间.标题. ...
- Jquery实现逐屏加载图片
引用jquery.scrollLoading.js $(document).ready(function () { //实现图片慢慢浮现出来的效果 $("img").load(fu ...