32000
54000
67500
29000
35000
80000
highest value => 115000
44500
100000
65000
vector<double> salaries(10);
Syntax 9.1: Vector Variable Definition vector<type_name> variable_name;
|
salaries[4] = 35000;
cout << salaries[4] << "\n";
Syntax 9.2: Vector Subscripts vector_expression[integer_expression]
|
vector<double> salaries(10);
cout << salaries[10]; /* COMMON ERROR! Legal subscripts are 0 until 9 */
vector<double> salaries; /* no size given */
salaries[0] = 35000; /* ERROR ! */
for(i = 0; i < salaries.size(); i++)
do something with salaries[i];
vector<double> salaries;
. . .
double s;
cin >> s;
. . .
salaries.push_back(s);
salaries.pop_back(); /* Now salaries has size 9 */
double average(vector<double> v)
{ if (v.size() == 0) return 0;
double sum = 0;
for (int i = 0; i < v.size(); i++) sum = sum + v[i];
return sum / v.size();
}
void raise_by_percent(vector<double>& v, double p)
{ for (int i = 0; i < v.size(); i++)
v[i] = v[i] * (1 + p / 100);
}
vector<double> between(vector<double> v, double low, double high)
{ vector<double> result;
for (int i = 0; i < v.size(); i++)
if (low <= v[i] && v[i] <= high) result.push_back(v[i]);
return result;
}
vector<int> find_all_between(vector<double> v, double low, double high)
{ vector<int> pos;
for (int i = 0; i < v.size(); i++)
if (low <= v[i] && v[i] <= high) pos.push_back(i);
return pos;
}
void erase(vector<string>& v, int pos)
{ int last_pos = v.size() - 1;
v[pos] = v[last_pos];
v.pop_back();
}
void erase(vector<string>& v, int pos)
{ for (int i = pos; i < v.size() - 1; i++) v[i] = v[i + 1];
v.pop_back();
}
void insert(vector<string>& v, int pos, string s)
{ int last = v.size() - 1;
v.push_back(v[last]);
for (int i = last; i > pos; i--) v[i] = v[i - 1];
v[pos] = s;
}
ACMA P600 Price: 995 Score75
ALaris Nx686 Price 798 Score 57
AMAX Powerstation 600 Price: 999 Score 75
AMS Infogold P600 Price: 795 Score: 69
AST PRemmia Price: 2080 Score: 80
Austin 600 Price: 1499 Score: 95
best value => Blackship NX-600 Price 598 Score: 60
Kompac 690 Price: 695 Score: 60