> 文章列表 > 【C++学习笔记】字符串、向量和数组

【C++学习笔记】字符串、向量和数组

【C++学习笔记】字符串、向量和数组

字符串类型

1.C语言风格字符串:char 变量名[] = "字符串值"
1. char: 字符常量或者单个字符 单引号定义字符串常量用 双引号 定义
2. 输出直接用cout

char str1[] = "hello world";
cout << str1 << endl;

2.C++语言风格字符串:string 变量名 = "字符串值"
**输出要包含头文件 #include<string> **

#include<string>
// 输出要包含头文件 
string str3 = "hello world";
cout << str3 << endl;

初始化string 对象的方式
在这里插入图片描述
在这里插入图片描述

Vector

vector是模板 不是类型
初始化vector 方法:在这里插入图片描述
初始化vector时,注意区分花括号和圆括号。
圆括号提供的值为构造vector对象的容量和值
花括号提供的值为vector 的列表初始化

vector<int> v1(10) // v1有10个int元素,每个值为0
vector<int> v2{10} // v2有1个int元素,值为10
vector<int> v3(10, 1) // v3有10个元素,值均为1
vector<int> v4{10, 1} // v4有2个元素,值为10和1

!!!在这里插入图片描述

C风格字符串

c++的字符串头文件是 string 对应string
c风格字符串头文件是 cstring对应char

string转换为char 函数 c_str(), 该函数的返回类型为const *char类型,定义时要注意

string s = "hahah haha";
const char *ss = s.c_str();
cout << s << endl;
cout << *ss << endl;

需要注意
在这里插入图片描述在这里插入图片描述在这里插入图片描述
在这里插入图片描述

const char a[] = {'h', 'e', 'l', 'l', 'o'};
const char *p = a;
while(*p)
{cout << *p << endl;++p;
}

输出结果: 以列表初始化方式赋值的C风格字符串与以字符串字面值赋值的区别,以字符串字面值赋值的字符串在最后会额外添加一个空字符表示字符串的结束,而前者不会。所以,当遍历完a中的5个字符后,没有遇到空字符,无法跳出while循环,会继续运行直到找到空字符。
!!! char 输出时出现乱码,大概率是因为 在初始化char时,没有添加空白符’\\n’,发生了越界。

h
e
l
l
o<

若想达到原有效果代码:

const char a[] = {'h', 'e', 'l', 'l', 'o', '\\0'};
const char *p = a;
while(*p)
{cout << *p << endl;++p;
}

输出结果:

h
e
l
l
o

对于vector、数组指针相加合法但是无意义的原因: 指针也是对象,其有三个属性,指针本身的值value、指针所指对象content及指针本身在内存中的存储位置address。
指针本身的值value:是内存地址的值,表示所指对象在内存中的存储地址
指针所指对象content:指针指向的对象
指针本身在内存中的存储位置address:指针本身也在内存中的某个位置,也有自己的地址

指针相加时,是指将两个指针所指对象的地址加在一起,合法但是无意义;而相减是指两个对象之间的地址差代表指针所指同一数组或vector的两个不同元素之间的距离。

利用别名遍历多维数组
设数组a[3][4]为三行四列的数组,可以将数组的一行,即将一个含有4个元素的数组,定义为一个别名类型,如using int_arr = int[4]

#include<iostream>
using namespace std;
using int_arr = int[4]; // 别名简化
int main()
{int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};cout << "范围for语句:" << endl;for(int (&row)[4] : a){for(int &col : row){cout << col << " " ;}cout << endl;}cout << "下标for:" << endl;for(int i = 0; i < 3; i++){for(int j = 0; j < 4; j++){cout << a[i][j]<<  " ";;}cout << endl;}cout << "指针for:" << endl;for(int (*p)[4] = a; p != a + 3; p++){for(int *q = *p; q != *p + 4; q++){cout << *q <<  " ";}cout << endl;}//利用别名cout << "利用别名范围for语句:" << endl;for(int_arr &row : a){for(int &col : row){cout << col << " " ;}cout << endl;}cout << "下标for:" << endl;for(int i = 0; i < 3; i++){for(int j = 0; j < 4; j++){cout << a[i][j]<<  " ";}cout << endl;}cout << "指针for:" << endl;for(int_arr (*p) = a; p != a + 3; p++){for(int *q = *p; q != *p + 4; q++){cout << *q <<  " ";}cout << endl;}return 0;}

数组

数组的以指针类型的形式进行调用 ^6htdd4

一维数组定义

  1. 数据类型 数组名[数组长度];
  2. 数据类型 数组名[数组长度] = {值1, 值2, ...}; 若初始化时值的个数 ≠ 数组长度,则会用0补全
  3. 数据类型 数组名[ ] = {值1, 值2, ...}
#include<iostream>
using namespace std;int main()
{// 定义1 数据类型 数组名[数组长度];int arr1[5]; // 声明数组// 赋值 进行初始化arr1[0] = 1;arr1[1] = 2;arr1[2] = 3;arr1[3] = 4;arr1[4] = 5;for(int i = 0; i < 5; ++i){cout << arr1[i] << endl; // 输出数组1}// 定义2 数据类型 数组名[数组长度] = {值1, 值2, ...}  // 若初始化时值的个数 ≠ 数组长度,则会用0补全int arr2[5] = {6, 7, 8};for(int i = 0; i < 5; ++i){cout << arr2[i] << endl; // 输出数组2}// 定义3 数据类型 数组名[ ] = {值1, 值2, ...}int arr3[ ] = {11, 12, 13, 14, 15};for(int i = 0; i < 5; ++i){cout << arr3[i] << endl; // 输出数组3}return 0;
}

数组的特点

  1. 放在一块连续的内存空间中
  2. 数组中每个元素数据类型相同

数组名作用

  1. 统计整个数组在内存中的长度 sizeof(数组名)
  2. 获取数组在内存中的**[[三、字符串、向量和数组#^6htdd4|首地址]]**(直接输出数组名),默认为十六进制,在前面加(int)可以转为十进制 cout << 数组名 << endl; ^ah4z70

二维数组定义

  1. 数据类型 数组名[行数][列数];
  2. 数据类型 数组名[行数][列数] = {{值1, 值2,...},{值1, 值2,...}};
  3. 数据类型 数组名[行数][列数] = {值1, 值2, 值3, 值4,...};
  4. 数据类型 数组名[ ][列数] = {值1, 值2, 值3, 值4,...};
#include<iostream>using namespace std;int main()
{// 定义方式 1int arr[2][3];arr[0][0] = 1;arr[0][1] = 2;arr[0][2] = 3;arr[1][0] = 4;arr[1][1] = 5;arr[1][2] = 6;  for(int i = 0; i < 2; ++i){for(int j = 0; j < 3; ++j){cout << arr[i][j] << " ";}cout << endl;}cout << endl;  // 定义方式2 没有值的位置用0 补充int arr2[2][3] = {{1,2 },{4,5,6}};for(int i = 0; i < 2; ++i){for(int j = 0; j < 3; ++j){cout << arr2[i][j] << " ";}cout << endl;}cout << endl;//定义方式3 没有值的位置用0 补充int arr3[2][3] = {1, 2, 3};for(int i = 0; i < 2; ++i){for(int j = 0; j < 3; ++j){cout << arr3[i][j] << " ";}cout << endl;}cout << endl;//定义方式4int arr4[ ][3] = {1,2,3,4};for(int i = 0; i < 2; ++i){for(int j = 0; j < 3; ++j){cout << arr4[i][j] << " ";}cout << endl;}cout << endl;return 0;
}