C ++ learning [1]

Source: Internet
Author: User

String

1.Several Special Data Types

 

String S ("hello ");
S. Size () = 6 string: size_type (unsigned type)

 

 

Int * P1, * P2;
Ptrdiff_t n = p1-P2; // pointer difference
# Include <cstddef>
Ptrdiff_t (signed INT)

 

 

Vector subscript type: size_type

String subscript type string: size_type

Array subscript type size_t

 

2. String and array

String S1, S2 = "hello ";
S1 = S2; // a string can be directly assigned a value.
Int Ia [] = {0, 1, 2 };
Int ia2 [3];
Ia2 = IA; // array is not allowed

 

 

3. Dynamic Array

Size_t n = get_size (); // The value is available only when an empty array get_size () is dynamically allocated.
Int * P = new int [ N];
 
String * PSA = new string [10]; // string is not of the built-in type. Memory is allocated and elements are initialized.
Int * Pia = new int [10]; // Int Is of the built-in type, memory is allocated, and elements are not initialized (so the const type is incorrect)
Int * Pia = new int [10] (); // force the compiler to initialize

 

 

4. cStyle string

# Include <cstdlib>
Int * Pi = NULL; // null is the C style

 

 

# Incldue <cstring>
Strlen (s) strcmp (S1, S2 )....
Char Ca [] = {'C', '+', '+ '};
Strlen (CA); // dangerous strlen judge the end based on the string Terminator null

 

 

5. Const and pointer

C ++ requires that the pointer to the const object must also have the const feature, but allows the address of a non-const object to be assigned to the pointer to the const object.

Const double Pi = 3.14;
Double * PTR = & PI; // Error
 
Double dval = 3.14;
Const double * cptr;
Cptr = & dval // correct, so the const pointer can point to the object value.

 

6. Differences in auto-increment operations

I ++; // you need to save the original I value.
++ I; // do less work

 

 

Vector <int> ivec;
Int CNT = 10;
While (CNT> 0)
Ivec. pust_back (CNT --);
Vector <int>: iterator iter = ivec. Begin ();
While (ITER! = Ivec. End ())
Cout <* ITER ++ <Endl; // * ITER ++ (ITER ++)

 

 

7. Arrow operators->

C ++ is the synonym used after the dot operator to dereference the operation definition.

(* P). Foo; óp-> Foo;

 

8. sizeof Operator

The sizeof operation on an array is equivalent to the sizeof operation on its element type. The result is multiplied by the number of array elements.

Int SZ = sizeof (IA)/sizeof (* IA); // obtain the number of array elements

 

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.