字串&指標

String&Pointer

-31教學長林芊妘

字串

String

#include <iostream>
using namespace std;
int main()
{
    string str1;
    str1="IZCC";
    return 0;
}

string 變數名稱

變數名稱="[文字]"

#include <iostream>
using namespace std;
int main()
{
    string str1,str2;
    str1="IZCC";
    str2=" = INFOR x ZSISC x CKCSC x CMIOC";
    cout<<str1[1]<<endl;
    cout<<str1+str2<<endl;
    str1[3]='2';
    cout<<str1<<endl;
    str1="ZSGH";
    cout<<str1<<endl;
    return 0;
}

Z

IZCC = INFOR x ZSISC x CKCSC x CMIOC

IZC2

ZSGH

#include <iostream>
using namespace std;
int main()
{
    string str1,str2;
    str1="IZCC";
    str2=" = INFOR x ZSISC x CKCSC x CMIOC";
    cout<<str1[1]<<endl;
    cout<<str1+str2<<endl;
    str1[3]='2';
    cout<<str1<<endl;
    str1="ZSGH";
    cout<<str1<<endl;
    return 0;
}

Z

IZCC = INFOR x ZSISC x CKCSC x CMIOC

IZC2

ZSGH

1. cout<<str1[1]<<endl;

str1="IZCC"

[0] [1] [2] [3]
I Z C C
#include <iostream>
using namespace std;
int main()
{
    string str1,str2;
    str1="IZCC";
    str2=" = INFOR x ZSISC x CKCSC x CMIOC";
    cout<<str1[1]<<endl;
    cout<<str1+str2<<endl;
    str1[3]='2';
    cout<<str1<<endl;
    str1="ZSGH";
    cout<<str1<<endl;
    return 0;
}

Z

IZCC = INFOR x ZSISC x CKCSC x CMIOC

IZC2

ZSGH

2. cout<<str1+str2<<endl;

str1="IZCC", str2=" = INFOR x ZSISC x CKCSC x CMIOC"

*字串可相加

IZCC = INFOR x ZSISC x CKCSC x CMIOC

#include <iostream>
using namespace std;
int main()
{
    string str1,str2;
    str1="IZCC";
    str2=" = INFOR x ZSISC x CKCSC x CMIOC";
    cout<<str1[1]<<endl;
    cout<<str1+str2<<endl;
    str1[3]='2';
    cout<<str1<<endl;
    str1="ZSGH";
    cout<<str1<<endl;
    return 0;
}

Z

IZCC = INFOR x ZSISC x CKCSC x CMIOC

IZC2

ZSGH

3. cout<<str1<<endl;

str1="IZCC"

[0] [1] [2] [3]
I Z C C

2

**字串位置指的是字元,因此要使用單引號' '

#include <iostream>
using namespace std;
int main()
{
    string str1,str2;
    str1="IZCC";
    str2=" = INFOR x ZSISC x CKCSC x CMIOC";
    cout<<str1[1]<<endl;
    cout<<str1+str2<<endl;
    str1[3]='2';
    cout<<str1<<endl;
    str1="ZSGH";
    cout<<str1<<endl;
    return 0;
}

Z

IZCC = INFOR x ZSISC x CKCSC x CMIOC

IZC2

ZSGH

3. cout<<str1<<endl;

str1="IZC2"

**字串位置指的是字元,因此要使用單引號' '

#include <iostream>
using namespace std;
int main()
{
    string str1,str2;
    str1="ZSISC";
    char _1st=str1[0], _3rd=str1[2];
    cout<<_1st<<" "<<_3rd;
    return 0;
}

Z I

#include <iostream>
using namespace std;
int main()
{
    string str1,str2;
    str1="IZCC";
    str2=" = INFOR x ZSISC x CKCSC x CMIOC";
    cout<<str1[1]<<endl;
    cout<<str1+str2<<endl;
    str1[3]='2';
    cout<<str1<<endl;
    str1="ZSGH";
    cout<<str1<<endl;
    return 0;
}

Z

IZCC = INFOR x ZSISC x CKCSC x CMIOC

IZC2

ZSGH

4. cout<<str1<<endl;

str1="IZC2"

str1="ZSGH"

#include <iostream>
using namespace std;
int main()
{
    string str1;
    string str2="love";
    if(str1.empty()==0) cout<<"not empty"<<endl;
    else cout<<"empty"<<endl;
    if(str2.empty()==0) cout<<"not empty"<<endl;
    else cout<<"empty"<<endl;
    cout<<str1.empty()<<" "<<str2.empty();
    return 0;
}

empty

not empty

1 0

**空字串回傳1 非空字串回傳0

利用[變數名稱].empty()得知字串是否有賦值

#include <iostream>
using namespace std;
int main()
{
    string str1="kawaii";
    string str2="suki";
    cout<<str1.size()<<" "<<str2.length()<<endl;
    return 0;
}

6 4

利用[變數名稱].size()/.length()得知字串長度

#include <iostream>
using namespace std;
int main()
{
    string mother="0229";
    string father="0229";
    if(mother==father) cout<<"same"<<endl;
    else cout<<"nice"<<endl;
    return 0;
}

same

利用變數相等得知字串內容是否相等

getline 用法

#include <iostream>
using namespace std;
int main()
{
    string name;
    while(getline(cin,name))
        cout<<"Happy birthday to "<<name<<"!"<<endl;
}

輸入

TFG

Taipei First Girl

輸出

Happy birthday to TFG!

Happy birthday to Taipei First Girl!

Try Try See

#include <iostream>
using namespace std;
int main()
{
    string name;
    while(cin>>name)
        cout<<"Happy birthday to "<<name<<"!"<<endl;
}

輸入

TFG

Taipei First Girl

輸出

把getline(cin,name)改成cin>>name

指標

Pointer

是否曾經出現過??

0x7fff544e3120 是啥?

變數位址

變數位址

變數位址指的是變數在電腦中存放的位置

也就是指向該變數的指標!

a

取址運算子

1. 設定變數

2. 獲得變數在電腦中的位置

當用法如下時

int *a;

*表示*a為指標,儲存內容為一個記憶體位址

輸出可發現 兩者位址相同!!

p=5 對應到的

位址為0x77ffd0db38ufc

*a為指標變數

用來儲存位址

&p為p的位址

a儲存p的指標

當用法如下時

cout<<*a;

*為取址運算子,用來取出指標所指向的位址的值

p=5 對應到的

位址為0x77ffd0db38ufc

*a為指標變數

用來儲存位址

&p為p的位址

a儲存p的指標

*a取出儲存的指標指向的值

若輸出*a,則是5;

若輸出a,則是位址

公告

我們將於下週社課12/19(四)頒發作業獎品!!!

分數會以ZREROJUDGE上的表現計分

(是很好的獎品啊~大家努力爭取🔥)

字串&指標

By chainy

字串&指標

  • 229