Array and word processing

Source: Internet
Author: User
Tags array definition

One
Array
Data collection, elements, following table

Word processing program handles character data
Character encoding
ascii-English
Character type
Character array

+ Array Definition
Data type array variable name [expression ...];

+size
sizeof (data type name)//evaluates to the number of bytes occupied by the specified data type
Or
sizeof (expression)//evaluates to the number of bytes used by the specified expression result type expression can be a single variable, constant, or array variable, and sizeof evaluates to the number of bytes occupied by that variable, constant, or array variable
sizeof (int) sizeof (double)
sizeof (2.0) sizeof (2+3.5)

+ Access to array elements
Array variable name [subscript 1][subscript 2] ....

+ overall input and output of the array

+ Array Initialization
int x[3]={2,4,6};

//求数组元素最大值、最小值#include <isostream>using namespace std;int main(){  int x[6]={1,4,6,2,5,3};  int max,min;//定义变量max保存最大值 min保存最小值  max=x[0];min=x[0];//先假设第0个元素就是最大值,也是最小值  for(int n=1;n<6;n++)  {    if(x[n]>max)max=x[n];//如果x[n]比max大,则这个值是最大值,修改max    if(x[n]<min)min=x[n];//如果x[n]比min小,则这个值是最小值,修改min  }  cout<<"最大值="<<max<<"最小值="<<min<<endl;//  return 0;}

+ Data Sorting

Second, pointers and arrays
+ Indirect access to array elements through pointer variables
int x[6]={1,4,6,2,5,3};//defines a one-dimensional array variable x and initializes
int p;
p=&x[0];//the pointer variable to the NO. 0 element, the address of the No. 0 element is the first address of the array
cout<<
p;//indirectly accesses the No. 0 element by pointer variable p, showing the result 1
p=&x[1];
cout<<*p;

+ First Address
p=&x[0];//pointing pointer variable p to the first address of the array
P=x;

+ arithmetic operation of pointer variable
Array variables are stored continuously in memory

The int type array pointer variable p points to the next array element p+=4;
Array of type double: p+=8

+ pointer variables and integers for addition and subtraction operations
The result of the expression P±n is still a pointer of type T, with the address value equal to the mean of P: ±n*sizeof (T)
+ Subtract from the same type pointer variable
The result of the expression p1-p2 is int, the value equals: (P1-P2)/sizeof (T)//Subscript Difference
+void Type pointer
Cannot participate in the above calculation
sizeof (void) does not have an exact definition

#include <isostream>using namespace std;int main(){  int x[6]={1,4,6,2,5,3};  int *p,*p1m*p2;    p=&x[0];//将指针变量p指向第0个元素  for(int n=0;n<6;n++)    cout<<*(p+n)<<",";//通过指针变量与整数的算术运算符依次访问各数组元素  int d;  p1=p+1;//将指针变量p1指向第1个元素  p2=p+4;//将指针变量p2指向第4个元素  d=p2-p1;//指针变量之间的差值等于其所指向数组元素下标之间的差值,即4-1  cout<<d<<endl;//显示结果为3  return 0;}

+ pointer-relational operations

+ Pointer Fetch content operation
int x[6]={1,4,6,2,5,3},p=x;
Accessing array elements through pointer operators

p, (p+1),(p+2)
Accessing array elements through the subscript operator []
P[0],P[1],P[2]
At this point the pointer is equivalent to the array name
Accessing array elements by array name
X[0],X[1],X[2]
X,(x+1),(x+2)
Array name equals pointer

+ Dynamic Memory allocation
For uncertain situations

New Delete operator
static assignment int x;x=10;
+ + + dynamic allocation and release of individual variables:
指针变量名=new 数据类型(初始值);
delete指针变量名;

The initial value can be omitted
When the computer executes the new operator, the memory space is allocated and initialized by the number of bytes specified by the data type, and the first address of the allocated memory unit is returned. The first address should be saved to a pre-defined pointer variable in the same type through an assignment statement
When the delete operator is executed, the specified memory unit is released according to the address in the pointer variable

int *p;p=new int;*p=10;delete p;int *p=new int(10);//简化

Dynamic allocation and release of one-dimensional arrays in + +
Pointer variable name =new data type [integer expression];
Delete [] pointer variable name

