title: c++之管理數(shù)組和字符串
tags:
- 語言工具
-c++
categories: c++
date: 2019-02-19
thumbnail: https://upload-images.jianshu.io/upload_images/16271012-4900bfe438f23d6c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240
數(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

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