C++基礎(chǔ)容器
序列型容器
數(shù)組
-
概念
- 代表內(nèi)存里一組連續(xù)的同類型存儲(chǔ)區(qū)
- 可以用來把多個(gè)存儲(chǔ)區(qū)合并成一個(gè)整體
-
數(shù)組聲明
- int arr[10];
- 類型名稱int表述數(shù)組里面所有元素的類型
- 名稱arr是數(shù)組的名稱
- 整數(shù)10表示數(shù)組里面的元素個(gè)數(shù)
- 數(shù)組里元素個(gè)數(shù)不可以改變
-
使用
- 每個(gè)元素都有下標(biāo),通過下標(biāo)可以直接訪問任意一個(gè)元素
- 下標(biāo)從0開始到元素個(gè)數(shù)減一為止
- 超過范圍的下標(biāo)不可以使用
- 數(shù)組名稱和下標(biāo)一起可以表述數(shù)組里面的元素。arr[4]
-
優(yōu)點(diǎn)
- 可以編寫循環(huán)依次處理數(shù)組里面所有的元素
- 循環(huán)變量可以依次代表所有有效下標(biāo)
- 下標(biāo)標(biāo)識了一個(gè)元素在數(shù)組的位置
off-by-one error(差一錯(cuò)誤)
- 考慮問題原則
- 首先考慮最簡單的情況的特例,然后將結(jié)果外推
- 仔細(xì)計(jì)算邊界
- 一般使用左閉右開的區(qū)間來表示范圍
C語言中設(shè)計(jì)數(shù)組下標(biāo)的原則:從0開始使用非堆成區(qū)間;
- 讓這個(gè)區(qū)間是非對稱區(qū)間
- 讓下界可以取到,讓上界取不到
這樣設(shè)計(jì)的好處:
- 取值范圍:下界到上界
- 如果這個(gè)取值范圍為空,上界值==下界值
- 即使取值范圍為空,上界值永遠(yuǎn)不可能小于下界值
數(shù)組的增刪改查
-
在尾部添加和刪除時(shí)間復(fù)雜度為O(1);
-
不在尾部添加和刪除時(shí)間復(fù)雜度為O(n);
-
數(shù)組遍歷高效,時(shí)間復(fù)雜度為O(1);
-
數(shù)組查找的時(shí)間復(fù)雜度為O(n),取決于數(shù)組容量;
二維數(shù)組的訪問
- 在一個(gè)小的時(shí)間窗內(nèi)訪問的變量地址越接近越好,這樣執(zhí)行速度快;
- 也就是說一般情況下需要將長循環(huán)放在內(nèi)層,最短的循環(huán)放在外層以減少cpu跨切循環(huán)層的次數(shù);
代碼展示:
#include <iostream>
using namespace std;
#include <vector>
int main()
{
// 數(shù)組訪問
int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 0, 0};
// 推薦方式 ++ 寫在前面避免拷貝構(gòu)造 提速
for (int index = 0; index < 10; ++index)
cout << a[index] << " ";
cout << endl;
// 不推薦方式
for (int index = 0; index <= 9; ++index)
cout << a[index] << " ";
// 數(shù)組的查找
int b[] = { 1, 2, 3, 4 };
int len = sizeof(b) / sizeof(b[0]);//得到數(shù)組容量
for (int index = 0; index < len; ++index)
{
if (b[index] == 5)
{
cout << index << endl;
return 0;
}
}
//二維數(shù)組的訪問:
int c[2][4] = { { 1, 2, 3, 4 },{ 5,6,7,8 } };
for (int row = 0; row < 2; ++row)
{
for (int col = 0; col < 4; ++col)
{
cout << c[row][col] << " ";
}
cout << endl;
}
return 0;
}
動(dòng)態(tài)數(shù)組 std::vector
vector是面向?qū)ο蟮膭?dòng)態(tài)數(shù)組 – 使用簡單的數(shù)組是無法動(dòng)態(tài)擴(kuò)容插入元素,因?yàn)槿萘坑邢?/p>
vector插入操作
-
vec.insert(–vec.end(), 6);
-
vec.push_back(5);
vector刪除操作
- vec.pop_back();
- vec.erase(vec.end() - 2);
示例代碼:文章來源地址http://www.zghlxwxcb.cn/news/detail-458600.html
#include <iostream>
#include <vector>
using namespace std;
int main()
{
// 創(chuàng)建動(dòng)態(tài)數(shù)組vector
vector<int> vec = { 1,2,3,4 };
cout << "size is " << vec.size() << endl;
cout << "capacity is " << vec.capacity() << endl;
// 遍歷所有元素
for (int index = 0; index < vec.size(); ++index)
{
cout << vec[index] << endl;
}
// 在尾部插入一個(gè)元素5
vec.push_back(5);
cout << "size is " << vec.size() << endl;
cout << "capacity is " << vec.capacity() << endl;
// 遍歷所有元素
for (int index = 0; index < vec.size(); ++index)
{
cout << vec[index] << endl;
}
// 在中間插入一個(gè)元素6
vec.insert(--vec.end(), 6);
cout << "size is " << vec.size() << endl;
cout << "capacity is " << vec.capacity() << endl;
// 遍歷所有元素
for (int index = 0; index < vec.size(); ++index)
{
cout << vec[index] << endl;
}
// 在尾部移除一個(gè)元素
vec.pop_back();
cout << "size is " << vec.size() << endl;
cout << "capacity is " << vec.capacity() << endl;
// 遍歷所有元素
for (int index = 0; index < vec.size(); ++index)
{
cout << vec[index] << endl;
}
// 在任意位置移除一個(gè)元素
vec.erase(vec.end() - 2);
cout << "size is " << vec.size() << endl;
cout << "capacity is " << vec.capacity() << endl;
// 遍歷所有元素
for (int index = 0; index < vec.size(); ++index)
{
cout << vec[index] << endl;
}
return 0;
}
字符串和字符數(shù)組
-
字符串變量
- 字符串是以空字符{‘\0’}結(jié)束的字符數(shù)組
- 空字符’\0’自動(dòng)添加到字符串的內(nèi)部表示中
- 在聲明字符串變量時(shí),應(yīng)該為這個(gè)空結(jié)束符預(yù)留一個(gè)額外的元素空間 char str[11] = {“helloworld”};
-
字符串常量
- 字符串常量是一對雙引號括起來的字符序列
- 字符串中每個(gè)字符作為一個(gè)數(shù)組元素存儲(chǔ) “helloworld”
ASCII碼一覽表,ASCII碼對照表 (biancheng.net)
Unicode編碼
-
最初目的是把世界上的文字都映射到一套字符空間中
-
為了表示Unicode字符集,有五種Unicode編碼方式,這里說3中
- utf-8 : 1byte 表示,可以兼容ascii
- 存儲(chǔ)效率高,變長,無字節(jié)序問題
- utf-16
- 特點(diǎn)是定長,有字節(jié)序的問題(不可作為外部編碼)
- utf-32
- 特點(diǎn)是定長,有字節(jié)序問題(不可作為外部編碼)
- utf-8 : 1byte 表示,可以兼容ascii
-
編碼錯(cuò)誤的根本原因是在于編碼方式和解碼方式的不統(tǒng)一
windows文件可能有Bom 如果在其他平臺可以去掉bom
字符串的指針表示方法
- 指針表示方法 – char* pStrHelloWrold = “helloworld”;
字符串的常見操作
-
字符串長度:strlen(s);
-
字符串比較:strcmp(s1, s2);
-
字符串拷貝:strcpy(s1, s2); 復(fù)制s2到s1s
-
復(fù)制指定長度字符串:strncpy(s1, s2, n);
-
字符串拼接:strcat(s1, s2);
-
查找字符:strchr(s1, ch);
-
查找字符串:strstr(s1, s2);
字符串操作的問題
- C中原始字符串操作在安全性和效率存在一定的問題
- 緩沖區(qū)溢出問題
- strlen的效率可以提升:空間換時(shí)間
#include <string.h>
#include <iostream>
using namespace std;
int main()
{
// 定義一個(gè)數(shù)組
char strHelloWorld[11] = { "helloworld" }; // 這個(gè)定義可以
char* pStrHelloWrold = "helloworld";
pStrHelloWrold = strHelloWorld;
//strHelloWorld = pStrHelloWrold; // 數(shù)組變量的值不允許改變
// 字符0, '\0', '0'的區(qū)別
char c1 = 0;
char c2 = '\0';
char c3 = '0';
// 通過數(shù)組變量遍歷修改數(shù)組中的元素值
for (int index = 0; index < strlen(strHelloWorld); ++index)
{
strHelloWorld[index] += 1;
std::cout << strHelloWorld[index] << std::endl;
}
// 通過指針變量遍歷修改數(shù)組中的元素值
for (int index = 0; index < strlen(strHelloWorld); ++index)
{
pStrHelloWrold[index] += 1;
std::cout << pStrHelloWrold[index] << std::endl;
}
cout << endl; // 換行
// 計(jì)算字符串長度
cout << "字符串長度為: " << strlen(strHelloWorld) << endl;
cout << "字符串占用空間為: " << sizeof(strHelloWorld) << endl;
return 0;
}
#include <string.h> //使用C庫的頭文件
#include <iostream>
using namespace std;
const unsigned int MAX_LEN_NUM = 16;
const unsigned int STR_LEN_NUM = 7;
const unsigned int NUM_TO_COPY = 2;
int main()
{
char strHelloWorld1[ ] = { "hello" };
char strHelloWorld2[STR_LEN_NUM] = { "world1" };
char strHelloWorld3[MAX_LEN_NUM] = {0};
//strcpy(strHelloWorld3, strHelloWorld1); // hello
strcpy_s(strHelloWorld3, MAX_LEN_NUM, strHelloWorld1);
//strncpy(strHelloWorld3, strHelloWorld2, NUM_TO_COPY); // wollo
strncpy_s(strHelloWorld3, MAX_LEN_NUM, strHelloWorld2, NUM_TO_COPY);
//strcat(strHelloWorld3, strHelloWorld2); // wolloworld1
strcat_s(strHelloWorld3, MAX_LEN_NUM, strHelloWorld2);
//unsigned int len = strlen(strHelloWorld3);
unsigned int len = strnlen_s(strHelloWorld3, MAX_LEN_NUM);
for (unsigned int index = 0; index < len; ++index)
{
cout << strHelloWorld3[index] << " ";
}
cout << endl;
// 小心緩沖區(qū)溢出
//strcat(strHelloWorld2, "Welcome to C++");
strcat_s(strHelloWorld2, STR_LEN_NUM, "Welcome to C++");
return 0;
}
C++中的std::string
- C++中提供了string類型專門表示字符串
- 使用string可以更加安全方便的管理字符串
使用起來比原始的C風(fēng)格的方法更安全和方便,對性能要求不是特別高的常見可以使用。文章來源:http://www.zghlxwxcb.cn/news/detail-458600.html
示例代碼:
#include <iostream>
#include <string>
using namespace std;
int main()
{
// 字符串定義
string s1;//定義空字符串
string s2 = "helloworld";//定義并初始化
string s3("helloworld");
string s4 = string("helloworld");
// 獲取字符串長度
cout << s1.length() << endl;
cout << s1.size() << endl;
cout << s1.capacity() << endl;
// 字符串比較
s1 = "hello", s2 = "world";
cout << (s1 == s2) << endl;
cout << (s1 != s2) << endl;
// 轉(zhuǎn)換成C風(fēng)格的字符串
const char *c_str1 = s1.c_str();
cout << "The C-style string c_str1 is: " << c_str1 << endl;
// 隨機(jī)訪問
for (unsigned int index = 0; index < s1.length(); ++index)
{
cout << c_str1[index] << " ";
}
cout << endl;
for (unsigned int index = 0; index < s1.length(); ++index)
{
cout << s1[index] << " ";
}
cout << endl;
// 字符串拷貝
s1 = "helloworld";
s2 = s1;
// 字符串連接
s1 = "helllo", s2 = "world";
s3 = s1 + s2; //s3: helloworld
s1 += s2; //s1: helloworld
return 0;
}
到了這里,關(guān)于C++基礎(chǔ)容器 -- C的數(shù)組和字符串和C++的數(shù)組和字符串的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!