码迷,mamicode.com
首页 > 编程语言 > 详细

c++ vector使用小例子

时间:2017-11-21 01:16:59      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:小例子   out   system   number   ase   last   end   lease   names   

#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;
}

 

c++ vector使用小例子

标签:小例子   out   system   number   ase   last   end   lease   names   

原文地址:http://www.cnblogs.com/bohat/p/7868729.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!