c++primer p101 3.4.2 练习3.24 未完成 很多问题

本文介绍了一个使用C++实现的控制台程序,该程序通过标准输入接收一系列整数并将其存储在向量中。之后,程序遍历向量并输出每一对相邻元素的和,同时处理了向量长度为奇数的情况。
// p101_3422.cpp : 定义控制台应用程序的入口点。
//


#include "stdafx.h"
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main()
{
vector<int> v;
int mem;
cout << "please input:" << endl;
while (cin >> mem)
{
//if( mem == -1)
//break;
v.push_back(mem);
//char ch = 'y';
//cout << "continue?" << endl;
//cin >> ch;
//if (ch != 'y')
//break;
//cout << "continue" << endl;
}
if (v.cbegin() == v.cend())
{
cout << "no datas" << endl;
return -1;
}
// cout << "continue?" << endl;
cout << "和为:" << endl;
for (auto it = v.cbegin(); it != v.cend() - 1; it++)
{
cout << (*it + *(++it)) << " ";
if ((it - v.cbegin() + 1) % 10 == 0)
cout << endl;
}


if (v.size() % 2 != 0)
cout << *(v.cend() - 1);


system("pause");
return 0;

}



1.需要判断输入流的结束吗?(前面的程序有判断,但这里没有)怎么判断?

2.it++不能写成it+1吗

3.for循环里面的容器在前面定义好不行吗?非得在for里面定义吗?

// p101_3422.cpp : 定义控制台应用程序的入口点。
//


#include "stdafx.h"
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main()
{


/*
vector<int> v;
int mem;
cout << "please input:" << endl;
while (cin >> mem)
{
//if( mem == -1)
//break;
v.push_back(mem);
//char ch = 'y';
//cout << "continue?" << endl;
//cin >> ch;
//if (ch != 'y')
//break;
//cout << "continue" << endl;
}
if (v.cbegin() == v.cend())
{
cout << "no datas" << endl;
return -1;
}
// cout << "continue?" << endl;
cout << "和为:" << endl;
for (auto it = v.cbegin(); it != v.cend() - 1; it++)
{
cout << (*it + *(++it)) << " ";
if ((it - v.cbegin() + 1) % 10 == 0)
cout << endl;
}


if (v.size() % 2 != 0)
cout << *(v.cend() - 1);
*/
vector<int> v;
int mem;
cout << "please input:" << endl;
while (cin >> mem)
v.push_back(mem);
if (v.cbegin() == v.cend())
{
cout << "no datas" << endl;
return -1;
}
cout << "和为:" << endl;
auto beg = v.begin();
auto end = v.end();
for (auto it = beg; it != beg+(end-beg)/2; it++)
{
cout << (*it + *(beg+(end-it)-1))<< " ";
if ((it - v.cbegin() + 1) % 10 == 0)
cout << endl;
}
if (v.size() % 2 != 0)
cout << *(v.begin() + (v.end() - v.begin()) / 2) << endl;
system("pause");
return 0;
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值