//读取一组整数到vector 并计算头尾元素的和
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> ivec;
int ival;
//读取整数
cout << "Enter numbers(Ctrl+Z to end)." << endl;
vector<int>::size_type first, last;
while (cin >> ival)
ivec.push_back(ival);
//计算首尾元素之和
if (ivec.size() == 0)
cout << "No elements.?!" << endl;
for (first = 0, last = ivec.size() - 1; first < last; ++first, --last)
cout << ivec[first] + ivec[last] << "\t";
if (first == last)
cout << "The middle element is not summed and it's value:" << ivec[first] << endl;
return 0;
}
Exercice_3.13.1_练习使用vector2
最新推荐文章于 2021-01-02 02:09:24 发布