C++学习
1 指针:
前面的变量啊,循环啊什么的不记录了。
首先学习的是指针:指针也是一个变量,它能够存储其他变量的地址。
指针变量的声明,在数据类型和变量名称之间使用一个 * 。
例子:
int *npoint;
double *dpoint;
string * spoint;
double dValue=8.9;
dpoint = &dValue;
这里&是一个取地址的符号。
输出指针所指的内容:
cout<<*npoint<<endl;
2 数组:
数组,Array,同一种数据类型的集合。声明:int nArray[10]; 包含了10个整型元素,分别是nArray[0]...nArray[9]。
其实,数组名也是一个指针,只不过是一个常量指针。不能自加自减操作。例如nArray++,nArray--都是非法的。
可以声明一个指针 int *p=nArray; p++则是合法的。
&nArray[0]==nArray;
cout<<*nArray==cout<<nArray[0];
cout<<*(nArray+1)==cout<<nArray[1];
...
那么对于二维的数组怎样使用指针输出元素?
int arr[m][n];
cout<<**arr==cout<<arr[0][0];
cout<<*(*(arr+m-1)+n-1)==cout<<arr[m][n];
3 动态内存:
前面的数组中,数组的声明必须指定数组的大小,否则非法。如果我们想存储一些元素,只有在使用程序的时候才知道需要多大的容量,这是怎么办呢?
cout<<"Input the size of array:"<<endl;
int size;
cin>>size;
int *array=new int[size];
....
delete []array;
注意要释放内存!
同样,可以申请不同类型的内存:
class car;
struct animal;
car *pcar=new car;
animal *panimal=new animal;
举个数据结构的例子,有c++ class实现:
#include<iostream>
#include <cstdlib>
using namespace std;
class mystack{
private:
struct node{
int data;
node *next;
};
node *head;
public:
mystack(){head=NULL;};
void push(int);
int pop();
};
void mystack::push(int x){
node *p=new node;
if(!p){
cout<<"Allocation error!"<<endl;
exit(0);
}
p->data=x;
p->next=NULL;
if(head==NULL){head=p;}
else{
p->next=head;
head=p;}
}
int mystack::pop()
{
node *p=NULL;
if(head==NULL){
cout<<"Stack is empty!"<<endl;
exit(0);
}
else{
p=head;
int temp=p->data;
head=head->next;
delete p;
return temp;
}
}
int main(){
mystack *p=new mystack;
p->push(0);
p->push(9);
p->push(4);
cout<<p->pop()<<endl;
cout<<p->pop()<<endl;
cout<<p->pop()<<endl;
cout<<p->pop()<<endl;
return 0;
}
4 引用:
引用其实就是被引用对象的别名,引用变量和被引用的变量是一样的。
引用的声明:int & rnValue=nValue;
如果rnValue++,则nValue的值也会自增。
引用是一个隐式常数变量,因此必须在声明的时候就要指定引用的变量。而且引用在使用的过程中不能重新定向指向其他变量。
double & rdValue=myDouble;
rdValue=dDouble;//illegal!
5 字符串:
其实,字符串就等同于字符数组。使用字符串时要在头文件中包含<string>
string myString="Hello,World!";
cout<<myString<<endl;
等同于 char myS[]="Hello,World!";
这个输出就要使用循环了。
for(int i=0;i<sizeof(myS)/sizeof(char);i++){
cout<< myS[i];}
cout<<endl;
如果用cout<<*myS<<endl;将输出H。全部输出要用:
for(int i=0;i<sizeof(myS)/sizeof(char);i++){
cout<<*(myS+i);}
cout<<endl;
使用指针:
char *ps="Hello,World!";
cout<<ps<<endl;
6 函数:
C++中的函数用法同C语言的区别不大,出了参数传递可以用引用之外。
参数传递可以是:传值,传指针,传引用。
返回值:各种数据类型,指针,引用。
这里讲一个对指针的引用。
举一个交换两个整形数的例子来说明:
源程序如下:
#include <iostream>using namespace std;
void iswapint(int a,int b) //第一个输出结果,变量的值和地址没有任何变化,传递的是变量的一个拷贝。
{
int t=a;
a=b;
b=t;
}
void iswapref(int &a,int &b)//使用引用,直接对变量操作,地址没有变化,变量的数值变化了。
{
int t=a;
a=b;
b=t;
}
void iswapptr1(int *a,int *b)//使用指针传递,里面的方法,*a等,其实这个叫做间接引用指针。
{
int t=*a;
*a=*b;
*b=t;
}
void iswapptr2(int *a,int *b)//指针传递,同第一个一样,传递的是指针的拷贝,对原来的变量和地址都没有改变。
{
int *t=a;
a=b;
b=t;
}
void iswaptrref1(int *&a,int *&b)//变量的值没有改变,但是改变了变量的地址,因此通过地址找变量的数值就不一样了
//这个相当于有两个房子A和B在广州和香港,里面对应的是一个男人(Y)和一个女人(X),把广州和香港对调,地址变了
//但是A房子还是Y男,B房子还是X女,如果通过地址找则香港是Y男了,而广州是X女了。
{
int *t=a;
a=b;
b=t;
}
void iswaptrref2(int *&a,int *&b)//这个结果是因为上面的函数原因造成的,其实这个跟void iswapptr1(int *a,int *b)//使用指针传递,里面的方法,*a等,其实这个叫做间接引用指针。
{
int t=*a;
*a=*b;
*b=t;
}
int main()
{
int x=5,y=9;
int *ptx=&x;
int *pty=&y;
cout<<"Original value: x= "<<x<<" y= "<<y<<endl;
cout<<"*ptx= "<<*ptx<<" *pty= "<<*pty<<endl;
cout<<"Address: x="<<ptx<<" y= "<<pty<<endl;
iswapint(x,y);
//iswapref(x,y);
//iswapptr1(ptx,pty);
//iswapptr2(ptx,pty);
//iswaptrref1(ptx,pty);
//iswaptrref2(ptx,pty);
cout<<endl;
cout<<"Changed value: x= "<<x<<" y= "<<y<<endl;
cout<<"*ptx= "<<*ptx<<" *pty= "<<*pty<<endl;
cout<<"Address: x="<<ptx<<" y= "<<pty<<endl;
cout<<"Original value: x= "<<x<<" y= "<<y<<endl;
cout<<"*ptx= "<<*ptx<<" *pty= "<<*pty<<endl;
cout<<"Address: x="<<ptx<<" y= "<<pty<<endl;
//iswapint(x,y);
iswapref(x,y);
//iswapptr1(ptx,pty);
//iswapptr2(ptx,pty);
//iswaptrref1(ptx,pty);
//iswaptrref2(ptx,pty);
cout<<endl;
cout<<"Changed value: x= "<<x<<" y= "<<y<<endl;
cout<<"*ptx= "<<*ptx<<" *pty= "<<*pty<<endl;
cout<<"Address: x="<<ptx<<" y= "<<pty<<endl;
cout<<"Original value: x= "<<x<<" y= "<<y<<endl;
cout<<"*ptx= "<<*ptx<<" *pty= "<<*pty<<endl;
cout<<"Address: x="<<ptx<<" y= "<<pty<<endl;
//iswapint(x,y);
//iswapref(x,y);
iswapptr1(ptx,pty);
//iswapptr2(ptx,pty);
//iswaptrref1(ptx,pty);
//iswaptrref2(ptx,pty);
cout<<endl;
cout<<"Changed value: x= "<<x<<" y= "<<y<<endl;
cout<<"*ptx= "<<*ptx<<" *pty= "<<*pty<<endl;
cout<<"Address: x="<<ptx<<" y= "<<pty<<endl;
cout<<"Original value: x= "<<x<<" y= "<<y<<endl;
cout<<"*ptx= "<<*ptx<<" *pty= "<<*pty<<endl;
cout<<"Address: x="<<ptx<<" y= "<<pty<<endl;
//iswapint(x,y);
//iswapref(x,y);
//iswapptr1(ptx,pty);
iswapptr2(ptx,pty);
//iswaptrref1(ptx,pty);
//iswaptrref2(ptx,pty);
cout<<endl;
cout<<"Changed value: x= "<<x<<" y= "<<y<<endl;
cout<<"*ptx= "<<*ptx<<" *pty= "<<*pty<<endl;
cout<<"Address: x="<<ptx<<" y= "<<pty<<endl;
cout<<"Original value: x= "<<x<<" y= "<<y<<endl;
cout<<"*ptx= "<<*ptx<<" *pty= "<<*pty<<endl;
cout<<"Address: x="<<ptx<<" y= "<<pty<<endl;
//iswapint(x,y);
//iswapref(x,y);
//iswapptr1(ptx,pty);
//iswapptr2(ptx,pty);
iswaptrref1(ptx,pty);
//iswaptrref2(ptx,pty);
cout<<endl;
cout<<"Changed value: x= "<<x<<" y= "<<y<<endl;
cout<<"*ptx= "<<*ptx<<" *pty= "<<*pty<<endl;
cout<<"Address: x="<<ptx<<" y= "<<pty<<endl;
cout<<"Original value: x= "<<x<<" y= "<<y<<endl;
cout<<"*ptx= "<<*ptx<<" *pty= "<<*pty<<endl;
cout<<"Address: x="<<ptx<<" y= "<<pty<<endl;
//iswapint(x,y);
//iswapref(x,y);
//iswapptr1(ptx,pty);
//iswapptr2(ptx,pty);
//iswaptrref1(ptx,pty);
iswaptrref2(ptx,pty);
cout<<endl;
cout<<"Changed value: x= "<<x<<" y= "<<y<<endl;
cout<<"*ptx= "<<*ptx<<" *pty= "<<*pty<<endl;
cout<<"Address: x="<<ptx<<" y= "<<pty<<endl;
return 0;
}
输出结果:
Function is: swap(int ,int)
Original value: x= 5 y= 9
*ptx= 5 *pty= 9
Address: x=0xbfd9b448 y= 0xbfd9b444
Changed value: x= 5 y= 9
*ptx= 5 *pty= 9
Address: x=0xbfd9b448 y= 0xbfd9b444
Function is: swap(int &,int &)
Original value: x= 5 y= 9
*ptx= 5 *pty= 9
Address: x=0xbfd9b448 y= 0xbfd9b444
Changed value: x= 9 y= 5
*ptx= 9 *pty= 5
Address: x=0xbfd9b448 y= 0xbfd9b444
Function is: swap(int *,int *)
Original value: x= 9 y= 5
*ptx= 9 *pty= 5
Address: x=0xbfd9b448 y= 0xbfd9b444
Changed value: x= 5 y= 9
*ptx= 5 *pty= 9
Address: x=0xbfd9b448 y= 0xbfd9b444
Function is: swap(int *,int *)
Original value: x= 5 y= 9
*ptx= 5 *pty= 9
Address: x=0xbfd9b448 y= 0xbfd9b444
Changed value: x= 5 y= 9
*ptx= 5 *pty= 9
Address: x=0xbfd9b448 y= 0xbfd9b444
Function is: swap(int *&,int *&)
Original value: x= 5 y= 9
*ptx= 5 *pty= 9
Address: x=0xbfd9b448 y= 0xbfd9b444
Changed value: x= 5 y= 9
*ptx= 9 *pty= 5
Address: x=0xbfd9b444 y= 0xbfd9b448
Function is: swap(int *&,int *&)
Original value: x= 5 y= 9
*ptx= 9 *pty= 5
Address: x=0xbfd9b444 y= 0xbfd9b448
Changed value: x= 9 y= 5
*ptx= 5 *pty= 9
Address: x=0xbfd9b444 y= 0xbfd9b448
7 递归函数:
递归函数很有意思,要讲一下。比如要计算n!我们就这样定义:
#include <iostream>
using namespace std;
double factorial(double n)
{
if(n==0)return 1;
else
{
return n*factorial(n-1);
}
}
int main()
{
cout << "Hello world!" << endl;
cout<<factorial(100)<<endl;
return 0;
}
这个递归函数在二叉树的遍历中使用的最多,而且很方便:
template <class T>
void preorder(T *root){
if(root!==NULL){
cout<<root->data<<"-->"
preorder(root->leftleaf);
preorder(root->rightleaf);
}
}
8 函数执行的顺序:
C++程序的执行是从开始到结束,遇到函数跳转。
int main(int argc,char *argv[]){
for(int i=0;i<10;i++){
if(i<=3)cout<<i<<endl;
cout<<i<<endl;
}
}
将输出:
1
1
2
2
3
3
4
5
6
7
8
9
所以程序是按照顺序执行的!
浙公网安备 33010602011771号