#include<iostream>
#include<cstdlib>
#include<vector>
#include<algorithm>
using namespace std;
//自定义函数声明
bool isOdd(int);
//主函数
int main(int argc,char * argv[])
{
//定义向量vector对象
vector<int> myVector;
//循环插入元素
for(int i=1;i<10;++i)
myVector.push_back(i);
//定义迭代器变量
vector<int>::iterator bound,it;
//partition
bound=partition(myVector.begin(),myVector.end(),isOdd);
cout<<"odd elements:";
//循环输出
for(it=myVector.begin();it!=bound;it++)
{
cout<<' '<<*it;
}
cout<<'\n'<<"even elements:";
//循环输出
for(it=bound;it!=myVector.end();it++)
{
cout<<' '<<*it;
}
cout<<'\n';
system("pause");
return 0;
}
//自定义函数
bool isOdd(int i)
{
return (i%2)==1;
}
谁能说说是怎么回事啊?
为什么for循环里的it++和++it输出结果一样?
++it和it++的执行效果是相同的
只有把自加语句放在其他语句中 才会有不同效果
比如 a=b++和a=++b就是不同的
单独的
b++;
和++b;是一样的
for循环中 ;;分隔的三段语句 和独立语句的效果相同追问
但是for循环里面不是有输出 *it 吗?如果是++it 岂不是指向下一个地址,而 it++ 才是当前地址?
追答++it 也就是for循环的第三项,是在每次循环结束时调用的
也就是说
for(A;B;C)等同于
A;
while(B)
{
xxxxxx
C;
}
所以循环体xxxxx中的执行 并不是和C一体 所以前自加和后自加没有影响
但是for循环里面不是有输出 *it 吗?如果是++it 岂不是指向下一个地址,而 it++ 才是当前地址?
追答你可以试试这个就明白了;
for(it=bound;it!=myVector.end();)
{
cout<<' '<<it++;
}
和
for(it=bound;it!=myVector.end();)
{
cout<<' '<<++it;
}
结果有什么不同