#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> ivec;
cout << "Enter numbers(Ctr+Z to the end)" << endl;
int val;
while(cin >> val)
ivec.push_back(val);
if(ivec.size() == 0){
cout << "No elements?!" << endl;
return -1;
}
vector<int>::size_type icn = 1;
for(vector<int>::iterator iter = ivec.begin(); (iter+1) < ivec.end(); ++iter,++icn){//这样的话输出是第偶数个数的两倍,好像是先执行了++iter一样,原因是自增操作符优先//级高于加操作,所以先执行了自增,然后再执行加法操作。
cout << *iter + *(++iter) << "\t"; for(vector<int>::iterator iter = ivec.begin(); (iter+1) < ivec.end();iter += 2,++icn)
if(icn % 6 == 0)cout << *iter + *(iter + 1);
cout << endl;
}
if(ivec.size() % 2 != 0)
cout << endl
<< "the last value is available!"
<< "and its value is : "
<< ivec[ivec.end()-1]<<endl;
return 0;
}
查找网络资源知:
cout是从后向前执行,<<操作符的递归调用,先递归执行的是最后一个,参数压栈的顺序是从右向左,输出则是从左到右,
还有就是自增++、自减--操作符的优先级高于+、-,所以
#include<iostream>
using namespace std;
int main()
{
int a = 1, b = a;
cout << a << "\t" << a++ <<endl;
cout << b + ++b << endl;
return 0;
}
输出就是
1 2
4
#include<iostream>
#include<string>
using namespace std;
int main()
{
int a = 1, b = a;
cout << a << "\t" << a++ <<endl;
cout << b + ++b << endl;
char str[] = "Hello Word ";
char *str2 = str;
cout << "*str2:" << *str2 << "\t" << "*str2++ :" << *str2++ << endl;
cout << "*str2 " << *str2 << endl;
cout << "str :" << str << endl;
return 0;
}
输出:
2 1
4
*str2:H *str2++ :H
*str2e
str :Hello Word
*的优先级高于++操作,*的优先级高于++,因此*s++是先去*s输出,在进行自增操作
同时欢迎提出宝贵意见,以帮助我改进,不胜感激!!!
——桑海整理