---
# System prepended metadata

title: 'Chapter 6 Arrays, Strings and Vectors'
tags: [程設筆記]

---

###### tags: `程設筆記`
{%hackmd theme-dark %}
# Chapter 6 Arrays, Strings and Vectors
## Array
* If there are not enough initializer for a given array, the remaining elements will be initialized to 0.
```c++=
#include <iostream>
int main(){
    int a[]={0, 1, 2, 3, 4};
    // the compiler will automatically recognize it as a[4]
    int b[4]={0, 1};
    // b[0]=0, b[1]=1
    //b[2]~b[4]=0
    int c[4];
    //The value c[0]~c[4] will be random
int x;
cin>>x;
int d[x];
//It's not allowed
```
```c++=
#include <iostream>
int main(){
    int x{0};
    std::cin>>x;
    int a[x];
    for(int i=0; i<=x; i++){
        a[i]=i;
    }
    for(int j=0; j<=x; j++){
        std::cout<<a[j]<<" ";
    }
}
// It's allowed
```
## Using '==' with arrays is not allowed
* Using '==' to compare an array with another is not allowed, since the "array_name" is the address of the first element in array_name[];
## Input a array to a function
* Entire array -> passed by reference
* Individual array elements -> passed by value.
* By adding const in function, the entire array won't be changed.
```c++=
void function(int array_name[], array_size){
    ...
}
function(array_name, array_size);
```
example(1D by value):
```c++=
#include <iostream>
using namespace std;
void print_arr_1d(int n, int arr[]){
    for(int i=0; i<n; i++){
        cout<<arr[i]<<" ";
    }
}
int main(){
    int arr[5]={1, 2, 3, 4, 5};
    print_arr_1d(5, arr);
}
```
example(1D by reference with pointer):
```c++=
#include <iostream>
using namespace std;
void print_array_square_1d(int n, int *arr){
    for(int i=0; i<n; i++){
        *(arr+i)=*(arr+i)**(arr+i);
    }
    for(int i=0; i<n; i++){
        cout<<arr[i]<<" ";
    }
}
int main(){
    int arr[5]={1, 2, 3, 4, 5};
    print_array_square_1d(5, arr);
}

```
example (2D by value):
```c++=
#include <iostream>
using namespace std;
void print_array_2d(int r, int c, int arr[][2]){
    for(int i=0; i<r; i++){
        for(int j=0; j<c; j++){
            cout<<arr[i][j]<<" ";
        }
        cout<<"\n";
    }
}
int main(){
    int array[2][2]={{1, 2}, {3, 4}};
    print_array_2d(2, 2, array);
}  
```
example(2D by reference with pointer):
```c++=
#include <iostream>
using namespace std;
void print_array_square_2d(int *arr, int r, int c){
    for(int i=0; i<r; i++){
        for(int j=0; j<c; j++){
            *((arr+i*c)+j)*=*((arr+i*c)+j);
        }
    }
    for(int i=0; i<r; i++){
        for(int j=0; j<c; j++){
            cout<<*((arr+i*c)+j)<<" ";
        }
        cout<<"\n";
    }
}
int main(){
    int array[2][2]={{1, 2}, {3, 4}};
    print_array_square_2d(&array[0][0], 2, 2);
}  
```

