Array of Strings in C++
There are three ways to create Array of Strings in C++.
- Using a two-dimensional array (C/C++)
1.1 Strings represented by character arrays
There are two types of initialization for character arrays
char str2[6]="china"; char str3[5] = {'c','h','i','n','a'};
The second initialization method is to initialize the character array with each individual character. In this case, there is no need to save' 0', as long as the space of the character array is satisfied >= the size of the character to be allocated.
Note: The assignment of character arrays cannot be assigned by a single string, but only by a single character.
In C language, character pointers can also be used to point to strings:
STR refers to a memory space of 5 bytes in size, in which there is no' 0', and the first address of this space is stored in str1.char *str1="china";
Unlike character arrays, character pointers can be assigned as a whole string:
str1 = "I love China";
char color[][10] = {"blue","Red","orange","yellow"};
1.2 Representing multiple strings with two-dimensional arrays
char color[][10] = {"blue","Red","orange","yellow"};//sizeof(color) = 40for(int i = 0; i < 4;i++) cout<<color[i]<<endl; //Because C/C++ multidimensional arrays are row-first, the elements of color arrays are distributed as follows:
b | l | u | e | \0 | \0 | \0 | \0 | \0 | \0 |
R | e | d | \0 | \0 | \0 | \0 | \0 | \0 | \0 |
o | r | a | n | g | e | \0 | \0 | \0 | \0 |
y | e | l | l | o | w | \0 | \0 | \0 | \0 |
color[i] is the first address of the string in line I.
Output is
The disadvantages of this method are:
- Number of Strings and Size of String – both the values are fixed.
- A 2D array is allocated, whose second dimension is equal to maximum sized string which causes wastage of space.
- Use string (C++)
The disadvantages of this method are:#include<bits/stdc++.h> using namespace std; int main() { // Initialize String Array string colour[4] = {"Blue", "Red", "Orange", "Yellow"}; // Print Strings for (int i = 0; i < 4; i++) cout << colour[i] << "\n"; }
Array is of fixed Size
-
Use vectors (C++) STL Container Vector can be used to dynamically allocate Array.
// C++ program to demonstrate vector of strings using #include<bits/stdc++.h> using namespace std; int main() { // Declaring Vector of String type vector <string> colour; // Initialize vector with strings using push_back // command colour.push_back("Blue"); colour.push_back("Red"); colour.push_back("Orange"); colour.push_back("Yellow"); // Print Strings stored in Vector for (int i=0; i<colour.size(); i++) cout << colour[i] << "\n"; }
From the above three points, vector should be the best way to create string arrays in c++.
Note:' 0'is a string terminator in C language, so only an array will save it with an extra byte when it processes a string. In other cases (non-character arrays), the array will not save it.
Array Decay in C++
outputWhat is Array Decay?
Array degradation refers to the type and size of array elements that are missing from an array. This usually happens when a pointer parameter is passed to a function by value. The result is that only the first address of the argument is passed to an array of parameters. This is an array that degenerates into a pointer, and the size of the array becomes the size of the pointer instead of the original array.
For a simple example:
#include <iostream> using namespace std; void findSize(int arr[]) { cout << sizeof(arr) << endl; } int main() { int a[10]; cout << sizeof(a) << " "; findSize(a); return 0; }
40 8d
// C++ code to demonstrate array decay
#include<iostream>
using namespace std;
// Driver function to show Array decay
// Passing array by value
void aDecay(int *p)
{
// Printing size of pointer
cout << "Modified size of array is by "
" passing by value: ";
cout << sizeof(p) << endl;
}
// Function to show that array decay happens
// even if we use pointer
void pDecay(int (*p)[7])
{
// Printing size of array
cout << "Modified size of array by "
"passing by pointer: ";
cout << sizeof(p) << endl;
}
int main()
{
int a[7] = {1, 2, 3, 4, 5, 6, 7,};
// Printing original size of array
cout << "Actual size of array is: ";
cout << sizeof(a) <<endl;
// Calling function by value
aDecay(a);
// Calling function by pointer
pDecay(&a);
return 0;
}
output
Actual size of array is: 28 Modified size of array by passing by value: 8 Modified size of array by passing by pointer: 8
How to prevent Array Decay?
A typical solution is to pass the size of the array to the function as a parameter, and not to use sizeof for the array parameters.
Another solution is to pass arrays by reference to functions, thus avoiding the transformation of arrays into pointers and avoiding degradation. such as
// C++ code to demonstrate prevention of
// decay of array
#include<iostream>
using namespace std;
// A function that prevents Array decay
// by passing array by reference
void fun(int (&p)[7])
{
// Printing size of array
cout << "Modified size of array by "
"passing by reference: ";
cout << sizeof(p) << endl;
}
int main()
{
int a[7] = {1, 2, 3, 4, 5, 6, 7,};
// Printing original size of array
cout << "Actual size of array is: ";
cout << sizeof(a) <<endl;
// Calling function by reference
fun(a);
return 0;
}
Other knowledge points:
- Array pointer: int (*t)[N] denotes an array pointer. It means that t is a pointer to a block consisting of N ints stored continuously in memory. N must have the same column value as a two-dimensional array of parameters. Look at the parentheses first to see that t is a pointer, look right to see that it points to an array, and then look left, the array element is int. (All pointers can be interpreted this way, including function pointers)
- Pointer array: int *t[N] denotes that t is an array, in which the elements are of type int *.
- Array reference: int (& t) [N] indicates that t is a reference to an array, and other meanings are the same as array pointer, which can be used as an eh alias for other arrays.
How to find size of array in C/C++ without using sizeof ?
Give an example:Principle:// C++ program to find size of an array by using a // pointer hack. #include <bits/stdc++.h> using namespace std; int main() { int arr[] = {1, 2, 3, 4, 5, 6}; int size = *(&arr + 1) - arr; cout << "Number of elements in arr[] is " << size; return 0; }
The first address of the array is stored in the array name arr.