string
单个变量的赋值及初始化
#include <iostream>
using namespace std;
int main()
{
string s1; //定义了一个字符串类型 (无参构造)
cout<<"s1 = "<<s1<<endl; //空串
cout<<"sizeof s1 = "<<sizeof (s1)<<endl; //32字节
//给一个字符串赋值
s1 = "hello world"; //等号运算符重载函数
cout<<"s1 = "<<s1<<endl;
cout<<"sizeof s1 = "<<sizeof (s1)<<endl; //32字节
//定义一个字符串变量,并进行初始化
string s2 = "ni hao";
cout<<"s2 = "<<s2<<endl;
//定义一个变量,并初始化
string s3("i love China");
cout<<"s3 = "<<s3<<endl;
//定义一个变量并初始化
string s4{"shang hai"};
cout<<"s4 = "<<s4<<endl;
//定义一个变量,使用多个连续字符初始化
string s5(5,'k');
cout<<"s5 = "<<s5<<endl;
return 0;
}
多个变量的初始化和赋值
#include <iostream>
using namespace std;
int main()
{
string s1 = "hello world";
string s2 = s1; //使用s1给s2进行初始化
cout<<"s2 = "<<s2<<endl;
string s3(s1); //使用s1给s3进行初始化
cout<<"s3 = "<<s3<<endl;
string s4; //没有初始化
s4 = s1; //两个字符串变量可以直接相互赋值,无需使用strcpy
cout<<"s4 = "<<s4<<endl;
string s5 = string("ni hao"); //使用匿名对象给一个字符串赋值
cout<<"s5 = "<<s5<<endl;
string s6 = s1+s5; //将s5连接到s1后,将结果赋值给s6,相当于strcat功能
cout<<"s6 = "<<s6<<endl;
string s7 = s1 + "lalalalalala"; //将字符串变量与字符串连接
cout<<"s7 = "<<s7<<endl;
string s8 = "wo ai" + s5; //将字符串与字符串变量连接
cout<<"s8 = "<<s8<<endl;
//string s9 = "i love" + "China"; //不支持
string s9 = string("i love") + "China";
cout<<"s9 = "<<s9<<endl;
s9 += "lalalala"; //使用+=运算符
cout<<"s9 = "<<s9<<endl;
return 0;
}
C风格和C++风格的字符串互换
#include <iostream>
#include<cstring>
using namespace std;
int main()
{
string s1 = "hello world"; //C++风格字符串
char s2[20] = "ni hao"; //C风格字符串
s1 = s2; //C风格字符串可以直接转换为C++风格字符串
cout<<"s1 = "<<s1<<" s2 = "<<s2<<endl;
s1 = "i love China";
//s2 = s1; //C++风格字符串不能直接转换为C风格字符串
//strcpy(s2, s1); //C++风格字符串不能直接转换为C风格字符串
//strcpy(s2, s1.c_str()); //可以使用成员函数c_str函数得到C++风格字符串的数据起始地址
strcpy(s2, s1.data()); //可以使用成员函数c_str函数得到C++风格字符串的数据起始地址
cout<<"s2 = "<<s2<<endl;
return 0;
}
string类中几个重要函数
#include <iostream>
using namespace std;
int main()
{
string s1 = "hello world";
cout<<"s1的长度为:"<<s1.size()<<endl; //11
cout<<"s1的长度为:"<<s1.length()<<endl; //11
cout<<"s1当前的最大容量:"<<s1.capacity()<<endl; //15
s1 += "kkkkk";
cout<<"s1的长度为:"<<s1.length()<<endl;
cout<<"s1当前的最大容量:"<<s1.capacity()<<endl; //30
//将字符串清空
s1.clear();
cout<<"s1的长度为:"<<s1.length()<<endl; //0
cout<<"s1当前的最大容量:"<<s1.capacity()<<endl; //30
//empty函数,是判断该字符串是否为空
if(s1.empty())
{
cout<<"字符串为空"<<endl;
}else
{
cout<<"字符串非空"<<endl;
}
//string类型指针访问成员
string *p = &s1;
p->push_back('h');
p->push_back('e');
p->push_back('l');
p->push_back('l');
p->push_back('o');
cout<<"*p = "<<*p<<endl;
return 0;
}
string类型的比较
#include <iostream>
#include<cstring>
using namespace std;
int main()
{
string s1 = "hello";
string s2 = "world";
//使用C风格字符串比较
if(strcmp(s1.data(), s2.data()) >0)
{
cout<<"s1 大"<<endl;
}else if(strcmp(s1.data(), s2.data()) <0)
{
cout<<"s1 小"<<endl;
}else
{
cout<<"一样大"<<endl;
}
//使用C++风格字符串比较
if(s1 > s2)
{
cout<<"s1 大"<<endl;
}else if(s1<s2)
{
cout<<"s1 小"<<endl;
}else
{
cout<<"一样大"<<endl;
}
return 0;
}
string类型的成员访问 at()
#include <iostream>
using namespace std;
int main()
{
string s1 = "hello world";
s1.at(0) = 'H'; //使用at访问字符串的成员,该函数有下标越界检查,如果下标越界则会报错
cout<<"s1 = "<<s1<<endl;
s1[6] = 'W'; //使用 中括号时,没有下标越界检查
cout<<"s1 = "<<s1<<endl;
return 0;
}
字符串的输入
#include <iostream>
using namespace std;
int main()
{
string s;
cout<<"请输入一个字符串:";
//cin>>s; //只能输入不带空格的字符串
getline(cin, s); //可以输入带空格的字符串
cout<<"s = "<<s<<endl;
return 0;
}
引用的基本使用
#include <iostream>
using namespace std;
int main()
{
int num = 520; //定义一个变量
int &ref = num; //定义一个引用,目标为num,那么此时ref跟num是同一空间
cout<<"ref = "<<ref<<" num = "<<num<<endl; //值相同
cout<<"&ref = "<<&ref<<" &num = "<<&num<<endl; //地址相同
cout<<"sizeof ref = "<<sizeof (ref)<<" sizeof num = "<<sizeof (num)<<endl; //空间大小相同
cout<<"type of ref = "<<typeid (ref).name()<<endl; //类型相同
cout<<"type of num = "<<typeid (num).name()<<endl;
num = 1314; //更改num的值
cout<<"ref = "<<ref<<" num = "<<num<<endl; //1314
ref = 666;
cout<<"ref = "<<ref<<" num = "<<num<<endl; //666
int key = 999; //定义其他变量
ref = key; //不会报错,该语句只是使用key给ref进行赋值操作
cout<<"&ref = "<<&ref<<" &num = "<<&num<<" &key = "<<&key<<endl;
int &op = num; //定义新的引用指向num
cout<<"&ref = "<<&ref<<" &num = "<<&num<<" &op = "<<&op<<endl; //一个目标可以有多个引用
return 0;
}
引用做形参(重要应用)
#include <iostream>
using namespace std;
//两数交换函数1
void fun1(int m, int n)
{
int temp = m;
m = n;
n = temp;
cout<<"fun1::m = "<<m<<" n = "<<n<<endl; //1314 520
}
//两数交换函数2
void fun2(int *m, int *n)
{
int *temp = m;
m = n;
n = temp;
cout<<"fun1::m = "<<*m<<" n = "<<*n<<endl; //1314 520
}
//两数交换函数3
void fun3(int *m, int *n)
{
int temp;
temp = *m;
*m = *n;
*n = temp;
cout<<"fun1::m = "<<*m<<" n = "<<*n<<endl; //1314 520
}
//定义交换函数4
void fun4(int &m, int &n)
{
int temp = m;
m = n;
n = temp;
cout<<"fun1::m = "<<m<<" n = "<<n<<endl; //520 1314
cout<<"&m = "<<&m<<" &n = "<<&n<<endl;
}
int main()
{
int num = 520;
int key = 1314;
//调用功能函数1
fun1(num,key);
cout<<"main::num = "<<num<<" key = "<<key<<endl; //520 1314
//调用功能函数2
fun2(&num,&key);
cout<<"main::num = "<<num<<" key = "<<key<<endl; //520 1314
//调用功能函数3
fun3(&num,&key);
cout<<"main::num = "<<num<<" key = "<<key<<endl; //1314 520
//调用功能函数4
fun4(num,key);
cout<<"main::num = "<<num<<" key = "<<key<<endl; //520 1314
cout<<"&num = "<<&num<<" &key = "<<&key<<endl;
return 0;
}
常引用(const)
#include <iostream>
using namespace std;
int main()
{
int num = 520;
const int &ref = num; //定义常引用,目标为非常变量
cout<<"ref = "<<ref<<endl; //通过引用读取目标中的数据没有问题
//ref = 1314; //不可用通过常引用更改目标的值
num = 1314; //可以使用变量自身进行更改
cout<<"ref = "<<ref<<endl; //1314
int key = 666;
//double &r = 777; //左值引用只能引用左值,不能引用右值
//int &&t = 777; //右值引用,只能引用右值,不能引用左值
//double &&t = key; //在自动类型转换中,本质上是将数据存放在一个临时空间,
//然后将临界空间的值赋值给新变量
const double &t = key; //const修饰后,可以引用临时值
return 0;
}
引用做返回值
#include <iostream>
using namespace std;
int &fun()
{
//int num = 520; //局部变量的地址不能当做引用函数的返回值
static int num = 520;
//cout<<"&num = "<<&num<<endl; //静态区地址
return num;
}
int main()
{
//cout<<"num = "<<num<<endl; //虽然是在静态区,但是num还依然是局部变量
cout<<"fun() = "<<fun()<<endl; //520
//cout<<"&fun() = "<<&fun()<<endl; //地址一致
fun() = 1314; //给静态区的num进行赋值
cout<<"fun() = "<<fun()<<endl; //1314
return 0;
}
结构体中的引用成员
#include <iostream>
using namespace std;
struct Stu
{
string name; //姓名
int age; //年龄
double &score; //分数 结构体中有引用成员
};
int main()
{
double s = 90;
//使用结构体定义变量时,必须给引用成员初始化目标
struct Stu s1 = {"zahngsan", 18, s}; //定义一个学生类型的变量
return 0;
}
C++中的动态内存分配和回收
#include <iostream>
using namespace std;
int main()
{
//在堆区申请一个int类型大小的空间
int *p1 = new int;
cout << "*p1 = "<<*p1<<endl; //没有初始化,空间中 的值是随机值
*p1 = 520;
cout << "*p1 = "<<*p1<<endl; //520
//在堆区申请一个double类型的空间并初始化
double *p2 = new double(3.14);
cout << "*p2 = "<<*p2<<endl; //3.14
//在堆区申请一个字符类型的空间并初始化
char *p3 = new char{'H'};
cout << "*p3 = "<<*p3<<endl; //H
//释放堆区空间
delete p1;
delete p2;
delete p3;
cout<<"*************************************"<<endl;
//在堆区连续申请5个int类型的空间
int *p4 = new int[5]; //没有初始化,默认都是随机值
for(int i=0; i<5; i++)
{
cout<<p4[i]<<" ";
}
cout<<endl;
//给空间内容进行赋值
for(int i=0; i<5; i++)
{
p4[i] = i+10;
}
for(int i=0; i<5; i++)
{
cout<<p4[i]<<" ";
}
cout<<endl;
//在堆区连续申请5个int类型的空间并初始化
int *p5 = new int[5]{333,444,555,666,777};
for(int i=0; i<5; i++)
{
cout<<p5[i]<<" ";
}
cout<<endl;
//释放连续内存空间
delete [] p4;
delete [] p5;
p1 = nullptr; //(void *)0
p2 = nullptr;
p3 = nullptr;
p4 = nullptr;
p5 = nullptr;
return 0;
}
函数重载(overload)
#include <iostream>
using namespace std;
//定义两个 整数求和
int my_sum(int m, int n) //my_sumii
{
return m+n;
}
//定义两个小数求和
double my_sum(double m, double n) //my_sumdd
{
return m+n;
}
//定义三个整型数据求和
int my_sum(int a, int b, int c) //my_sumiii
{
return a+b+c;
}
//定义两个字符串求和‘
string my_sum(string m, string n)
{
return m+n;
}
int main()
{
cout << my_sum(2,8) << endl; //自动调用参数为整形的函数 8
cout << my_sum(2.4,8.3) << endl; //自动调用参数为double的函数 10.7
cout<<my_sum("hello" ,"world")<<endl; //自动调用参数为string类型的函数 helloworld
return 0;
}
函数的默认参数
#include <iostream>
using namespace std;
//当被调函数在主调函数之后,需要对被调函数进行声明,函数的默认参数应该设置在声明部分
int my_sum(int m ,int n =10 , int k =10 );
int main()
{
cout << my_sum(1,2, 3) << endl; //6
cout << my_sum(1,2) << endl; //13
cout << my_sum(1) << endl; //21
return 0;
}
//函数定义处,就不需要在设置默认参数了
int my_sum(int m ,int n , int k )
{
return m+n+k;
}
函数重载和默认参数同时出现
#include <iostream>
using namespace std;
//当被调函数在主调函数之后,需要对被调函数进行声明,函数的默认参数应该设置在声明部分
int my_sum(int ,int =10 , int =10 );
//声明一个两个整数求和 定义该函数没有问题
int my_sum(int a, int b) //尽量不要出现这种问题
{
return a+b;
}
int main()
{
cout << my_sum(1,2, 3) << endl; //6
//cout << my_sum(1,2) << endl; //调用该函数时,会产生混乱
cout << my_sum(1) << endl; //21
return 0;
}
//函数定义处,就不需要在设置默认参数了
int my_sum(int m ,int n , int k )
{
return m+n+k;
}
哑元
#include <iostream>
using namespace std;
//函数定义处,就不需要在设置默认参数了
int my_sum(int m ,int , int k )
{
return m+k;
}
int main()
{
cout << my_sum(1,2, 3) << endl; //4
cout << my_sum(1,2, 3) << endl;
cout << my_sum(1,2, 3) << endl;
cout << my_sum(1,2, 3) << endl;
cout << my_sum(1,2, 3) << endl;
cout << my_sum(1,2, 3) << endl;
cout << my_sum(1,2, 3) << endl;
cout << my_sum(1,2, 3) << endl;
cout << my_sum(1,2, 3) << endl;
cout << my_sum(1,2, 3) << endl;
cout << my_sum(1,2, 3) << endl;
return 0;
}