目录
一、C++setw()的使用
setw() 函数需要加头文件 #include <iomanip>
实例:
#include <iostream>
#include <iomanip>
using namespace std;
int main(void)
{
cout << "AA" << setw(4) << "BB" << endl;
cout << "AA" << setw(8) << "BB" << endl;
cout << "AA" << setw(20) << "BB" << endl;
return 0;
}
结果:
二、C++数组
定义数组:
数据类型 变量[个数] ;
实例:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int n[10]; // n 是一个包含 10 个整数的数组
// 初始化数组元素
for (int i = 0; i < 10; i++)
{
n[i] = i + 100; // 设置元素 i 为 i + 100
}
cout << "Element" << setw(13) << "Value" << endl;
// 输出数组中每个元素的值
for (int j = 0; j < 10; j++)
{
cout << setw(7) << j << setw(13) << n[j] << endl;
}
return 0;
}
结果:
三、C++字符串
实例:
#include <iostream>
using namespace std;
int main()
{
char site[7] = { 'R', 'U', 'N', 'O', 'O', 'B', '\0' };
cout << "菜鸟教程: ";
cout << site << endl;
return 0;
}
结果:
函数:
序号 | 函数 & 目的 |
---|---|
1 | strcpy(s1, s2); 复制字符串 s2 到字符串 s1。 |
2 | strcat(s1, s2); 连接字符串 s2 到字符串 s1 的末尾。连接字符串也可以用 + 号,例如: string str1 = "runoob"; string str2 = "google"; string str = str1 + str2; |
3 | strlen(s1); 返回字符串 s1 的长度。 |
4 | strcmp(s1, s2); 如果 s1 和 s2 是相同的,则返回 0;如果 s1<s2 则返回值小于 0;如果 s1>s2 则返回值大于 0。 |
5 | strchr(s1, ch); 返回一个指针,指向字符串 s1 中字符 ch 的第一次出现的位置。 |
6 | strstr(s1, s2); 返回一个指针,指向字符串 s1 中字符串 s2 的第一次出现的位置。 |
实例:
#include <iostream>
#include <string>
using namespace std;
int main()
{
//定义string类
string str1 = "runoob";
string str2 = "google";
string str3;
int len;
// 复制 str1 到 str3
str3 = str1;
cout << "str3 : " << str3 << endl;
// 连接 str1 和 str2
str3 = str1 + str2;
cout << "str1 + str2 : " << str3 << endl;
// 连接后,str3 的总长度
len = str3.size();
cout << "str3.size() : " << len << endl;
return 0;
}
结果:
四、C++指针
指针是一个变量,其值为另一个变量的地址,即,内存位置的直接地址。就像其他变量或常量一样,您必须在使用指针存储其他变量地址之前,对其进行声明。指针变量声明的一般形式为:
type *变量名;
在这里,type 是指针的基类型,它必须是一个有效的 C++ 数据类型,var-name 是指针变量的名称。用来声明指针的星号 * 与乘法中使用的星号是相同的。但是,在这个语句中,星号是用来指定一个变量是指针。以下是有效的指针声明:
int *ip; /* 一个整型的指针 */ double *dp; /* 一个 double 型的指针 */ float *fp; /* 一个浮点型的指针 */ char *ch; /* 一个字符型的指针 */
所有指针的值的实际数据类型,不管是整型、浮点型、字符型,还是其他的数据类型,都是一样的,都是一个代表内存地址的长的十六进制数。不同数据类型的指针之间唯一的不同是,指针所指向的变量或常量的数据类型不同。
实例:
#include <iostream>
using namespace std;
int main()
{
int var = 20; // 实际变量的声明
int* ip; // 指针变量的声明
ip = &var; // 在指针变量中存储 var 的地址
cout << var << endl;
// 输出在指针变量中存储的地址
cout << ip << endl;
// 访问指针中地址的值
cout << *ip << endl;
return 0;
}
结果:
在 C++ 中,有很多指针相关的概念,这些概念都很简单,但是都很重要。下面列出了 C++ 程序员必须清楚的一些与指针相关的重要概念:
概念 | 描述 |
---|---|
C++ Null 指针 | C++ 支持空指针。NULL 指针是一个定义在标准库中的值为零的常量。 |
C++ 指针的算术运算 | 可以对指针进行四种算术运算:++、--、+、- |
C++ 指针 vs 数组 | 指针和数组之间有着密切的关系。 |
C++ 指针数组 | 可以定义用来存储指针的数组。 |
C++ 指向指针的指针 | C++ 允许指向指针的指针。 |
C++ 传递指针给函数 | 通过引用或地址传递参数,使传递的参数在调用函数中被改变。 |
C++ 从函数返回指针 | C++ 允许函数返回指针到局部变量、静态变量和动态内存分配。 |
五、C++引用
引用变量是一个别名,也就是说,它是某个已存在变量的另一个名字。一旦把引用初始化为某个变量,就可以使用该引用名称或变量名称来指向变量。
C++ 引用 与 指针
引用很容易与指针混淆,它们之间有三个主要的不同:
- 不存在空引用。引用必须连接到一块合法的内存。
- 一旦引用被初始化为一个对象,就不能被指向到另一个对象。指针可以在任何时候指向到另一个对象。
- 引用必须在创建时被初始化。指针可以在任何时间被初始化。
实例:
#include <iostream>
using namespace std;
int main(void)
{
// 声明简单的变量
int i;
double d;
// 声明引用变量
int& r = i;
double& s = d;
i = 5;
cout << "Value of i : " << i << endl;
cout << "Value of i reference : " << r << endl;
d = 11.7;
cout << "Value of d : " << d << endl;
cout << "Value of d reference : " << s << endl;
s = 15;
cout << "Value of d : " << d << endl;
cout << "Value of d reference : " << s << endl;
return 0;
}
结果:
概念 | 描述 |
---|---|
把引用作为参数 | C++ 支持把引用作为参数传给函数,这比传一般的参数更安全。 |
把引用作为返回值 | 可以从 C++ 函数中返回引用,就像返回其他数据类型一样。 |
六、C++标准输入(cin)
预定义的对象 cin 是 iostream 类的一个实例。cin 对象附属到标准输入设备,通常是键盘。cin 是与流提取运算符 >> 结合使用的。
实例:
#include <iostream>
using namespace std;
int main()
{
char name[32];
cout << "请输入您的名称: ";
cin >> name;
cout << "您的名称是: " << name << endl;
}
结果:
七、C++结构体
实例:
#include <iostream>
using namespace std;
//定义一个结构体
struct str
{
int a;
int b;
int c;
};
int main()
{
//定义一个结构体变量
struct str map;
map.a = 1;
map.b = 2;
map.c = 3;
cout << map.a << endl;
cout << map.b << endl;
cout << map.c << endl;
return 0;
}
结果:
typedef 关键字 :
实例:
#include <iostream>
using namespace std;
//定义一个结构体
typedef struct str
{
int a;
int b;
int c;
}Dook;
int main()
{
//定义一个结构体变量
Dook map;
map.a = 1;
map.b = 2;
map.c = 3;
cout << map.a << endl;
cout << map.b << endl;
cout << map.c << endl;
return 0;
}
结果:
八、C++日期与时间
C++ 标准库没有提供所谓的日期类型。C++ 继承了 C 语言用于日期和时间操作的结构和函数。为了使用日期和时间相关的函数和结构,需要在 C++ 程序中引用 <ctime> 头文件。
有四个与时间相关的类型:clock_t、time_t、size_t 和 tm。类型 clock_t、size_t 和 time_t 能够把系统时间和日期表示为某种整数。
结构类型 tm 把日期和时间以 C 结构的形式保存,tm 结构的定义如下:
struct tm {
int tm_sec; // 秒,正常范围从 0 到 59,但允许至 61
int tm_min; // 分,范围从 0 到 59
int tm_hour; // 小时,范围从 0 到 23
int tm_mday; // 一月中的第几天,范围从 1 到 31
int tm_mon; // 月,范围从 0 到 11
int tm_year; // 自 1900 年起的年数
int tm_wday; // 一周中的第几天,范围从 0 到 6,从星期日算起
int tm_yday; // 一年中的第几天,范围从 0 到 365,从 1 月 1 日算起
int tm_isdst; // 夏令时
};
下面是 C/C++ 中关于日期和时间的重要函数。所有这些函数都是 C/C++ 标准库的组成部分,您可以在 C++ 标准库中查看一下各个函数的细节。
序号 | 函数 & 描述 |
---|---|
1 | time_t time(time_t *time); 该函数返回系统的当前日历时间,自 1970 年 1 月 1 日以来经过的秒数。如果系统没有时间,则返回 -1。 |
2 | char *ctime(const time_t *time); 该返回一个表示当地时间的字符串指针,字符串形式 day month year hours:minutes:seconds year\n\0。 |
3 | struct tm *localtime(const time_t *time); 该函数返回一个指向表示本地时间的 tm 结构的指针。 |
4 | clock_t clock(void); 该函数返回程序执行起(一般为程序的开头),处理器时钟所使用的时间。如果时间不可用,则返回 -1。 |
5 | char * asctime ( const struct tm * time ); 该函数返回一个指向字符串的指针,字符串包含了 time 所指向结构中存储的信息,返回形式为:day month date hours:minutes:seconds year\n\0。 |
6 | struct tm *gmtime(const time_t *time); 该函数返回一个指向 time 的指针,time 为 tm 结构,用协调世界时(UTC)也被称为格林尼治标准时间(GMT)表示。 |
7 | time_t mktime(struct tm *time); 该函数返回日历时间,相当于 time 所指向结构中存储的时间。 |
8 | double difftime ( time_t time2, time_t time1 ); 该函数返回 time1 和 time2 之间相差的秒数。 |
9 | size_t strftime(); 该函数可用于格式化日期和时间为指定的格式。 |
实例:
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
// 基于当前系统的当前日期/时间
time_t now = time(0);
// 把 now 转换为字符串形式
char* dt = ctime(&now);
cout << "本地日期和时间:" << dt << endl;
// 把 now 转换为 tm 结构
tm* gmtm = gmtime(&now);
dt = asctime(gmtm);
cout << "UTC 日期和时间:" << dt << endl;
}
结果:
使用结构 tm 格式化时间
tm 结构在 C/C++ 中处理日期和时间相关的操作时,显得尤为重要。tm 结构以 C 结构的形式保存日期和时间。大多数与时间相关的函数都使用了 tm 结构。下面的实例使用了 tm 结构和各种与日期和时间相关的函数。
在练习使用结构之前,需要对 C 结构有基本的了解,并懂得如何使用箭头 -> 运算符来访问结构成员。
实例:
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
// 基于当前系统的当前日期/时间
time_t now = time(0);
cout << "1970年到目前经过秒数:" << now << endl;
tm* ltm = localtime(&now);
// 输出 tm 结构的各个组成部分
cout << "年: " << 1900 + ltm->tm_year << endl;
cout << "月: " << 1 + ltm->tm_mon << endl;
cout << "日: " << ltm->tm_mday << endl;
cout << "时间: " << ltm->tm_hour << ":";
cout << ltm->tm_min << ":";
cout << ltm->tm_sec << endl;
}
结果: