C++ Arrays and Strings
Working with arrays and string manipulation
Arrays
int arr[5]; # declare array of 5 ints
int nums[] = {1, 2, 3, 4, 5}; # initialize array
arr[0] = 10; # set first element
cout << nums[2]; # access third element (3)
Multi-dimensional Arrays
int matrix[3][3]; # 3x3 matrix
int grid[2][3] = {{1,2,3}, {4,5,6}}; # initialize 2D array
matrix[0][1] = 5; # set element
C-Style Strings
char str[10] = "Hello"; # character array
strlen(str); # get length (5)
strcpy(str, "World"); # copy string
strcat(str, "!"); # concatenate
C++ String Class
#include <string> # include string
string text = "Hello"; # create string
text.length() # get length
text + " World" # concatenate
text.substr(0, 3) # get substring "Hel"
text.find("ll") # find position (2)