Chapter Seven

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

A partially filled array is normally used with an accompanying integer variable that holds the number of items stored in the array.

When you process a partially filled array, you must only process the elements that contain valid data items. When you populate an array incompletely. You should have a counter that keeps track of how many of the elements contain data.

vector<int> set2(set1);

After this statement executes, set2 will be a copy of set1 .

Parallel Arrays By using the same subscript, you can build relationships between data stored in two or more arrays.

It's especially useful when the related data is of unlike types. The same subscript is used to access multiple arrays at the same element.

the STL vector The Standard Template Library offers a vector data type, which in many ways, is superior to standard arrays

The Standard Template Library (STL) is a collection of data types and algorithms that you may use in your programs. These data types and algorithms are programmer-defined . They are not part of the C++ language

C++ does not perform array bounds checking. Note: It seems to be possible to increment an array's declarator to increase the array's capacity. Using a non-constant variable as the declarator.

This means you can write programs with subscripts that go beyond the boundaries of a particular array. int size = 1; int array[size]; size++; // increase the array's declarator by 1 {Works}

pop_back member function removes the last element from a vector.

collection.pop_back(); This removes the last element from the collection vector.

push_back Member Function You cannot use the [] operator to access a vector element that does not exist.

To store a value in a vector that does not have a starting size, or that is already full, use the push_back member function. The push_back member function accepts a value as an argument and stores that value after the current last element in the vector.

Defining a vector vector<int> numbers; // numbers is a vector of integers vector<int> numbers { 10, 20, 30, 40 }; // This initializes the numbers vector with four elements.

To use vector s in your program, you must include the vector header file with the following statement: #include <vector> You do not have to declare the number of elements that the vector will have.

Arrays may be initialized when they are defined. The series of values inside the braces and separated with commas is called an initialization list . Not all elements have to be initialized. The remaining uninitialized will be 0

const int NUMBERS = 6; int days[NUMBERS] = {10, 20, 30, 40, 50, 60}; 10 is stored in days[0] & 20 is stored in days[1] etc...

To store a value in an element that already exists in a vector , you may use the array subscript operator []

double grossPay = hours[index] * payRate[index];

the empty member function empty(); is used to determine if a vector is empty.

if (numberVector.empty()) // if this is true the vector is empty cout << "No values in numberVector.\n";

The range-based for loop: is a loop that iterates once for each element in an array. Each time the loop iterates, it copies an element from the array to a variable. The next value is 3 The next value is 6

int numbers [ ]= { 3, 6}; // This will call all elements in for (int val : numbers) // the numbers array { cout << "The next value is " << val << endl;; }

You can define a starting size for a vector vector<int> numbers(10); defines numbers as 10 ints

vector<int> numbers(10, 2); numbers is defined as a vector of 10 ints. Each element is initialized to a value of 2.

An array allows you to store and work with multiple values of the same data type. If an array is defined globally, all of its elements are initialized to zero by default. Array elements may be used with the cin and cout objects like any other variable.

An array works like a variable that can store a group of values, all of the same type. The values are stored together in consecutive memory locations. Local arrays have no default initialization value. You can use any integer expression as an array subscript. cin >> hours[count − 1]; count - 1 is the subscript.

Two-Dimensional Arrays A two-dimensional array is like several identical arrays put together. It is useful for storing multiple sets of data.

Array with 3 rows and 4 columns const int NUM_DIVS = 3, NUM_QTRS = 4; double sales[NUM_DIVS][NUM_QTRS];

It's possible to define an array without specifying its size, as long as you provide an initialization list.

C++ automatically makes the array large enough to hold all the initialization values. An array with five elements: double ratings[ ]= {1.0, 1.5, 2.0, 2.5, 3.0};

Arrays with Three or More Dimensions When writing functions that accept multi-dimensional arrays as arguments, all but the first dimension must be explicitly stated in the parameter list.

C++ does not limit the number of dimensions that an array may have. It is possible to create arrays with multiple dimensions, to model data that occur in multiple sets

