题目来自于C++Prime第四版
#include<iostream>
#include<vector>
using namespace std;
void main01()
{
vector<int> ivec;
int ival;
cout << "请输入多个整型变量:";
while (cin >> ival)//这里通过0作为数据结束的条件
{
if (ival==0)
break;
ivec.push_back(ival);
}
//进行判断是否读入数据
if (ivec.size() == 0)
{
cout << "没有元素" << endl;
return;
}
for (vector<int>::iterator it = ivec.begin(); it != ivec.end()-1; it++)
{
cout << *(it)+*(it + 1) << " ";
}
cout << endl;
//判断数据是否为奇数
if ((ivec.size() ) % 2 != 0)
{
cout << "读入元素的个数为奇数:" << *(ivec.end() - 1) << endl;
}
vector<int>::iterator it1, it2;
for (it1 = ivec.begin(), it2 = ivec.end() - 1; it2 > it1; it1++, it2--)
{
cout << *(it1)+(*it2) << " ";
}
cout << endl;
system("pause");
}
//把vector对象中的每一个单词转化为大写字母输出vector转化后的结果
void main02()
{
vector<char> c1;
char c;
while (cin >> c)
{
if (c>'z'||c<'A')
break;
c1.push_back(c);
}
for (vector<char>::iterator it = c1.begin(); it != c1.end() - 1; it++)
{
if (*it>='a'&&*it <= 'z')
{
*it -= 32;
}
}
for (vector<char>::iterator it = c1.begin(); it != c1.end() - 1; it++)
{
cout << *it;
}
//cout << c1 << endl;
system("pause");
}