數(shù)組和字符串


title: c++之管理數(shù)組和字符串
tags:


數(shù)組和字符串


什么是數(shù)組

int number [5] = {0};
char number [5];
int number [5] = {100,12,15,48,6}
int number [5] = {};
int number [5] = {66,55};

int number [3] [3] = {{515,585,84},{},{45,77,24}} 三行三列

const int ARRAY_LENGTH = 5;
int number [ARRAY_LENGTH] = {45,458,48,45,8};
以上即為數(shù)組的聲明,上面全部是靜態(tài)數(shù)組,即在編譯階段就已經(jīng)確定了長度。
在執(zhí)行階段確定長度的數(shù)組為動態(tài)數(shù)組。

數(shù)組的預(yù)留空間大小=數(shù)組的個數(shù)x數(shù)組的類型的大小

訪問數(shù)組中的元素 N [n] 訪問的是n+1個元素,超過的補隨機(jī)數(shù)。

務(wù)必初始化數(shù)組,否則將包含未知數(shù),保證在邊界內(nèi)索引。

使用std::vector初始化動態(tài)數(shù)組 std::vector<int>dynArray (3);

C++字符串 使用std::string 初始化字符串、存儲用戶輸入、復(fù)制和拼接字符串以及確定字符串長度

應(yīng)例

0: #include <iostream>
1: #include <string>
2:
3: using namespace std;
5: int main()
6: {
7: string greetString ("Hello std::string!");
8: cout << greetString << endl;
9:
10: cout << "Enter a line of text: " << endl;
11: string firstLine;
12: getline(cin, firstLine);
13:
14: cout << "Enter another: " << endl;
15: string secondLine;
16: getline(cin, secondLine);
17:
18: cout << "Result of concatenation: " << endl;
19: string concatString = firstLine + " " + secondLine;
20: cout << concatString << endl;
21:
22: cout << "Copy of concatenated string: " << endl;
23: string aCopy;
24: aCopy = concatString;
25: cout << aCopy << endl;
26:
27:
cout << "Length of concat string: " << concatString.length() << endl;
28:
29:
return 0;
30: }

image.png

文章依據(jù)21天學(xué)通C++第八版,純屬小白自學(xué)?。。。?/p>

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容