C + + arrays, strings, string

Source: Internet
Author: User

1. When defining an array, the number of elements in the array cannot be dynamic and cannot be represented by a variable (the const variable can), which must be known.

2. Referencing an array can only refer to an element in the array and cannot refer to the entire array.

3. When a two-dimensional array is defined, the length of the first dimension can be omitted if all are initialized at the same time.

int a[][3]={1,2,3,4,5,6,7,8,9};

4. Array to do function parameters:

void haha (int array[],int n);
haha (a,10);

The value of the argument changes because the address is passed in.

5. Array of strings:

Char str[]= "Hello";
Char str[]={' h ', ' e ', ' l ', ' l ', ' o ', ' n '};

The above two are equivalent, not equivalent to the following

Char str[]={' h ', ' e ', ' l ', ' l ', ' o '};

The length of the two is different because the last element of the character array may not be '/' and will not be faulted.

6. String processing function, header file: string or String.h

strcat (Char[],const char[]);
strcat (STR1,STR2);

Add str2 to str1 tail, str2 remains unchanged, str1 becomes longer.

strcpy (Char[],const char[],int N);
strcpy (STR1,STR2);
strcpy (str1,str2,2);

Copy the str2 to str1, or copy the first 2 characters of str2 to str1.

strcmp (const char[],const char[]);
strcmp (STR1,STR2);

STR1=STR2, returns 0;

STR1>STR2, returns 1;

STR1<STR2, returns-1.

You cannot directly compare two STR because they are addresses.

strlen (const char[]);
Char str[]= "Hello";
strlen (str);

Returns the length of STR, no ' to ', and therefore 5.
7.string:

String str= "Sdsdsdsds";

string defines a normal variable instead of an array, can be arbitrarily assigned what, or can be used subscript, such as str[3] to access and can be modified, can be directly used with CIN, cout.

String can be directly "=", equivalent to strcpy;

String can be directly "+", equivalent to strcat;

String can be directly "<", ">", equivalent to strcmp;

vs2015, sizeof (string) = 28.

C + + arrays, strings, string

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.