A:取整函数:
引入cmath。
1,floor()会取不大于自变量的最大整数的(向下取整)
2,ceil()会取不小于自变量的最大整数(向上取整)
3,round()函数(四舍五入)
4,fix() 朝零方向取整,正数向下去,负数向上取(朝0取整)
B:精确到小数点n位函数:
引入#include <iomanip>,值为cout<<fixed<<setprecision(n);(n为保留的位数)
C:n的k次方函数:
引入cmath,值为pow(n,k)
D:求算术平方根函数:
引入cmath,值为sqrt(n);
E:绝对值函数:
引入cmath,值为abs(n)
F:开n次方根函数:
引入cmath,n对k次方开根为pow(n,1.0/k);
G:获取长度的函数:
1)length()函数
string str = "ADAS";
int len = str.length();
//len=4,
c++中length()函数只能用来获取字符串长度。
2)size()函数
string str = "ADAS";
vector < int> num(10,5)
int lenstr = str.size();
int lenvec = num.size();
//其中lenstr = 4; lenvec = 10
C++中size()函数除了跟length()函数一样可以获取字符串长度之外,还可以获取vector类型的长度。
3)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
H:sort函数排序
先定义一个数组arr;
sort(arr, arr + 4);//对0到3进行升序
sort(arr, arr + 4,greater<int>());//对0到3进行序
vector<int> v={ 1,65,2 };
sort(v.begin(),v.end());//从小到大排序
for (int i = 0; i < v.size(); i++)
{
cout << v[i] << ' ';
}
I:cin.getline(s, 10); //s是字符串的名称,10是字符串长度
将一行字符从文件或屏幕中读入,可以读空格,遇到换行跳出。
J:一串字符的有效长度
#include<bits/stdc++.h>
using namespace std;
int main()
{
char s[100] = "asd";
int n = strlen(s);
cout << n << endl;//n等于3
return 0;
}