问题:使用条件运算符从vector<int>中找到值是奇数的元素,并将这些奇数值翻倍。
1.条件运算符:a>b?c:d,为三目运算符;
2.在遍历和改变数组、容器等序列元素时,使用范围for循环比较方便;(范围for循环详见该书168页)
3.在给容器添加元素时,不要用下标进行操作;
4.使用while循环结束时用crtl+z;
5.system("pause");语句是为了能显示控制台;
6.本人使用的平台是vs2015;
7.望各位网友批评指正。
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> v_int;
int value;
cout << "请输入元素值:";
while (cin >> value)
v_int.push_back(value);
for (auto p : v_int)
cout << p << ' ';
cout << endl;
for (auto& s : v_int)
s = ((s % 2) == 0 ? s : s * 2);
for (auto p : v_int)
cout << p << ' ';
system("pause");
return 0;
}