C++言語のこまごました知識点のまとめ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include
using namespace std;
int *GetNum(int x); //
void main(void)
{
cout << "===============start================" << endl;
int num;
cout << "Please enter the number between 0 and 6: ";
cin >> num;
cout << "result is:" << *GetNum(num) << endl; //
system("pause");
}
int *GetNum(int x) {
static int num[] = { 0,1,2,3,4,5,6 }; // static , ,
return &num[x]; //
}
2、関数ポインタ関数ポインタは、関数を指すポインタです.各関数はコンパイル時にエントリアドレスが割り当てられ、一般的に関数名で表され、このアドレスが関数のポインタである.構文:
:type (*func)( )
上記の定義形式から分かるように、関数ポインタとポインタ関数の直感的な違いは、ポインタ記号*と関数名/ポインタ名が括弧()で包まれているかどうかであり、この点から両者を区別するのは容易である.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include
using namespace std;
int max(int a, int b) {
return a>b ? a : b;
}
void main(void)
{
cout << "===========start===========" << endl;
int(*func)(int, int); //
func = max;
int a, b;
cout << "Please enter two numbers:";
cin >> a >> b;
cout << "max=" << (*func)(a, b) << endl; //
cout << "max=" << max(a, b) << endl; //
cout << "max=" << func(a, b) << endl; // ,func = max
system("pause");
}
上記の例では、定義されたポインタ変数(*func)で直接呼び出される2つの最も一般的な呼び出し形式と、ポインタ名で呼び出される2つの呼び出し形式が示されています.後者は、元の関数を新しい名前で呼び出すように見え、関数の別名と見なすことができます.もう一度強調します.ポインタ変数名とポインタ記号*は必ず括弧で包んでください.
3、関数ポインタ配列は各要素が関数ポインタの配列であり、直接関数ポインタ名の後ろに配列記号[]を付ければよい.構文:
:type (*func[ ])( )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include
using namespace std;
void fun1()
{
cout << " fun1" << endl;
}
void fun2()
{
cout << " fun2" << endl;
}
void fun3()
{
cout << " fun3" << endl;
}
int main()
{
//
void(*pfun)() = &fun1;
void(*pfun2)() = &fun2;
void(*pfun3)() = &fun3;
// 。
void(*pfunarr[3])();
void(*pfunarr[3])();
pfunarr[0] = pfun;
pfunarr[1] = pfun2;
pfunarr[2] = pfun3;
/*
pfunarr[0] = &fun1;
pfunarr[1] = &fun2;
pfunarr[2] = &fun3;
*/
//
pfunarr[0]();
pfunarr[1]();
pfunarr[2]();
/*
(*pfunarr[0])();
(*pfunarr[1])();
(*pfunarr[2])();
*/
system("pause");
return 0;
}
4、関数ポインタ配列へのポインタ構文:
:type (* (*func )[ ])( )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include
using namespace std;
void fun1()
{
cout << " fun1" << endl;
}
void fun2()
{
cout << " fun2" << endl;
}
void fun3()
{
cout << " fun3" << endl;
}
int main()
{
//void(*pfun)() = &fun1;
//void(*pfun2)() = &fun2;
//void(*pfun3)() = &fun3;
// 。
void(*pfunarr[3])();
void(*(*pfunarr2)[3])();
// , 3 。
pfunarr[0] = &fun1;
pfunarr[1] = &fun2;
pfunarr[2] = &fun3;
pfunarr2 = &pfunarr;
(*pfunarr2)[0]();
pfunarr[0]();
system("pause");
return 0;
}
:void(*(*pfunarr2)[3])()
(* pfunarr2)[3] , void(* )( ) , 。
5、ポインタ、アドレス、参照参照:参照は変数の別の名前であり、別名とも呼ばれる.定義方法:
int a=10; int &b=a;
ここでは、a変数に新しい名前bを付けたことを意味するので、bは再定義できない.参照は初期化され、空の参照がなく、参照は等級を問わない必要があります.たとえば、スワップ関数swap()1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
void swap(int &a,int &b)
{
int temp=a;
a=b;
b=temp;
}
void main()
{
int x=10,y=20;
swap(x,y);
}
:
void swap(int *const a,int *const b)
{
int tmp=*a;
*a=*b;
*b=temp;
}
void main()
{
int x=10,y=20;
swap(&x,&y);
}
int pos;
char ch[10];
int a[3];
scanf(%d,&pos); // &,
scanf(%s,ch);
scanf(%d,&a[1]); // a[3] a[0],a[1],a[2] , a[3]
変数aは本質的に記憶ユニットを表す.CPUは、記憶部のアドレスを介して記憶部のデータにアクセスする.従ってaは、本来、メモリセルのアドレスとメモリセルのデータの2つの値を表す.そこで二異性ができた.このような二義性を解消するために、C言語は、aがメモリセル内のデータを表し、&aがメモリセルのアドレスを表すことを規定する.ここで、aに対応するメモリセルのデータは、必ず別のメモリセルのアドレスであることが要求される.定義int× b;int a=5; b=&a;すると、×bは、別のメモリセルにおけるデータを表す.ポインタに値を割り当てるときb=&a;*bについてaに対応するメモリセルのアドレスのデータを示す.&aは、aに対応するメモリセルのアドレスを示す.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include
using namespace std;
int main(){
int* b;
int a=5;
b=&a;
cout<
6、方法配列要素の変更
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include
using namespace std;
void xiu(int *a){
a[0]=a[1];
}
/*
void xiu(int a[]){
a[0]=a[1];
}
void xiu(int a[4]){
a[0]=a[1];
}
s ,
*/
int main(){
int s[4]={1,2,3,4};
xiu(s);
for(int i=0;i<4;i++)
cout<
6、return文return文の一般的な形式は:return式;または:return(式);ありますか、ありませんか.例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
return max; return a+b; return (100+200); ~~~ **return , , return **, ( , Go ) ** return , 。** **return 。return , ;return , , 。** ------
- 1.1、 operator
struct Edge
{
int from, to, weight;
};
bool operator{
//使用大于号实现小于号,表示排序顺序与默认顺序相反。若使用小于号实现小于号,则相同。可用此法方便理解。
return a.weight > b.weight;
}
#include #include #include #include #include using namespace std; struct Edge{ int from, to, weight; Edge(int from, int to, int weight):from(from), to(to), weight(weight){}}; bool operator{return a.weight>b.weight;//もとは} int main(){ priority_queue pq; pq.push(Edge(0, 1, 7)); pq.push(Edge(1, 2, 4)); pq.push(Edge(2, 3, 10)); pq.push(Edge(3, 4, 5)); }対応する出力は:4 5 7 10 10 7 5 4 10 7 4 4 struct Edge{ int from, to, weight;};bool cmp(Edge a, Edge b){ return a.weight > b.weight;} //sort()関数sort(data.begin()、dataを使用します.end(), cmp); #include #include #include using namespace std; struct cmp{ bool operator()(const int &a, const int &b) { return a > b; }}; int main(){set se;set::iterator iter;se.insert(7);se.insert(4);se.insert(10);se.insert(5);//sort()関数はデフォルトでインクリメンタルであり、ここでは降順に反転する.for(iter=se.begin();iter!=se.end();++iter){cout<*iter< return 0;
} #include using namespace std;int main(int argc,char argv[]){const char p=“hello”;cout<
++pは、右から左へ結合する順序で(++p)に等価であり、++p(++は前、先に後、後に後)を先に実行し、実行後、pは3番目の文字、すなわちlを指し、式(++p)はpが増加した値を返すので、最終結果はlとなる. 同様に、pa++は(pa++)に等価であり、実行された結果は、式値が6(この結果、変数pa自体ではなく(pa++)式が作用し、pa値が1増加し、未知のメモリを指すことを証明することができる.++paを実行すると,paがこのとき指す位置が未知であるため,その中の内容を自己加算した結果も未知であるため,最終的な結果は1つのゴミ数である. strlen関数とsizeofオペレータ しんしんへんかん
|