11. 字符串
- C风格字符串
自动在尾部加入’\0’作为字符串结束标识符。
//字符串
char site[] = "RUNNOB";
int size = sizeof(site); //7 加上结束符所占的大小
int len = strlen(site); //6 字符串长度
cout << site << " "<< size << " " << len << endl;//RUNNOB 7 6
char str2[7];
strcpy(str2,site); //拷贝,site -> str2
cout << str2 <<endl;
char s1[] = "abc";
char s2[] = "abc";
cout << strcmp(s1,s2) <<endl; // 比较二者字符串,== 为0, < 为负数,> 为正数
char* chPoint = strchr(s1,'b');
char* strPoint = strstr(s1,"bc");
char* result = strcat(s1,"def");
cout << result <<endl;
system("pause");
相关的函数:
strcpy(s1, s2):复制字符串 s2 到字符串 s1。
strcat(s1, s2):连接字符串 s2 到字符串 s1 的末尾。
strlen(s1):返回字符串 s1 的长度。
strcmp(s1, s2):如果 s1 和 s2 是相同的,则返回 0;如果 s1<s2 则返回值小于 0;如果 s1>s2 则返回值大于 0。
strchr(s1, ch):返回一个指针,指向字符串 s1 中字符 ch 的第一次出现的位置。
strstr(s1, s2):返回一个指针,指向字符串 s1 中字符串 s2 的第一次出现的位置。
- C++的string类型
//C++ string类型,是一个类,不是基本数据类型
string str1 = "hello";
string str3 = "world";
string str4;
str4 = str1; //深拷贝
str1 = "0";
cout << str4 << " "<< str1 << endl; //hello 0
int strLen = str1.size(); //类 有自己的属性和方法
str4 = str1 + str3; // 字符串拼接
cout << str4 <<endl;//0world
12. 引用
引用在创建时,必须初始化,并且初始化之后不可再更改。指针可以重新指向新的变量。引用相当于给同一个变量取别名。
- 引用作为参数
int main ()
{
// 局部变量声明
int a = 100;
int b = 200;
cout << "交换前,a 的值:" << a << endl; //100
cout << "交换前,b 的值:" << b << endl; //200
/* 调用函数来交换值 */
swap(a, b);
cout << "交换后,a 的值:" << a << endl; //200
cout << "交换后,b 的值:" << b << endl; //100
return 0;
}
// 函数定义
void swap(int& x, int& y)
{
int temp;
temp = x; /* 保存地址 x 的值 */
x = y; /* 把 y 赋值给 x */
y = temp; /* 把 x 赋值给 y */
return;
}
- 引用作为返回值
当函数返回一个引用时,则返回一个指向返回值的隐式指针。这样,函数就可以放在赋值语句的左边
double vals[] = {10.1, 12.6, 33.1, 24.1, 50.0};
double& setValues(int i) {
double& ref = vals[i];
return ref; // 返回第 i 个元素的引用,ref 是一个引用变量,ref 引用 vals[i]
}
setValues(1) = 20.23; // 改变第 2 个元素
//不能返回局部变量(超出作用域),可以返回static局部变量
13. 日期和时间
主要是两个结构体类型,time_t时间戳对象,秒为单位、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; // 夏令时
};
time_t now = time(0); //时间戳,秒为单位
char* strTime = ctime(&now); //时间戳转为字符串
tm* gmtTime = gmtime(&now); //转为UTC时间的tm结构
tm* localTime = localtime(&now); //转为本地时间的tm结构
char* tmStr = asctime(localTime);//tm结构转为字符串
time_t cnow = mktime(localTime); //tm结构再转会时间戳
cout << strTime << " " << tmStr <<endl;
// 输出 tm 结构的各个组成部分
cout << "year: "<< 1900 + localTime->tm_year << endl;
cout << "month: "<< 1 + localTime->tm_mon<< endl;
cout << "day: "<< localTime->tm_mday << endl;
cout << "time: "<< localTime->tm_hour << ":";
cout << localTime->tm_min << ":";
cout << localTime->tm_sec << endl;
14. 结构体
类似于类,二者的使用:
(1) 在表示诸如点、矩形等主要用来存储数据的轻量级对象时,首选struct。
(2) 在表示数据量大、逻辑复杂的大对象时,首选class。
(3) 在表现抽象和多级别的对象层次时,class是最佳选择
struct Book
{
char name[10];
char author[10];
int id;
};
//结构体
Book book;
strcpy(book.author,"C++");
strcpy(book.name,"faf++");
book.id = 1;
print(book);