C + + array, string manipulation

Source: Internet
Author: User

One, array manipulation

1. Initialization of arrays
1-1 initialization of one-dimensional arrays:
Standard way one: int value[100]; Value[i] has a variable value, not initialized
Standard way two: int value[100] = {+}; Value[0] and Value[1] values of 1 and 2, respectively, and no defined value[i>1]
Pointer mode: int* value = new Int[n]; Not initialized
delete []value; Must not forget to delete the array space
1-2 initialization of two-dimensional arrays:
Standard way one: int value[9][9]; VALUE[I][J] has a variable value, not initialized
Standard way two: int value[9][9] = {{1,1},{2}};//value[0][0,1] and value[1][0] values are initialized, others initialized to 0
Pointer mode one: INT (*value) [n] = new Int[m][n];
delete []value; n must be a constant, call intuitive. Not initialized
2. Array copy
Header file #include<string.h>
int a[10]={34,56,4,10,77,51,93,30,5,52};
int b[10];
memcpy (b,a,sizeof (b));
3. Assigning values to arrays
Header file #include<string.h>
int A[50];
memset (A,0,sizeof (a));
4. Array sorting
Introduction of header File #include<algorithm>
int a[] = {8, 4, 5, 6, 8, 0, 11};
Sort (A, a + 7);
5. Array length
sizeof (Array)/sizeof (array[0]);

Second, string manipulation

C++:

Header file #include<string>

String str;

1. String length

Len = Str.length ();

Len = Str.size ();

2. String comparisons

can be directly compared

You can also:

Str1.compare (STR2);

Str1.compare (POS1,LEN1,STR2,POS2,LEN2); The value is negative, 0, positive.

Nops length to the end.

3. Additional

STR1 + = str2;

Or

Str1.append (STR2);

Str1.append (STR2.POS2,LEN2);

4. String extraction

STR2 = Str1.substr ();

str2 = Str1.substr (POS1);

str2 = Str1.substr (POS1,LEN1);

5. String Search

where = Str1.find (STR2);

where = Str1.find (STR2,POS1); POS1 is starting with the first of the str1.

where = Str1.rfind (STR2); Search from the back forward.

6. Inserting a string

is not an assignment statement.

Str1.insert (POS1,STR2);

Str1.insert (POS1,STR2,POS2,LEN2);

Str1.insert (Pos1,numchar,char); NumChar is the number of insertions, Char is the character to be inserted.

7. Replacing strings

Str1.replace (POS1,STR2);

Str1.replace (POS1,STR2,POS2,LEN2);

8. Delete a string

Str.erase (Pos,len)

Str.clear ();

9. Exchanging strings

Swap (STR1,STR2);

C + + array, string manipulation

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.