Mastermate官网 香港|英国|新加坡|澳大利亚|澳门|深圳硕士研究生申请平台

c++课程 学习小结

今天看了1-7章的c++,写一下个人认为c++和c不同的区别吧~~

c++和c的输入输出函数各具特色,但是以c++输入函数要慢一些。

尤其是对于字符数组的处理:

利用c++对字符数组的处理

#include<iostream>
#include<string.h>
using namespace std;
int main()
{
  char a[20],c[20];
  int len;
  cin.getline(a,20);
  cout<<a[3]<<endl;
  cout.put(a[3])<<endl;
  cout.write(a,3)<<endl;

}

  显示结果:

这就是c++中两个输出函数 cout.put(a[3]); 和cout.write(a,3);

三个输出函数的区别。

对于c++来讲存在着:

直接输入数据 ab cdef这组数据  三种输入语句 char a[20];  //tips 这些输入都不包含'\n';

cin>>a;  类似c语言中的scanf("%s",a);

这种输入方式使得 数组中仅存在ab。遇到空格停止继续输入。

cin.getline(a,20);   类似c语言中的gets(a);

自己仅采用这两个输入函数就可以了注意要添加'\n';

整体输入,遇到'\n'输入停止。(不会包含回车)

建议不要使用cin.get(a,20);对回车的处理自己还没有掌握。

#include<iostream>
#include<string.h>
#include<stdlib.h>
using namespace std;
int main()
{
  char a[20],b[20],c[20];
  int len;
  cin.getline(a,20);
  cin.getline(b,20);
  cin.getline(c,20);
  
  cout<<a<<endl;
  cout<<b<<endl;
  cout<<c<<endl;
  system("pause");
}

 

对于二维数组和指向二维数组的指针问题

经典解释:

用地址法表示二维数组中的某个元素(如a[i][j]),首先要得到该元素所在的行地址a+i;

然后将此行地址转化成理地址*(a+i);再列上变化j,即*(a+i)+j,得到a[i][j]元素的地址;再取值,即*(*(a+i)+j),则得到a[i][j];

*(   *(  a+i  )  +  j )

          -----           //  第i行的首地址(行地址)

      ----------        //转化为列地址,以后变化将在列上变化

      ---------------//第i行第j列元素的地址(即a[i][j]上的地址)

-------------------//a[i][j]i

还有指针数组和数组指针的概念

   前面两个字就是修饰后面两个字;

 指针数组   就是定义一个数组它能存放指针     *p[5]

 数组指针   即使定义一个指针它能指向数组    (*p)[5]

 

对于二维数组而言 只能定义 数组指针来存储,不能用单一的指针变量 *p 来存储。

 

 

#include<iostream>
#include<string.h>
#include<stdlib.h>
using namespace std;
int main()
{
  int a[3][3]={1,2,3,4,5,6,7,8,9},i,j;
  int (*p1)[3],(*p2)[3],(*p3)[3];
  for(i=0;i<3;i++)
  {
   for(j=0;j<3;j++)
   cout<<a[i][j]<<"\t";
   cout<<endl;
  }
  p1=a;
  cout<<"&a[0][0]="<<&a[0][0]<<endl;
  cout<<"p1="<<p1<<endl;
  cout<<"p1+1="<<p1+1<<endl;
  cout<<"*(p1+1)="<<*(p1+1)<<endl;
  cout<<"a[1]="<<a[1]<<endl;
  cout<<"*(*(p1+1))="<<*(*(p1+1))<<endl;
  cout<<"*(p1)+1="<<*(p1)+1<<endl;
  cout<<"a[0]+1="<<a[0]+1<<endl;
  cout<<"*(*(p1))="<<*(*(p1))<<endl;
  system("pause");
}

 

#include<iostream>
#include<string.h>
#include<stdlib.h>
using namespace std;
int main()
{
  int a[3][3]={1,2,3,4,5,6,7,8,9},i,j;
  int *p[3];
  for(i=0;i<3;i++)
  {
   for(j=0;j<3;j++)
   cout<<a[i][j]<<"\t";
   cout<<endl;
  }
  p[1]=&a[0][0];
  p[2]=a[1];
  cout<<*p[1]<<endl;
  cout<<*p[2]<<endl;  
  cout<<*(p[2]+1)<<endl;
  system("pause");
}

数组指针是直接对地址进行取值。

在使用指针进行交换时要注意:

应采用下面的方式:

#include<iostream>
#include<string.h>
#include<stdlib.h>
using namespace std;

void swap(int *p,int *q);
int main()
{
  int a=3,b=2;
  cout<<"a="<<a<<endl;
  cout<<"b="<<b<<endl;
  swap(&a,&b);
  cout<<"a="<<a<<endl;
  cout<<"b="<<b<<endl;
  system("pause");
}
void swap(int *p,int *q)
{
   int temp;
   temp=*p;
   *p=*q;
   *q=temp;     
}

/*void swap(int *p,int *q)   不要使用这种方法因为不知道*temp的地址会给程序带来破坏性影响。
{
   int *temp;
   *temp=*p;
   *p=*q;
   *q=*temp;     
}
*/

  注意引用必须保证参数有一个合法的内存空间。必须是已定义的变量,不能对其进行加减乘等操作。

错误操作 :swap(12,34)  swap(a+4,b+5)  swap(a,b*3) 

 

  

 

 

   

 

 

 

 

 

 

posted @ 2012-06-18 22:20  大嘴鸟  阅读(509)  评论(0编辑  收藏  举报
Mastermate官网 香港|英国|新加坡|澳大利亚|澳门|深圳硕士研究生申请平台