When a two-dimensional array is passed to a function, the parameter type must contain a size declarator for the number of columns. When the compiler generates code for accessing the elements of a two-dimensional array, it needs to know how many bytes separate the rows in memory. The number of columns is a critical factor in this calculation.

Function Header from 7-22 p.422 void showArray(const int numbers[][COLS], int rows) C++ requires the columns to be specified in the function prototype and header because of the way two-dimensional arrays are stored in memory.

When initializing a two-dimensional array, it helps to enclose each row's initialization list in a set of braces. This helps distinguish the rows from the columns. int hours[rows][columns] int hours[3][2] ---------------Column 0-------Column 1 Row 0-----|-----8---------|--------5---- Row 1-----|-----7---------|--------9---- Row 2-----|-----6---------|--------3----

Here is an example: int hours[3][2] = {{8, 5}, {7, 9}, {6, 3}}; hours[0][0] is set to 8 hours[0][1] is set to 5 hours[1][0] is set to 7 hours[1][1] is set to 9 hours[2][0] is set to 6 hours[2][1] is set to 3

The data types that are defined in the STL are commonly called containers . They are called containers because they store and organize data.

Only two types of containers in the STL: sequence containers and associative containers. A sequence container organizes data in a sequential fashion, similar to an array. Associative containers organize data with keys, which allow rapid, random access to elements stored in the container.

An array's size declarator must be a constant integer expression with a value greater than zero. It can be either a literal, as in the previous example, or a named constant, as shown in the following: const short NUM_DAYS = 6; short days[NUM_DAYS]; This array uses 12 bytes of memory. short = 2 bytes each.

The following are all valid array definitions: float temperatures[100];//Array of 100 floats string names[10];//Array of 10 string objects long units[50];// Array of 50 long integers double sizes[1200];// Array of 1200 doubles The amount of memory used by an array is the data type multiplied by the DECLARATOR.

int days[6]; The name of this array is days. The days array can store six elements, each one an integer. Each element is assigned a number known as a subscript . The first element is assigned the subscript 0 int days[6] has subscripts 0 through 5

The value inside the brackets is the array's size DECLARATOR. It indicates the number of elements , or values, the array can hold. A subscript is used as an index to pinpoint a specific element within an array. days[0] is read as "days sub zero"

vector<int> numbers { 10, 20, 30, 40 };

The vector will have 4 elements, initialized with the values 10, 20, 30, and 40. No equals sign is needed.

Using the Range-Based for Loop with a vector

With C++ 11, you can use a range-based for loop to step through the elements of a vector p.433 7-25 .

Individual array elements are processed like any other type of variable. Each element in an array is treated as a separate variable. sum = num[1] + num[2];

You can increment, or decrement elements in an array: num[1] = num[1]++; // Post-increment ++num[1]; // Pre-increment amount[count]++ // This increments the value of the ELEMENT represented by the value of the count variable.

Arrays as Function Arguments NOTE: When an array is passed as an argument to a function. The function CAN make changes to the array's elements directly.

You prevent a function from making changes to an array argument by using the const key word in the parameter declaration: void showValues(const int nums[ ], int size) ALWAYS use const array parameters in any function that is not intended to modify its array argument.

You can declare the range variable as a reference variable. This allows you to modify the contents of the array. One element at a time from 0 to the end of array. simply write an ampersand ( & ) in front of the range variables name. You can also use the auto key word: for (auto &val : numbers)

int numbers[5]; // for loop with &val reference variable for (int &val : numbers) // This will ask for input for each { // element in the numbers array cout << "Enter an integer value: "; cin >> val; }

vector s can report the number of elements they contain. Using the size member function. size();

numValues = set.size(); numValues is an int and set is a vector After this executes, numValues will contain the number of elements in set .

clear member function completely clears all of the elements of a vector

numbers.clear(); After this, numbers will be cleared of all its elements.


Kaugnay na mga set ng pag-aaral

Biology Ch. 14 and 15 Test Topics

View Set

New Zealand CPL Aircraft technical knowledge and loading

View Set

bstrandable NCLEX Cardiovascular/Hematologic 1 of 2

View Set

Linear Regression and Ordinary Least Squares

View Set

Chapter 7: Client's Response to Illness

View Set