Arrays and Vectors

Arrays and Vectors

Copyright: © 2024 |Pages: 66
DOI: 10.4018/979-8-3693-2007-5.ch003
OnDemand:
(Individual Chapters)
Available
$37.50
No Current Special Offers
TOTAL SAVINGS: $37.50

Abstract

Chapter 3 dives deep into the core data structures of C++: arrays and vectors. Beginning with the fundamentals, it introduces the basic syntax and declaration of arrays and vectors, illustrated through concise examples. It elaborates on accessing array elements and efficiently iterating over them using loops. The chapter emphasizes array initialization techniques—ranging from direct initialization lists to loops. A focus on the range-based 'for' loop brings modern C++ practices to the fore. The intricate dynamics of parallel arrays, comparing arrays, and leveraging arrays in functions are explored. Diverse array structures, including static and multidimensional arrays, are expounded upon, offering insights into 2D arrays and jagged arrays. The chapter proceeds to discuss search and sort techniques, comparing their efficiencies. The versatility of the C++ Standard Library's vector template is introduced, diving into vector declaration, initialization, and algorithms. The latter section offers a deep dive into vector performance and its underlying intricacies.
Chapter Preview
Top

3. Introduction

An array is a data structure composed of similar data items. When an array is declared, its size is specified and cannot be changed at runtime. The elements of an array are all of the same kind and are stored in a continuous pool of memory regions. To refer to a particular position or element in the array, we must supply both the array's name and the element's position number. Elements of an array may be accessed using their index, which starts at 0 for the first item.

Vectors are comparable to arrays, although there are significant distinctions. Vectors are dynamic, meaning that their dimensions might vary during execution. In addition, vectors have operations for adding, deleting, and accessing items. Typically, vectors are used when the amount of the data to be stored is unknown in advance or if the size of the data may vary during runtime.

C++ uses both arrays and vectors to store collections of data, but the decision between the two depends on the unique use case and application needs.

Array Example: int c[5];

c[0]=10;
c[1]=10;
c[2]=10;
c[3]=10;
c[4]=10;

This array has five items. A program may refer to any of these items by surrounding the array's name and the element's position number inside square brackets ([]). Formally, the slot number is called as the index or index (this number specifies the number of elements from the beginning of the array). The initial element of any array has an index of 0 (zero) and is sometimes referred to as the zero element. So, the elements of the array 'c' are c[0],c[1], c[2], etc. The greatest index of array c is 4, which is 1 less than the array's capacity of 5 items. Using the same naming guidelines as other variable names, array names must be identifiers.

Index must be an integer or integer [deleted] using any integral type. If a program uses an expression as an index, the expression is evaluated to determine the index. c[a + b] += 2; adds 2 to the array member c[3] if we believe that variable an equals 1 and variable b equals 2. Notice that the indexed array name is a value that may be used on the left side of an assignment, similar to a variable name that is not an array.

Vector Example

#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> myVector; // create an empty vector of integers
// add elements to the vector
myVector.push_back(10);
myVector.push_back(20);
myVector.push_back(30);
// print out the contents of the vector
cout << “The elements of the vector are: “;
for (int i = 0; i < myVector.size(); i++) {
cout << myVector[i] << ” “;
}
return 0;
}

In this example, the program contains the <iostream> and <vector> header files. The <iostream> header offers input and output capabilities, whereas the vector> header defines the vector class.

The program then writes a main() method that uses the vector class to make a vector of integers called myVector that is empty. The pushback() technique is then used to add three numbers (10, 20, and 30) to the vector.

The program then uses a for loop to run through the vector and the cout instruction to output the vector's contents. The loop iterates for the vector's size (myVector.size()) and utilizes the square bracket notation (myVector[i]) to access each vector member.

The software then returns 0 to signify successful completion.

3.1 Declaring Arrays

Declaring an array in C++ involves defining the data type of the array's elements, the array's name, and the number of items it may carry.

Arrays take up space in memory. The programmer specifies the element type and required number of elements of an array as follows:

<datatype> <array_name>[<array_size>];

The compiler reserves the amount of RAM required. array_size must be a positive integer larger than zero. Use the declaration, for instance, to instruct the compiler to reserve 12 members for the integer array c.

int c[ 12 ];

c is an array of 12 integers, uninitialized array elements are ones that don't have a value until they are given one.

Memory can be reserved for multiple arrays with a single statement. The following statement dedicates 100 elements to the integer array b and 27 elements to the integer array x.

int b[ 100 ], // b is an array of 100 integers
int x[27 ]; // x is an array of 27 integers

Arrays can be declared to hold values of any non-reference data type. For example, an array of type char can be used to store a string of characters. So far, we have used string objects to store strings.

Complete Chapter List

Search this Book:
Reset