文档:http://www.cprogramming.com/tutorial/lesson6.html
1.头文件#include <string.h>是C语言的,而C++要用#include <string>
否则以下程序不能输出字符串:
int main(){ string a = "333ddd"; cout << a << endl; cin.get(); }
2.switch case 语句
注意:case中不能为字符串,只能为int型
int main() { int a = 20; switch (a) { case(10): cout << "im 10" << endl; case(20): cout << "im 20" << endl; default: cout << "im default" << endl; break; } cin.get(); return 0; }
3. 指针,pointer
搞不懂就看文档:http://www.cprogramming.com/tutorial/lesson6.html
或者http://www.weixueyuan.net/view/5847.html
概念:
指针,也是一个变量。它的值存放的是另一个变量的内存地址。
eg:访问变量A有两种方式,一种是直接访问cout << A,
另一种是间接访问(即用A的地址来访问),
int A ; //定义int型变量A
int *p ; //定义指向int型的指针(int表示基类型,*表示变量p是指针类型),为什么要定义基类型,下面有说明。
p = &A; //把A的地址传给p(即变量P的值存放的是A的地址)
cout << p; //输出A的地址
cout << *p; //输出变量A!!!!
定义:
// one pointer, one regular int int *pointer1, nonpointer1; // two pointers int *pointer1, *pointer2;
使用:
#include <iostream> using namespace std; int main() { int x; // A normal integer int *p; // A pointer to an integer p = &x; // Read it, "assign the address of x to p" cin>> x; // Put a value in x, we could also use *p here cin.ignore(); cout<< *p <<" "; // Note the use of the * to get the value cin.get(); }
c programming language:
By definition, the value of a variable or expression of type array is the address of element zero of the array. Thus after the assignment
pa = &a[0];
pa and a have identical values. Since the name of an array is a synonym for the location of the
initial element, the assignment pa=&a[0] can also be written as
pa = a;
Rather more surprising, at first sight, is the fact that a reference to a[i] can also be written as
*(a+i). In evaluating a[i], C converts it to *(a+i) immediately; the two forms are
equivalent.
说明:数组 char a[10],int *p.其实数组中单独就变量名称a而言,它就是一个指针(个人理解为),指向的是第一个元素,所以 p=&a[0]等同于p=a(都表示把第一个元素的地址赋给p);a[1],*(p+1), *(a+1)三个变量都表示第二个元素的value!!!!!
!!!!!!!!!!!p是地址,*p是指向该地址的value!!!!!!!!!!!
但是不能换过来用:
There is one difference between an array name and a pointer that must be kept in mind. A pointer is a variable, so pa=a and pa++ are legal. But an array name is not a variable; constructions like a=pa and a++ are illegal.
eg:
char allocbuf[ALLOCSIZE]; //声明数组
char *allocp = allocbuf; //把数组第一个元素的地址给指针,也就是让指针指向数组的第一个元素
另一个要点:
char *a, *p;
a-p不等于地址,而等于一个int,表示a地址比p地址大的大小。(前提是两个指针指向的是同一个数组!!!)
eg:
char *a, *p;
char array[10];
a = p = array; //也可以写成 a = p = &array[0]
p++;
p-a; //结果等于1
p; //表示array[1]的地址
*p; //表示array[1]的value
另一个要点:
其实一个字符做运算的时候,可以当成是int,而int的值是这个字符对应的阿斯科马!!!!
比如:
char test = 'a'; char test2 = 'b'; int c = test - test2; printf("%d", c); //output:-1
另一个要点:
char *name[] = {"first", "second", "third", "fourth"}; //指针数组,表示每一个元素是一个字符串,等同于二维数组name[4][10]。name[1]可输出second,name[0]或*name可输出first char name[4][10]; char *name[] = {'first', 'second'}; //错误!!!, char name[] = {'f','s'}; //正确,一位数组
基类型:
eg:
int *p;
float *p;
string *p;
其中int float string就是基类型,既然有*表示p是指针变量,为何还要基类型呢,
原因是有p+1,表示p的值+1也就是地址+1,而不同int float string的1不同,比如int是2字节,string 4字节
4.结构体
概念:
有点像类,使用时,也得实例一个对象。
定义:
struct aaa{
int a = 10; //结构体中只能对整形初始化
string b;
}
使用:
struct aaa obj; //struct可不写
#include <iostream> using namespace std; int main() { int x; // A normal integer int *p; // A pointer to an integer p = &x; // Read it, "assign the address of x to p" cin>> x; // Put a value in x, we could also use *p here cin.ignore(); cout<< *p <<" "; // Note the use of the * to get the value cin.get(); }
obj.b = "im a string from struct"; cout << obj.a << obj.b <<endl;
结构体中使用指针:
struct aaa{ int a = 10; } struct aaa obj; struct aaa *pointer; pointer = &obj; cout << pointer->a << endl; //使用-> , 就像类的对象调用方法一样
5.数组
定义:
string box[10]; //必须规定类型和长度
使用:(略)
数组中使用指针:
string box[10]; string *pointer; pointer = &box;
6.方法
定义:
return-type function_name ( arg_type arg1, ..., arg_type argN );