C++
一. 字符串
c++提供了两种类型的字符串
1.c风格字符串
2.c++引入的string类型
c风格字符串
字符串实际上就是使用null字符 \0终止的一维字符数组
int main(){
char greeting[] = {'h', 'e', 'l', 'l', '0', '\0'};
可以简写成:
char greeting2[] = "hello"; // 最后会跟着一个\0的空字符,此时如果括号中写长度,必须大于等于6
}
c++字符串
c++标准库提供了string类型,需要引入#include,由于string类声明在命名空间std中,使用时要记得加上
#include<iostream>
#include<string>
using namespace std;
int main(){
std::string name = "你好";
string num {};
// 字符串拼接
string name1 = "xiaohong";
cout << name + name1 << endl;
// 获取指定位置字符
cout << name1[0] << endl;
cout << name1.at(2) << endl;
// 字符串遍历
for (char i:name1){
cout << i << endl;
}
// 截取字符串
substr(开始位置,截取长度)
cout << name1.substr(3,3) << endl;
// 获取字符在字符串中的索引
cout << name1.find("hong") << endl;
// 获取字符串长度
cout << name1.length() << endl;
cout << name1.size() << endl;
}
二. Vector
Vector很大程度上和数组一样,只是数组是固定长度,而vector是不定长度(动态增长)。
#include<vector>
using namespace std
int main(){
vector<char> num = {'a', 'b', 'b', 'd'};
cout << num[4] << endl; // 不会检查越界
cout << num.at(4) << endl; // 抛出越界异常
// 增加
num.push_back(666);
// 修改
num.at(4) = 6;
num[0] = 6;
//查询
for (int i = 0; i < num.size(); ++i) {
cout << num[i] << endl;
}
for (int s:num) {
cout << s << endl;
}
}
二维Vector
#include <iostream>
#include <vecotr>
using namespace std;
int main(){
//声明并初始化vector
vector<vector<int>> scores {
{95,77,80,85},
{58,89,93,100},
{69,73,81,97}
};
for (int i = 0; i < scores.size(); ++i) {
for (int j = 0; j < scores[i].size(); ++j) {
cout << scores[i][j] <<"\t" ;
}
cout << endl;
}
return 0 ;
}
三. 函数
定义函数
函数的定义一般包含四个部分:方法名称、方法参数、方法体、返回值
#include<iostream>
void sayHello(){
std::cout << "hello" << std::endl;
}
int main(){
sayHello();
}
函数类型
无参数无返回值、无参数有返回值、有参数无返回值、有参数有返回值
函数原型
把函数分成声明和定义两部分,函数的原型定义在调用的前面,具体实现可以放在后面。
#include <iostream>
using namespace std;
//函数声明 ,也叫函数原型 并不知道这个函数具体是如何实现的。只是有一些基本架子而已。
int add (int a , int b);
int main(){
cout << add(1 ,2)<< endl;
return 0 ;
}
//函数定义 ,函数的真正实现。
int add(int a , int b){
return a + b ;
}
分离式编译
一般说来,函数的声明 ( 函数原型 )通常都会放到头文件中,之所以称之为头文件是因为它总是在main函数的前面就引入进来。头文件一般以 .h 或者 .hpp 结尾,通常用于 写类的声明(包括类里面的成员和方法的声明)、函数原型、#define常数等,但一般来说不写出具体的实现
- calc.h
为了能够让声明和定义能够快速的被关联上,通常它们的名称会被定义成一样的,这已经成为了一种默认的规定
//函数声明
int add (int a , int b);
- calc.cpp
#include "math.h"
//函数定义 ,函数的真正实现。
int add(int a , int b){
return a + b ;
}
- main.cpp
#include <iostream>
#include "math.h" //这里使用"" 表示从当前目录查找
int main(){
add(1 ,2);
return 0 ;
}
函数重载
两个或者两个以上的函数名称是一样的,当然他们的 参数个数 或者 参数类型 或者是 参数的顺序 是不一样的。这种现象有一个学名叫做 重载
函数参数
值传递
#include<iostream>
using namespace std;
void scale_number(int num);
int main(){
int number{1000};
scale_number(number);
//打印number 1000
cout << number <endl;
return 0 ;
}
void scale_number(int num){
if(num > 100)
num = 100;
}
传递数组
函数的参数除了能传递普通简单的数据之外,数组也是可以传递的。但是数组稍微有点特殊,这里多做讲解。
- 前面提过,形参实际上就是实参的一份拷贝,就是一个局部变量。
- 数组的数据太大,如果都进行拷贝,那么比较麻烦,也造成了浪费
- 所以实际上传递数组的时候,并不会进行整个数组的拷贝,而只是传递数组的第一个元素内存地址 (指针 ) 进来。
- 数组的数据还是在内存中,只是把第一个元素(也就是数组的起始)内存地址传进来而已。
- 这就造成了函数的内部根本无法知道这个数组的元素有多少个。
传递引用
目前为止,我们所有函数的参数传递,都是对数据进行了一份拷贝(数组除外)。那么在函数的内部是不能修改值的,因为这仅仅是一份值得拷贝而已(函数外部的值并不会受到影响)。如果真的想在函数内部修改值,那么除了数组之外,还有一种方式就是传递引用。
引用实际上只是原有数据的一种别名称呼而已,使用 & 定义
#include<iostream>
using namespace std;
void scale_number(int &num);
int main(){
int number{1000};
scale_number(number);
//打印number100
cout << number <<endl;
return 0 ;
}
void scale_number(int &num){
if(num > 100)
num = 100;
}