int *p=new int[5];//动态分配一个int型一维数组变量,包含5个数组元素*(p+1)=10;//通过指针运算符访问第1个元素,向其中写入数据10    //或过下标运算符访问第一个元素: p[1]=10;cout<<*(p+1);//通过指针运算符访问第1个元素,读出数据10并显示出来    //或通过下标运算符访问第一个元素: cout<<p[1];delete []p;//内存使用完后,用delete运算符释放该数组变量所分配的内存空间
#include <ostream>using namespace std;int main(){  int N;  cin>>N;  int *p=new int[N];//动态创建包含N个元素的数组,用于保存数列的前N项  p[0]=0;p[1]=1;//指定数列的前2项  int n;  for(n=2;n<N;n++)//使用循环结构计算出剩余的数列项    p[n]=p[n-1]+p[n-2];//每一项等于其前来2项之和  for(n=0;n<N;n++)  {    cout<<p[n]<<",";    if((n+1)%5==0) cout<<endl;  }  delete[]p;//数组使用结束,动态释放其内存空间  return 0;}

Three, character type
+ word processing
Character encoding
Character
Coded value
ASCII encoding 0-9 from small to large; alphabetic sequential encoding; case-coded, lowercase larger than uppercase 32;0 represents a special character, called a null character
+ Character type
The character type is merged into a single-byte integer type: char, one byte
Char ch=77;
+ character constant ' a ' a '? ' ' @ ' ch= ' M '; equivalent to ch=77
Invisible characters Esc key (27)
Use the escape character form ' \x1b ' ' \33 '
Characters can also be escaped using the form ' M ' (77) Escape form ' \x4d ' \115 '

+ Character-type operations
You can perform arithmetic operations on character data by taking the ASCII value of a character as an integer in the operation

Four, character array and word processing
+ Character array
Char str[10]={' h ', ' e ', ' l ', l ', ' o '};
+ string
""
+ String Constants
"Hello" "A"
End auto-End id = length equals number of characters +1
Char *p;
p= "Hello"; String constants are assigned to a character pointer variable: the first address of a string in memory is assigned to a pointer/pointer to a string

cout<< "Yse\nno"; Can insert escape characters
cout<< "Yse", ' No ' "; "Yse", ' no '

Initialization
Char str[10]={' h ', ' e ', ' l ', l ', ' o '};//uninitialized default 0
Char str[]={' h ', ' e ', ' l ', l ', ' o '};
Char str[10]= "Hello";
Char str[]= "Hello";
Char str[3][10]={};

+ integer input and output of the character array
can be input and output overall
Char str[10];
cin>>str;
cout<<str;

+ Output of pointer variable
int x,*p=&x;
cout<<p<<endl;

Char str[10]= "Hello";
Char *p=str;
cout<<p<<endl;//Display Hello
cout<<p+2<<endl;//Display Llo

cout<< (int *) p<<endl;//show the address in P forced to another type

+ Common word processing algorithm

//检测有多少非0字符#include <iostream>using namespace std;int main(){  char str[10]="hello";  int n=0;//定义int型变量n来保存元素下标,初始化为0  while(str[n]!=‘\0‘)    n++;  cout<<n<<endl;  return 0;}
//字符串插入#include <iostream>using namespace std;int main(){  char str[10]="helo";  char ch=‘l‘;//插入的字符  char oldch;  int n=2;//在哪个位置插入  do  {    oldch=str[n];//插入前要吧当前位置原来的字符先暂存起来,以便后移    str[n]=ch;//ch保存在当前位置    ch=oldch;//oldch转存到ch中,算法含义:将当前字符作为下一次循环时      //将插入在下一元素位置的字符,这样后移操作被转为在下一位置的插入操作    n++;//转入下一元素位置,循环做插入操作  }while(ch!=‘\0‘);//如果ch是结束符,则停止循环  str[n]=‘\0‘;//为插入操作后的字符串添加结束符‘\0‘  cout<<str<<endl;  return 0;}
//字符串拷贝#include <iostream>using namespace std;int main(){  char str1[10]="hello";  char str2[20];//将str1种的字符串拷贝到str2    //注意 定义str2时的数组长度应大于字符串长度,否则会越界  int n=0;//保存元素下标  while(str1[n]!=‘\0‘)//通过循环结构从第0个元素开始拷贝  {    str2[n]=str1[n];//拷贝第n个元素    n++;//下标加1  继续拷贝下一个元素  }  str2[n]=‘\0‘;  cout<<str2<<endl;  return 0;}

Five, Chinese processing
+ man character coding standard such as GB2312
+ Chinese operating system such as Chinese windows
+ Chinese-language application software such as Chinese word

Char str[]= "Hello, hello";
cout<<str<<endl;

+ Character encoding standard
ASCII single-byte character set
Man coded double Byte character set

ANSI: English + Chinese, English + Japanese defects: Not in front of Chinese + Japanese
Unicode encoding: English + chinese + Japanese + ...

Array and word processing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.