Skip to content

Commit 2c21be9

Browse files
committed
近期学习
1 parent 486ceee commit 2c21be9

6 files changed

Lines changed: 100 additions & 0 deletions
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <iostream>
2+
usingnamespace std;
3+
4+
/*
5+
在C++中无论是使用string.length 还是使用string.size都可以获取到一个字符串的长度。二者的结果是一样的,只是size一般用于容器,length一般用于表示字符串,由于length也算是一个容器,所以拥有size方法
6+
*/
7+
8+
int main(){
9+
std::string str = "str";
10+
cout << str.size() << endl; //3
11+
cout << str.length() << endl; //3
12+
return 0;
13+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <iostream>
2+
#include <string.h>
3+
using namespace std;
4+
5+
/*
6+
strstr(s1,s2): 测试s1字符串中是否存在s2字符串。返回的是第一次出现的位置(地址),如果不出现,就返回null
7+
----note:php中也存在相应的函数,如果存在就返回s1中的匹配点之后的字符串。如果不存在就返回false。
8+
*/
9+
10+
int main(){
11+
const char *s1 = "hello world,welcome to strstr function";
12+
const char *s2 = "world";
13+
cout << strstr(s1,s2) << endl;
14+
15+
//C++ 中使用string类的查找方法find
16+
string str = "hello world.This is anther world.";
17+
string toFind = "world";
18+
//find函数返回的是查到的位置索引。
19+
cout << str.find(toFind) << endl;
20+
//rfind函数是从后往前进行查找
21+
cout << str.rfind(toFind) << endl;
22+
return 0;
23+
}

一个函数返回值是函数指针.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//②: 对于 void (*pfunc)(); 定义了一个函数指针变量。由于是变量,不能有函数体。对于 pfunc() 跟 (*pfunc)() 这两个的效果也是一样的。
55
//③:不支持函数名的二次取地址 也就是 func == &func 但是&&func是错的。
66
//④:注意,pfunc != &pfunc 因为pfunc是一个指针变量,对它取地址是得到存这个变量的地址。 &&pfunc 没有意义。
7+
//note: 此文件不能用g++编译,只能gcc。
78

89
void* a() { return (void*)998; }
910

@@ -28,3 +29,18 @@ int main()
2829
printf("%u\n", (*bool(100))(&a));
2930
return 0;
3031
}
32+
33+
34+
/*扩展:
35+
这是一个函数申明,因为 *f1没有被扩起来
36+
返回值是void(**)(int),如果 (*f1)那么返回值是 void(*)(int)
37+
参数是(int, void(*)(int))
38+
*/
39+
void (*f1(int,void(*)(int)))(int);
40+
41+
/*扩展
42+
这是一个函数指针
43+
返回值是 void(**)()
44+
参数是int
45+
*/
46+
void (*(*f2)(int))() = NULL;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <iostream>
2+
#include <string.h>
3+
using namespace std;
4+
5+
/*
6+
C/C++会在字符串的末尾追加上'\0' 即使是用户所给定的字符串中存在了 '\0'
7+
如果程序员在一个字符串的末尾手动的加上了一个null,那么这个字符串就有了两个null结尾。
8+
*/
9+
10+
int main(){
11+
char s[] = "\0\0\0";
12+
cout << sizeof s << endl; //4
13+
cout << strlen(s) << endl; //0
14+
std::string str = "str";
15+
cout << str.size() << endl;
16+
cout << str.length() << endl;
17+
return 0;
18+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
/*
5+
1)下面的测试说明:C/C++中 函数的默认返回值是int。所以如果是没有返回值,应该明确的书写为void
6+
2)对于int类型返回值的函数,如果没有提供返回值,通过查询知道这个返回的是一个垃圾值,对于不同的编译器这个是未定义的。
7+
*/
8+
9+
func(int arr[], int length){
10+
if(!arr || length <= 0)
11+
return -1;
12+
return "hello world"; //这里报错,提示不能从const char * 转换为int
13+
}
14+
15+
int main(){
16+
int arr[] = {1,2,3};
17+
cout << func(arr,3) << endl;
18+
return 0;
19+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
测试原因:
3+
在一个地方看到,如果用一个不够长的数组进行字符串常量的保存,会导致编译错误。
4+
这里用一个只有5个长度的数组保存 "hello" 由于这个常量的末尾存在一个空字符 '\0'所以导致真实长度是6而不是5,这样就存在放不下的问题。
5+
测试结果:
6+
这的结果是提示一个错误,说是用了一个过长的字符串对一个字符数组进行初始化。
7+
*/
8+
int main(){
9+
char arr[5] = "hello"; //提示用于数组初始化的字符串太长.
10+
return 0;
11+
}

0 commit comments

Comments
 (0)