#include <iostream> #include <vector> using std::vector; using std::cin; using std::cout; using std::endl; //不是很完美,感觉可以再优化 int main(){ int inter; int state; vector<int> ivec; do{ cout<<"If you want to push an integer to a vector,input 1 else input 0"<<endl; cin>>state; if(state == 1){ cout<<"Please input an integer."<<endl; cin>>inter; ivec.push_back(inter); } } while(state == 1); vector<int>::size_type isize = ivec.size(); if((isize % 2) == 0){ for(vector<int>::size_type i = 0; i < ivec.size(); i = i + 2) cout<<ivec[i]<<" + "<<ivec[ i + 1]<<" = "<<ivec[i]+ivec[i+1]<<endl; } else{ for(vector<int>::size_type i = 0; i < ivec.size()-1; i = i + 2) cout<<ivec[i]<<" + "<<ivec[i + 1]<<" = "<<ivec[i]+ivec[i+1]<<endl; cout<<"The last one "<<ivec[ivec.size()-1]<<" has no sum."<<endl; } system("pause"); return 1; }
转载于:https://www.cnblogs.com/zhaokaikai/archive/2012/10/08/2715677.html