c++ vector使用小例子

#include "stdafx.h"
#include <vector>
#include <iostream>
using namespace std;

 

int main()
{
vector<int> ivec;
int ival;
cout << "enter number(ctrl+z to end ):" << endl;
while (cin>>ival)
{
ivec.push_back(ival);
}
if (ivec.size() == 0)
{
cout << "no element?!" << endl;
return -1;
}
cout << "sum of each pair of adjacent element int the vector:" << endl;
cout << "the vector ivec size is :" << ivec.size() << endl;
for (vector<int>::size_type ix = 0; ix < ivec.size() - 1; ix = ix + 2)
{
cout << ivec[ix] + ivec[ix + 1] << endl;
}
if (ivec.size() % 2 != 0)
{
cout << "the last element is not been summed";
cout << "the last elementis:" << ivec[ivec.size() - 1] << endl;
}
cout << "please cin the number to :ivec[5]" << endl;
ivec[5] = 20;
cout << "ivec[5] has been change to :" << ivec[5];
system("pause");
return 0;
}

 

posted @ 2017-11-20 23:06  boht  阅读(1150)  评论(0编辑  收藏  举报