博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++中vector容器的逆序访问
阅读量:4561 次
发布时间:2019-06-08

本文共 1047 字,大约阅读时间需要 3 分钟。

  今天在写个小的十进制转换程序时,遇到个问题就是关于vector容器的逆序访问问题,后来知道其中有多种方法可以解决,下面介绍我应用的两种简单方法,顺便熟悉一下vector容器的相关函数。下面是相关代码:

#include 
#include
#include
using namespace std;int main(){ int a = 0, d = 0, answer1 = 0, mod = 0, answer2 = 0; vector
m; cout << "Please input the number :" << endl; cin >> a; cout << "Please input the scale :" << endl; cin >> d; while (a) { mod = a%d; a = a / d; m.push_back(mod); } for (vector
::reverse_iterator it = m.rbegin(); it != m.rend(); it++) { answer2 = (*it) + answer2 * 10; } reverse(m.begin(), m.end()); for (vector
::iterator it = m.begin(); it != m.end(); it++) { answer1 = (*it)+answer1*10; } cout << answer1 << endl; cout << answer2 << endl;}

  程序中用蓝色和黄色标记的分别是两种不同的方法,第一种利用的是逆置迭代器,要注意逆置迭代器的初始化。第二种是利用头文件<algorithm>中的函数reverse进行容器的逆置,要注意包含头文件。两种方法都很简单和方便。

转载于:https://www.cnblogs.com/helloforworld/p/5452554.html

你可能感兴趣的文章
VS调SQL中存储过程实现登陆
查看>>
ubuntu安装hadoop2.6
查看>>
技术栈
查看>>
Docker Centos安装Redis以及问题处理
查看>>
解决JS弹出新窗口被浏览器阻止的解决方案
查看>>
最短路——SPFA算法模板
查看>>
ASP.NET Web Game 构架设计2--数据库设计
查看>>
第六章.解决大问题
查看>>
ASIHTTP
查看>>
iOS 9: UIStackView入门
查看>>
Android——SQLite数据库(一)创建数据库、创建表、初始化数据
查看>>
自然语言处理Attention Model
查看>>
URAL 1152. False Mirrors (记忆化搜索 状压DP)
查看>>
操作mdb文件的帮助类
查看>>
mysql优化
查看>>
如何写技术博客
查看>>
百度 搜索引擎 关键字 算法
查看>>
git 命令
查看>>
13.UiAutomator 辅助APK的使用
查看>>
通过ida dump Uinity3D的加密dll
查看>>