|
Figure 1 Figure of Salaries |
|
Figure 2 Vector Slot Filled with double Value |
vector<double> salaries(10);
cout << salaries[10];
double highest = salaries[0];
for (i = 1; i < 10; i++)
if (salaries[i] > highest)
highest = salaries[i];
vector<type_name> variable_name;
vector<type_name> variable_name(initial_size);
vector<int> scores;
vector<Employee> staff(20);
Define a new variable of vector type, and optionally supply an initial size.
vector_Expression[integer_expression]
salaries[i + 1]
Access an element in a vector.
for (i = 1; i < salaries.size(); i++)
if (salaries[i] > highest)
highest = salaries[i];
vector<double> salaries;
...
double s;
cin >> s;
...
salaries.push_back(s);
double last = salaries[salaries.size() - 1];
salaries.pop_back();
vector<double> data(10);
data[10] = 5.4;
string greeting = "Hello";
can be considered a vector of five characters 'H', 'e', 'l', 'l', 'o'
greeting[3] = 'p';
greeting[4] = '!';
double average(vector<double> values)
{
if (values.size() == 0) return 0;
double sum = 0;
for (int i = 0; i < values.size(); i++)
sum = sum + values[i];
return sum / values.size();
}
void raise_by_percent(vector<double>& values, double rate)
{
for (int i = 0; i < values.size(); i++)
values[i] = values[i] * (1 + rate / 100);
}
vector<double> between(vector<double> values, double low, double high)
{
vector<double> result;
for (int i = 0; i < values.size(); i++)
if (low <= values[i] && values[i] <= high)
result.push_back(values[i]);
return result;
}
vector<int> between_locations(vector<double> values, double low, double high)
{
vector<int> pos;
for (int i = 0; i < values.size(); i++)
{
if (low <= values[i] && values[i] <= high)
pos.push_back(i);
}
return pos;
}
vector<int> matches = between_locations(salaries, 45000.0, 65000.0);
for (int j = 0; j < matches.size(); j++)
cout << salaries[matches[j]] << "\n";
double average(vector<double> values)
double average(const vector<double>& values)
Removing an element — Where order is not important
|
Figure 5 Removing an Element in an Unordered Vector |
Removing an element — Where order is important
for (int i = pos; i < values.size() - 1; i++)
values[i] = values[i + 1];
values.pop_back();
See erase2.cpp |
Figure 6 Removing an Element in an Ordered Vector |
Inserting, other than at the end
int last = values.size() - 1;
values.push_back(values[last]);
for (int i = last; i > pos; i--)
values[i] = values[i - 1];
values[pos] = s;
See insert.cpp |
Figure 7 Inserting an Element in an Ordered Vector |
vector<string> names;
vector<double> prices;
vector<int> scores;
Figure 9 Eliminating Parallel Vectors
double salaries[10];
double salaries[] = { 31000, 24000, 55000, 82000, 49000, 42000, 35000,
66000, 91000, 60000 };
type_name variable_name[size];
int scores[20];
Define a new variable of an array type.
const int SALARIES_CAPACITY = 100;
double salaries[SALARIES_CAPACITY];
int salaries_size = 0;
while (more && salaries_size < SALARIES_CAPACITY)
{
cout << "Enter salary or 0 to quit: ";
double x;
cin >> x;
if (cin.fail())
more = false;
else
{
salaries[salaries_size] = x;
salaries_size++;
}
}
double highest = 0;
if (salaries_size > 0)
{
highest = salaries[0];
int i;
for (i = 1; i < salaries_size; i++)
if (salaries[i] > highest)
highest = salaries[i];
}
double maximum(double a[], int a_size);
double maximum(const double a[], int a_size)
void read_data(double a[], int a_capacity, int& a_size)
{
a_size = 0;
while (a_size < a_capacity)
{
double x;
cin << x;
if (cin.fail()) return;
a[a_size] = x;
a_size++;
}
}
void between(double values[], int values_size, double low, double high,
double result[], double& result_size)
|
Figure 10 A Character Array |
int strlen(const char s[])
{
int i = 0;
while (s[i] != '\0')
i++;
return i;
}
|
Figure 11 Accessing an Element in a Two-Dimensional Array |
for (int i = 0; i < POWERS_ROWS; i++)
for (int j = 0; j < POWERS_COLS; j++)
powers[i][j] = value for row i, column j;
i * POWERS_COLS + j
Figure 12 A Two-Dimensional Array Is Stored as a Sequence of Rows
void print_table(const double table[][POWERS_COLS], int table_rows)
{
const int WIDTH = 10;
cout << fixed >> setprecision(2);
for (int i = 0; i < table_rows; i++)
{
for (int j = 0; j < POWERS_COLS; j++)
cout << setw(WIDTH) << table[i][j];
cout << "\n";
}
}
type_name variable_name[size1][size2];
double monthly_sales[NREGIONS][12];
Define a new variable that is a two-dimensional array.
double maximum(const double a[], int a_size)
void print(const double table[][], int table_rows,
int table_cols) // NO!
const int TABLE_COLS = 6;
void print(const double table[][TABLE_COLS],
int table_rows) // OK