vector那些事,该怎么处理
vector那些事
#include "stdafx.h"
#include<vector>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
vector<int> ivec;
int ival;
for(int i=0;i<5;i++)
{
cin>>ival;
ivec.push_back(ival);
}
if(ivec.size()==0)
return 0;
vector<int>::iterator a,b;
for(a=ivec.begin(),b=ivec.end()-1;a<=b;++a,++b)
cout<<(*a)+(*b)<<endl;
if(ivec.size()%2!=0)
cout<<"the last number is not been summed"<<endl;
return 0;
}
照c++primer习题上的,可是有错误啊。。。求大神指点
------解决方案--------------------
++a,--b
我猜测你这里打错了
------解决方案--------------------
修改成--b可以输出,错误是什么?

------解决方案--------------------
楼主到底要干什么?从两头往中间加的话,最后一句输出的信息也有问题啊。
------解决方案--------------------
加上system("pause")
应该是输出太快,没阻塞你看不见
------解决方案--------------------
应该是这个样子。控制台进程在程序结束后不关闭
------解决方案--------------------
#include "stdafx.h"
#include<vector>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
vector<int> ivec;
int ival;
for(int i=0;i<5;i++)
{
cin>>ival;
ivec.push_back(ival);
}
if(ivec.size()==0)
return 0;
vector<int>::iterator a,b;
for(a=ivec.begin(),b=ivec.end()-1;a<=b;++a,++b)
cout<<(*a)+(*b)<<endl;
if(ivec.size()%2!=0)
cout<<"the last number is not been summed"<<endl;
return 0;
}
照c++primer习题上的,可是有错误啊。。。求大神指点
C++
Iterator
------解决方案--------------------
++a,--b
我猜测你这里打错了
------解决方案--------------------
修改成--b可以输出,错误是什么?
------解决方案--------------------
楼主到底要干什么?从两头往中间加的话,最后一句输出的信息也有问题啊。
------解决方案--------------------
加上system("pause")
应该是输出太快,没阻塞你看不见
------解决方案--------------------
应该是这个样子。控制台进程在程序结束后不关闭
------解决方案--------------------