由于本人有其他语言编程基础,所以对大部分c++较为基础的内容不做笔记。只记录个人之前不熟悉的知识点,此系列文章**不适合新手学习,仅做个人笔记记录。**同时由于本人对c++不熟悉,可能会有理解错误的地方,欢迎大佬来指出错误。
部分学习内容取自菜鸟教程
double pow(double, double);
//假设第一个参数为 x,第二个参数为 y,则该函数返回 x 的 y 次方。
int abs(int);
//该函数返回整数的绝对值。
double fabs(double);
//该函数返回任意一个浮点数的绝对值。
double floor(double);
//该函数返回一个小于或等于传入参数的最大整数。
伪随机:计算机无法模拟真正意义上的随机数,计算机会根据随机种子和特定的计算公式生成随机数,如果随机种子不发生变化,则无论运行多少次,结果总会相同,从而失去数据的随机性。
//随机种子默认为定值,不发生变化
cout << rand() << endl;
cout << rand() << endl;
cout << rand() << endl;
cout << rand() << endl;
/*
第一次调用:
41
18467
6334
26500
第二次调用:
41
18467
6334
26500
*/
srand(time(NULL));//设置时间(动态不重复)为随机种子,则函数在每次调用时种子都会发生变化,从而体现数据随机性
cout << rand() << endl;
cout << rand() << endl;
cout << rand() << endl;
cout << rand() << endl;
/*
第一次调用:
3252
24690
18002
10840
第二次调用:
3318
10283
14837
586
*/
int * getRandom( )
{
static int r[10];
// 设置种子
srand( (unsigned)time( NULL ) );
for (int i = 0; i < 10; ++i)
{
r[i] = rand();
cout << r[i] << endl;
}
return r;
}
// 要调用上面定义函数的主函数
int main ()
{
// 一个指向整数的指针
int *p;
p = getRandom();
for ( int i = 0; i < 10; i++ )
{
cout << "*(p + " << i << ") : ";
cout << *(p + i) << endl;
}
return 0;
}
624723190
1468735695
807113585
976495677
613357504
1377296355
1530315259
1778906708
1820354158
667126415
*(p + 0) : 624723190
*(p + 1) : 1468735695
*(p + 2) : 807113585
*(p + 3) : 976495677
*(p + 4) : 613357504
*(p + 5) : 1377296355
*(p + 6) : 1530315259
*(p + 7) : 1778906708
*(p + 8) : 1820354158
*(p + 9) : 667126415
char site[7] = {'R', 'U', 'N', 'O', 'O', 'B', '\0'};
cout << site << endl;
//RUNOOB
strcpy(s1, s2);
复制字符串 s2 到字符串 s1。
strcat(s1, s2);
连接字符串 s2 到字符串 s1 的末尾。连接字符串也可以用 + 号,例如:
string str1 = "runoob";
string str2 = "google";
string str = str1 + str2;
strlen(s1);
返回字符串 s1 的长度。
strcmp(s1, s2);
如果 s1 和 s2 是相同的,则返回 0;如果 s1
strchr(s1, ch);
返回一个指针,指向字符串 s1 中字符 ch 的第一次出现的位置。
strstr(s1, s2);
返回一个指针,指向字符串 s1 中字符串 s2 的第一次出现的位置。
char str1[13] = "runoob";
char str2[13] = "google";
char str3[13];
int len ;
// 复制 str1 到 str3
strcpy( str3, str1);
cout << "strcpy( str3, str1) : " << str3 << endl;
// 连接 str1 和 str2
strcat( str1, str2);
cout << "strcat( str1, str2): " << str1 << endl;
// 连接后,str1 的总长度
len = strlen(str1);
cout << "strlen(str1) : " << len << endl;
/*
strcpy( str3, str1) : runoob
strcat( str1, str2): runoobgoogle
strlen(str1) : 12
*/