## Multidimensional Array
![](https://i.imgur.com/0otlmaZ.png) 

* If there are not enough initializer for a given array, the remaining elements will be initialized to 0.
Ex:
```c++=
#include <iostream>
using namespace std;
int main(){
    int array[2][3]={{0, 1, 2}, {3, 4, 5}};
    for(int i=0; i<2; i++){
        for(int j=0; j<3; j++){
            cout<<array[i][j]<<" ";         //0 1 2
                                            //3 4 5
        }
        cout<<"\n";
    }
    int array2[2][3]={{0, 1}, {3}};
    for(int i=0; i<2; i++){
        for(int j=0; j<3; j++){
            cout<<array2[i][j]<<" ";    //0 1 0
                                        //3 0 0
        }
        cout<<"\n";
    }
}
//If the number isn't being input, the element will be 0.
```
* Passing a multidimensional array to a function
```c++=
    void function_2D(const int a[][nums]){
    ...
}
    void fucntion_3D(const int a[][nums][nums]){
    ...
}
```


## Calculating the address of an array.
* A 2D array with 6 elements, if the address of $a[0][0]$ is 100, what is the address of $a[1][1]$?
    * $a[1][1]$=116, since an address  will occupied 4 bytes.
* A 3D array with 24 elements, if the address of $b[0][0][0]$ is 200, waht is the address of $b[1][1][3]$ and $b[0][2][2]$?
    * $b[1][1][3]$=280
    * $b[0][2][2]$=244
## Char Array and String(C#)
Reference: https://shengyu7697.github.io/cpp-string-compare/
* Char array
    * One char is 1 byte.
    * Declaration:char char_name[arraysize];
* String
    * ＃include <cstring>
    * the size of the string is chars+1 bytes.
        * ex: dog => 4 bytes 
    * c_string must be ended with '\0'.
    * initialization:
        ```c++=
        #include <iostream>
        using namespace std;
        int main(){
            char a[10]={'H', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd'};    //by using {}, the size of the array must be word_amount;
            char b[11]="Helloworld";    //by using "", the size of a array must be word_amount+1;
            cout<<a<<" "<<b;
        }
        ```
    * cin.getline(char_name, char_length, end_condition)
        ```c++=
        #include <iostream>
        #include <cstring>
        using namespace std;
        int main(){
        char string[11];
        cin.getline(string, 11, '\n');
        cout<<string;
        }
        ```
    * '\0'!='0'.
    * One Chinese character = 2 bytes.
    * strcpy(str1, str2) => copy string2 to string 1.
    * strcat(str1, str2) => str1 + str2
    * strcmp(str1, str2) => compare str1 and str2.
         ex:
        ```c++=
        #include<iostream>
        #include <cstring>
        using namespace std;
        int main(){
            char a[50], b[50];
            strcpy(a, "Hi hello world.");
            strcpy(b, "Hi hello");
            cout<<strcat(a, b);
        }
        ```
## String (C++)
References:https://ithelp.ithome.com.tw/articles/10276673
* declaration:　
```c++=
#include <iostream>
#include <string>
int main(){
    string s="Hi";
    string s("Hi");
    //both of them are the same.
}    
```
* input variables
    * with getline(), we can input strings with spaces.
```c++=
#include <iostream>
using namespace std;
int main(){
    string s;
    getline(cin, s);
    cout<<s;
}
```
* Using '+' with strings is allowed
```c++=
#include <iostream>
using namespace std;
int main(){
    string a="Hello";
    string b="World";
    cout<<a+b; //cout HelloWorld
}
```
* Using "==" and "!=" to compare two strings is allowed
```c++=
#include <iostream>
using namespace std;
int main(){
    string s1, s2;
    getline(cin, s1);
    getline(cin, s2);
    if(s1==s2){
        cout<<"same";
    }
    else{
        cout<<"different";
    }
    
}
```
* Creating copy of strings
    * str1=str2 => str1(str2).
* at()
    * str[1]=> str$.$at(1).
* length()
    * Gives u the length of a string.
* empty()
    * return true if the string is empty.
* substr( )
    * return a specific part of a string.
    substr(a, b) => return the part of the string bewteen $[$ a, b $)$.
```c++=
#include <iostream>
#include <string>
using namespace std;
int main(){
    string test;
    test="Hello World";
    cout<<test.substr(0, 5);
}
```    
    
## Linear search
* To find the corresponding element of a value;
Example:
```c++=
#include <iostream>
#include <cmath>
void search(int a[], int value, int arraysize){
    int flag=0, element;
    for(int i=0; i<arraysize; i++){
        if(value==a[i]){
            flag=1;
            element=i; 
        }
    }
    if(flag==1){
        std::cout<<"The corresponding element of the value is: "<<element<<'\n';
    }
    else{
        std::cout<<"There is no corresponding element to your value.\n";
    }

}
int main(){
    int a[5]={1, 2, 3, 4, 5};
    int value;
    while(std::cin>>value){
        search(a, value, 5);
    }
}
```
## Bubble Sort
Reference:　https://shengyu7697.github.io/cpp-bubble-sort/
```c++=
#include <iostream>
using namespace std;
void swap(int &a, int &b){
    int temp=a;
    a=b;
    b=temp;
}
int main(){
    int a[5]={40, 30, 50, 60, 20};
    int len=sizeof(a)/sizeof(a[0]);
    for(int i=0; i<len-1; i++){
        for(int j=i+1; j<len; j++){
            if(a[i]>a[j]){
                swap(a[i], a[j]);
            }
        }
    }
    for(int i=0; i<len; i++){
        cout<<a[i]<<" ";
    }
}
```
## Vectors
https://hackmd.io/0Jtv1856SEeV5SwOfw9lag
