Exercice_3.13.1_练习使用vector2

//读取一组整数到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] << "	";

    if (first == last)
        cout << "The middle element is not summed and it's value:" << ivec[first] << endl;
    return 0;
}

原文地址:https://www.cnblogs.com/mrbourne/p/9959475.html