C++基础用法
知识补充—各种数据类型在C++中的内存占用情况
一、length()函数
C++中length()函数只能用来获取字符串长度。
string str = "ADAS";
int len = str.length();
其中
len = 4
二、size()函数
C++中size()函数除了跟length()函数一样可以获取字符串长度之外,还可以获取vector类型的长度。
string str = "ADAS";
vector < int> num(10,5)
int lenstr = str.size();
int lenvec = num.size();
其中
lenstr = 4; lenvec = 10
三、sizeof()函数
sizeof() 用于获取对象所占内存空间大小
char c[] = "ADAS";
char* cc = c;
char cn[40] = "ADAS";
int a[] = {1,2,3,4,5,6};
int* aa = a;
cout << sizeof(c) << sizeof(cc) << sizeof(cc) << sizeof(cn);
cout << sizeof(a) << sizeof(aa) << sizeof(aa);
结果输出:
sizeof(c) = 5 //c是数组,计算到'\0'位置,结果为4 * 1+1=7
sizeof(cc) = 8 //cc为指针类型,大小为8
sizeof(*cc) = 1 //*cc指向c的第一个字符,大小为1
sizeof(cn) = 40 //开辟40个char空间,大小为40 * 1=40
sizeof(a) = 24 //a是数组,但不需计算到'\0',结果为6 * 4=24
sizeof(aa) = 8 //aa为指针类型,大小为8
sizeof(*aa) = 4 //*aa指向a的第一个数字,大小为4
四、sort()函数
一.sort函数
1.sort函数包含在头文件为#include的c++标准库中,调用标准库里的排序方法可以实现对数据的排序。
2.sort函数的模板有三个参数:
void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);
(1)第一个参数first:是要排序的数组的起始地址。
(2)第二个参数last:是结束的地址(最后一个数据的后一个数据的地址)
(3)第三个参数comp是排序的方法:可以是从升序也可是降序。如果第三个参数不写,则默认的排序方法是从小到大排序。
3.实例
从小到大
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
//sort函数第三个参数采用默认从小到大
int a[]={15,12,34,67,97,12,2,4,5,55};
sort(a,a+10);
for(int i=0;i<10;i++)
cout<<a[i]<<" ";
}
输出;
从大到小
#include<iostream>
#include<algorithm>
using namespace std;
bool cmp(int a, int b);
int main() {
//sort函数第三个参数自己定义,实现从大到小
int a[] = { 15,12,34,67,97,12,2,4,5,55 };
sort(a, a + 10, cmp);
for (int i = 0; i < 10; i++)
cout << a[i] << " ";
}
//自定义函数
bool cmp(int a, int b) {
return a > b;
}
#include<iostream>
#include<algorithm>
#include"cstring"
using namespace std;
typedef struct student {
char name[20];
int math;
int english;
}Student;
bool cmp(Student a, Student b);
int main() {
//先按math从小到大排序,math相等,按english从大到小排序
Student a[4] = { {"apple",87,89},{"wangxiaoyu",90,56},{"liuxing",56,99} };
sort(a, a + 3, cmp);
for (int i = 0; i < 3; i++)
cout << a[i].name << " " << a[i].math << " " << a[i].english << endl;
}
bool cmp(Student a, Student b) {
if (a.math > b.math)
return a.math < b.math;//按math从小到大排序
else if (a.math == b.math)
return a.english > b.english; //math相等,按endlish从大到小排序23
}
4.对于容器,容器中的数据类型可以多样化
1) 元素自身包含了比较关系,如int,double等基础类型,可以直接进行比较greater() 递减, less() 递增
#include<iostream>
#include<algorithm>
#include"vector"
using namespace std;
typedef struct student {
char name[20];
int math;
int english;
}Student;
bool cmp(Student a, Student b);
int main() {
int s[] = { 34,76,21,23,48 };
vector<int>arr(s, s + 5);
sort(arr.begin(), arr.end(), greater<int>());
for (int i = 0; i < arr.size(); i++)
cout << arr[i] << " ";
}
2)元素本身为class或者struct,类内部需要重载< 运算符,实现元素的比较;
注意事项:bool operator<(const className & rhs) const;
如何参数为引用,需要加const,这样临时变量可以赋值;重载operator<为常成员函数,可以被常变量调用;
#include<iostream>
#include<algorithm>
#include"vector"
using namespace std;
typedef struct student {
char name[20];
int math;
//按math从大到小排序
inline bool operator < (const student &x) const {
return math > x.math;
}
}Student;
int main() {
Student a[4] = { {"wxy",50},{"ly",90},{"apple",88} };
sort(a, a + 3);
for (int i = 0; i < 3; i++)
cout << a[i].name << " " << a[i].math << " " << endl;
}
重载<也可以定义为如下格式:
struct Cmp{
bool operator()(Info a1,Info a2) const {
return a1.val > a2.val;
}
};
最后记录一个知识点,C++内存占用情况