学习数据结构-补充部分C++知识-基础不牢-地动山摇

各位同学:

       本学期的数据结构课程授课过程中采用了一点点C++的知识,目的是为了简化、方便代码的编写。涉及的知识点有如下几个:

 

1、文件扩展名的变化

C语言的源代码文件扩展名为c,C++语言的源代码文件扩展名为cpp

2、头文件的变化

C语言采用scanf、printf作为输入输出函数,格式繁杂,容易出错,对应的头文件格式为:#include<stdio.h>

C++语言采用cin、cout作为输入输出函数,格式简单,对应的头文件格式为:

#include <iostream>

using namespace std;

3、scanf、printf、cin、cout用法对比

功能要求:输入三个整数,存到变量a、b、c中

C语言版本

#include<stdio.h>

int main(  )

{

         int a,b,c;

         scanf("%d%d%d",&a,&b,&c);

         printf("%d %d %d\n",a,b,c);

         return 0;

}

C++版本

#include<iostream>

using namespace std;

int main(  )

{

         int a,b,c;

         cin>>a>>b>>c;

         cout<<a<<" "<<b<<" "<<c<<endl;

         return 0;

}

4、向操作系统申请空间的操作

C语言中向操作系统申请空间用malloc、calloc、realloc等,释放空间用free,需要添加头文件#include <stdlib.h>;C++语言中申请空间用new,释放空间用delete,在iostream中包含。

功能:申请5个整型数据空间,输入数据并输出

C语言版本

#include<stdio.h>

#include<stdlib.h>

int main(  )

{

         int *a;

         int i;

         a=(int *)malloc(sizeof(int)*5);

         for(i=0;i<5;i++)

                   scanf("%d",&a[i]);

 

         for(i=0;i<5;i++)

                   printf("%d ",a[i]);

 

         free(a);

         return 0;

}

C++版本

#include<iostream>

using namespace std;

int main(  )

{

         int *a;

         a=new int[5];

         int i;

         for(i=0;i<5;i++)

                   cin>>a[i];

 

         for(i=0;i<5;i++)

                   cout<<a[i]<<" ";

        

         delete []a;

         return 0;

}

 

5、参数传递

在c语言中,参数传递是单项值传递,如果想改变实际参数的值,需要传递指针,而C++语言中引入了传引用的语法,一举解决了这个问题。

在我们的教材中,采用的就是这种形式,大家一定要及早熟悉起来。

功能:交换两个数的值。

C语言版本

#include<stdio.h>

void swap1(int *a,int *b)

{

         int t;

         t=*a;*a=*b;*b=t;

}

int main(  )

{

         int m,n;

         scanf("%d%d",&m,&n);

         swap1(&m,&n);

         printf("%d %d\n",m,n);

         return 0;

}

C++版本

#include<iostream>

using namespace std;

void swap2(int &a,int &b)

{

         int t;

         t=a;a=b;b=t;

}

int main(  )

{

         int m,n;

         cin>>m>>n;

         swap2(m,n);

         cout<<m<<" "<<n<<endl;

         return 0;

}

解释:

void swap2(int &a,int &b)

//参数中变量前加&,表示按引用传递

//即:形式参数是实际参数的“别名”,二者指向同一个内存空间

 

 

6、结构体struct

在c语言中,结构体内只能定义成员变量;

在C++中,结构体内既可以定义成员变量,有可以定义成员函数。

C语言版本

#include<iostream>

using namespace std;

 

struct Add

{

         int a,b;

        

};

int main(  )

{

         struct Add s1;

         s1.a=3;

         s1.b=4;

        

         cout<<s1.a+s1.b<<endl;

          

         return 0;

}

C++版本

#include<iostream>

using namespace std;

struct Add

{

         int a,b;

         int add()

         {

                   return a+b;

         }

};

int main(  )

{

         struct Add s1;

         s1.a=3;

         s1.b=4;    

         cout<<s1.add()<<endl;   

         return 0;

}

7、函数模板

在C++中,可用函数模板处理只有类型不同的情况。

 

#include<iostream>

using namespace std;

 

template<typename T>

T add(T a,T b)

{

         return a+b;

}

int main(  )

{

         cout<<add(3,4)<<endl;

         cout<<add(3.3,4.4)<<endl;

          

         return 0;

}

 

8、结构体模板

在C++中,可用结构体模板处理只有类型不同的情况。

 

#include<iostream>

using namespace std;

 

template<typename T> struct Add

{

         T a,b;

         T add()

         {

                   return a+b;

         }

};

int main(  )

{

         Add<double> s1;

         s1.a=3.3;

         s1.b=4.4;

         cout<<s1.add()<<endl;   

         return 0;

}

9、类

在C++中,可以用class定义一个类,类也是数据类型。类的语法跟struct相似,也有成员变量和成员函数,但比struct重要得多,类是面向对象语言的核心。

 

#include<iostream>

using namespace std;

class Add

{

         int a,b;

         public:

         Add(int x,int y)

         {//构造函数

                   a=x;b=y;

         }

        

         ~Add()

         {//析构函数

                  

         }

         int add()

         {

                   return a+b;

         }

};

int main(  )

{

         Add s1(3,4);

        

         cout<<s1.add()<<endl;   

         return 0;

}

解释:

1、在定义一个类的对象时,构造函数自动执行;

2、在离开对象的作用域时,对象的析构函数自动执行。因此,析构函数通常被用来释放动态申请的空间等。

10、类模板

在C++中,类模板可以处理只有类型不同的情况。

 

#include<iostream>

using namespace std;

 

template<typename T>

class Add

{

         T a,b;

         public:

         Add(T x,T y)

         {//构造函数

                   a=x;b=y;

         }

        

         ~Add()

         {//析构函数

                  

         }

         T add()

         {

                   return a+b;

         }

};

int main(  )

{

         Add<int> s1(3,4);    

         cout<<s1.add()<<endl;

         Add<double> s2(3.3,4.4);        

         cout<<s2.add()<<endl;   

         return 0;

}

11、for_each

C++中for_each的用法

#include<iostream>

#include<algorithm>//for_each在其中

using namespace std;

void f(int x)

{

         cout<<x<<" ";

}

 

int main()

{

    int a[] = { 5, 6, 7, 8};

   

    for_each(a,a+4,f);//对于数组a的下标[0,4]之间的数据,都执行f

}

 

猜你喜欢

转载自blog.csdn.net/weixin_43917370/article/details/108454527