<C++ Primer_5th>习题_3.24
来源:互联网 发布:ubuntu修改hosts翻墙 编辑:程序博客网 时间:2024/06/06 03:50
//读入一组整数并把它们存入一个vector对象,将每对相邻整数的和输出出来。//使用迭代器实现#include<iostream>#include<vector>using namespace std;int main(){vector<int> v_int;int i_val;cout << "请输入一组数字: " << endl;while (cin >> i_val)v_int.push_back(i_val);if(v_int.cbegin() == v_int.cend()) //if (v_int.size() == 0){cout << "没有任何元素 " << endl;system("pause");return -1;}cout << "相邻两项的和依次是: " << endl;//奇数项if(v_int.size() % 2 != 0){ for (auto it = v_int.cbegin(); it != v_int.cend() - 1; ++it){//输出两项和cout << (*it + *(++it)) << " ";//每行输出5个数字if ((it - v_int.cbegin() + 1) % 10 == 0)cout << endl;}//如果元素个数是奇数,单独处理最后一个元素if (v_int.size() % 2 != 0)cout << *(v_int.cend() - 1) << endl;}//偶数项else{for (auto it = v_int.cbegin(); it != v_int.cend(); ++it){//输出两项和cout << (*it + *(++it)) << " ";//每行输出5个数字if ((it - v_int.cbegin() + 1) % 10 == 0)cout << endl;}} cout << endl; system("pause");return 0;}//求首尾元素和的程序/*#include<iostream>#include<vector>using namespace std;int main(){vector<int> v_int;int i_val;cout << "请输入一组数字: " << endl;while (cin >> i_val)v_int.push_back(i_val);if (v_int.cbegin() == v_int.cend()){cout << "没有任何元素: " << endl;system("pause");return 0;}cout << "首尾两项的和依次是: " << endl;auto beg = v_int.cbegin();auto end = v_int.cend();auto mid = beg+(end - beg) / 2; //若写成这样auto mid = (end - beg) / 2是错的for (auto it = beg; it !=mid ; ++it){cout << (*it + *(beg + (end - it) - 1)) << " ";if ((it - beg + 1) % 5 == 0)cout << endl;}if (v_int.size() % 2 != 0)cout << *(beg + (end - beg) / 2);cout << endl;system("pause");return 0;}*/
阅读全文
0 0
- <C++ Primer_5th>习题_3.24
- <C++ Primer_5th>习题_3.2
- <C++ Primer_5th>习题_3.3
- <C++ Primer_5th>习题_3.4
- <C++ Primer_5th>习题_3.5
- <C++ Primer_5th>习题_3.6
- <C++ Primer_5th>习题_3.10
- <C++ Primer_5th>习题_3.14
- <C++ Primer_5th>习题_3.16
- <C++ Primer_5th>习题_3.17
- <C++ Primer_5th>习题_3.20
- <C++ Primer_5th>习题_3.22
- <C++ Primer_5th>习题_3.23
- <C++ Primer_5th>习题_3.25
- <C++ Primer_5th>习题_3.31
- <C++ Primer_5th>习题_3.35
- <C++ Primer_5th>习题_3.36
- <C++ Primer_5th>习题_1.3
- <C++ Primer_5th>习题_3.23
- Spring框架-第一弹
- ios-frame和bounds
- LinkList_Stack(链式栈)
- 第2篇 SercureCRT使用root远程登录ubuntu16.04
- <C++ Primer_5th>习题_3.24
- CodeForces
- <C++ Primer_5th>习题_3.25
- <C++ Primer_5th>习题_3.31
- 3 文本编辑器vim
- 基于Excel的QR二维码生成工具——原理及算法详解(之七)
- <C++ Primer_5th>习题_3.35
- POJ—487-3279
- <C++ Primer_5th>习题_3.36