#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector<int> ivec;
int ival;
cout << "Enter numbers(Ctrl+Z to end):" << endl;
while(cin >> ival)
{
ivec.push_back(ival);
}//用cin读入一组整数并把它们存入一个vector对象中
// 计算相邻元素的和并输出
if (ivec.size() == 0)
{
cout << "No element?!" << endl;
return -1;
}
cout << "Sum of each pair of adjacent elements in the vector:"<< endl;
for (vector<int>::size_type ix = 0; ix < ivec.size()-1;ix = ix + 2)
{
cout << ivec[ix] + ivec[ix+1] << "\t";
if ( (ix+1) % 6 == 0) // 每行输出6 个和
cout << endl;
}
if (ivec.size() % 2 != 0) // 提示最后一个元素没有求和
{
cout << endl
<< "The last element is not been summed "
<< "and its value is "
<< ivec[ivec.size()-1] << endl;
}
return 0;
}