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] << "\t";

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

posted @ 2014-05-04 23:10  庄浩  阅读(98)  评论(0编辑  收藏  举报