目次
小樽C++複数章⑧ (1)ポインタ変数https://blog.csdn.net/weixin_44775255/article/details/129031168
小樽C++マルチ第8章(2) ポインタと配列https://blog.csdn.net/weixin_44775255/article/details/129396791
3. 関数と文字列
文字列について言えば、文字列ライブラリをインポートする必要があります: #include<cstring>
文字列の一般的なメソッドを学びます。strcpy、strcmp、strstr、strlen。
//字符串
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char a[100],b[100];
int main(){
strcpy(a,"hello");
// cout<<a<<endl;
printf("%s,len=%d\n",a,strlen(a)); //字符串长度
scanf("%s",b);
int cmp = strcmp(a,b); //字符串比较大小
if(cmp==0){
printf("%s=%s\n",a,b);
}
else if(cmp<0){
printf("%s<%s\n",a,b);
}
else{
printf("%s>%s\n",a,b);
}
if(strstr(a,b)!= NULL){ //查找子串
printf("%s in %s\n",b,a);
}
return 0;
}
難易度: strcpyと strlen の起源を具体的に見てみましょう。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char *strcpy(char *dest,const char *scr){
char *p=dest;
while(*scr != '\0'){
*dest = *scr;
dest++;
scr++;
}
*dest = '\0';
return p;
}
int main(){
char *a=new char;
*strcpy(a,"cvbnm");
cout<<a<<endl;
*strcpy(a,"asd");
cout<<a;
return 0;
}
strcpy は代入の意味で、既知の文字列値を変数に代入し、変数の値を出力します。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
size_t strlen(const char *str){
const char *cp = str;
while (*cp++){
;
}
return (cp-str-1);
}
int main(){
cout<<strlen("abcdr")<<endl;
}
これらの関数の実装はすべてポインター操作です。配列を使用して実装することもできますが、配列のストレージ効率は低くなります。
4、関数とポインタ
4.1 関数パラメータとしてのポインタ
カスタム関数のパラメーターは整数型または浮動小数点型にすることができますが、ポインター型は使用できますか?
次に、ポインター パラメーターを使用して2 つの変数の値を交換してみます。
例 1. 2 つの値を交換して比較します。
#include<iostream>
#include<cstdio>
using namespace std;
void swap(int *x,int *y){ //交换两个值
int t = *x;
*x = *y;
*y = t;
}
void sort(int *x,int *y,int *z){
if(*x > *y) swap(x,y); //比大小
if(*x > *z) swap(x,z);
if(*y > *z) swap(y,z);
}
int main(){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
sort(&a,&b,&c);
printf("a=%d,b=%d,c=%d",a,b,c);
return 0;
}
4.2 関数リターンポインタ
ポインターを関数として使用します (例: int *a(int a, int b))。
レシピ 1. n 個の整数を含む配列の最初の素数を見つけます。素数があれば関数のアドレスを返します。素数がなければ、NULL を返します。
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int n,a[1000];
bool isprime(int n){//求质数
if (n<2) return false;
if(n==2) return true;
for(int i=2;i<=sqrt(n);i++){
if(n%i==0){
return false;
}
}
return true;
}
int *find(){ //指针查找函数
for(int i=1;i<=n;i++){
if(isprime(a[i])){
return &a[i];
}
}
return NULL;
}
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
}
int *p = find();
if(p != NULL){
cout<<p<<" "<<*p;
}
return 0;
}
4.3 関数ポインタと関数ポインタ配列
(1) 一般に関数を定義します: int test(int); その後、ポインタ関数 int (*test)(int); に変更します。
int *test (int); このように 、プログラミングでは test (int) として定義された関数が宣言され、戻り値の型は int * になります。
(2) 関数のアドレスを取得します。関数名は配列と同様にアドレスであり、関数名はポインタとみなすこともできます。
1. typedef を使用して関数ポインタの型を宣言します。
#include<iostream>
#include<cstdio>
using namespace std;
//函数指针
int sum(int a,int b){
return a+b;
}
typedef int (*LP)(int,int);
//定义声明了LP类型的函数指针,内有2个参数。
int main(){
LP p = sum; // 定义LP类型的指针p
cout<<p(2,5); //函数指针p调用参数
return 0;
}
2. シミュレーションメニュー関数の実装例、関数ポインタ配列
//函数指针数组
void t1(){ cout<<"test1"<<endl; }
void t2(){ cout<<"test2"<<endl; }
void t3(){ cout<<"test3"<<endl; }
void t4(){ cout<<"test4"<<endl; }
typedef void(*Tp)();
定义声明了空类型的函数指针 Tp,无参数。
int main(){
Tp a[] = {t1,t2,t3,t4};
定义TP类型的函数指针数组a
int x;
cin>>x;
a[x]();
return 0;
}
4.4 構造体ポインタ
- 名前、性別、学年のメンバーを含む構造を定義します。
- 構造体を宣言して値を割り当て、さらに構造体ポインターも宣言します。
#include<iostream>
#include<cstdio>
using namespace std;
struct Student{
char name[20];
char sex;
float score;
};
Student *p;
Student stu={"lisi",'m',95.5};
- 3. ポインターアクセス構造体のメンバーメソッド:
- (*ポインタ名).メンバー名 (*p).name
- ポインタ名 -> メンバー名 p -> 名前
int main(){
cout<<stu.name<<endl;
p = &stu; //指针指向stu地址
cout<<(*p).name<<endl;
// (*p).name 效果等同 stu.name
cout<<(*p).sex<<endl;
cout<<p->score;
// p->score 效果等同(*p).score
return 0;
}
通常、構造体変数を参照するため、構造体全体を参照することになり効率が悪いため、一般的にはポインタを使用する方が書き込み効率が向上します。