64,646
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector<int> ivec;
int ival;
//Enter the data
cout<< "Enter the number(Ctrl+z to quit)"<< endl;
while(cin>> ival)
ivec.push_back(ival);
//calculate the sum and cout
if(ivec.size()== 0)
{
cout<< "No elements!"<< endl;
return -1;
}
for(vector<int>::size_type ix= 0; ix< ivec.size()-1; ix= ix+2)
{
cout<< ivec[ix]+ ivec[ix+1]<< '\t';
if(ix% 6== 0)
cout<< endl;
}
//the last element
if(ivec.size()% 2!= 0)
{
cout<< "The last element has no pairs"<< endl;
cout<< "Its value is "<< ivec[ivec.size()-1]<< endl;
}
return 0;
}
cout<< "Enter the number(<Ctrl+z><Enter> to quit)"<< endl;
while(cin>> ival)
while 的语法想必LZ是知道的,我想LZ应该去考虑下cin >> ival 这条语句的返回值是什么,C++有个特性它没有真正意义上的Boolean类型的,对于结果为0的数都看做是False,所有结果不为0的数都看做是True。