C + + Standard header file differs from C header file

Source: Internet
Author: User
Tags strcmp

1, Cstdlib is a C + + inside a common header file, equivalent to C in the <stdlib.h>.

2, generally a with ". h" extension of the library file, such as iostream.h. This is the continuation of the C language, in order to be compatible with C. In the new standard library, there is a

Instead of the ". H" extension, the difference is that there are many improvements to the latter, and one thing is that the latter is put into the "Std" namespace.

But string.h a bit special, the problem is that C + + to be compatible with C standard library, C standard library also has a name called "String.h" in the header file, inside

Contains commonly used C string processing functions, such as strcmp. This header file has nothing to do with the C + + string class, so there is no relationship between the <string> and <string.h> two header files.

<cstring> is relative to <string.h> in the C standard library, but running <cstring> needs to add the Std namespace.

Now clarify the difference between the string.h string CString three header files:

<string.h> is the old C header file, corresponding to the char*-based string processing function

<string> is the new string class that corresponds to the C + + header file that wraps the STD

<cstring> is the STD version corresponding to the C header file

Cstring is a class in MFC (it feels like a lot of people are mistaken)

C + + Standard function library is expanded on the basis of C, C + + standard in the inheritance of the C standard, the back of the header file is removed. h, and then added C. For example, the C standard of <stddef.h> to the C + + standard has become cstddef. SIZE_T is defined in the stddef.h.

Here is the comparison:

C Standard Library
    • <assert.h>
    • <complex.h>
    • <ctype.h>
    • <errno.h>
    • <fenv.h>
    • <float.h>
    • <inttypes.h>
    • <iso646.h>
    • < Limits.h>
    • <locale.h>
    • <math.h>
    • <setjmp.h>
    • <signal.h>
    • <stdarg.h>
    • <stdbool.h>
    • <STDDEF.H>
    • <stdint.h>
    • <stdio.h>
    • <stdlib.h>
    • <string.h>
    • <tgmath.h>
    • <time.h>
    • <wchar.h>
    • <wctype.h>
C + + Standard Library
  • Ios
  • iostream
  • Iomanip
  • FStream
  • Sstream
Standard Template Library
  • Vector
  • Deque
  • List
  • Map
  • Set
  • Stack
  • Queue
  • Bitset
  • Algorithm
  • Functional
  • Iterator
c++0x
  • Unordered_map
  • Unordered_set
C Standard Library
  • Cassert
  • Cctype
  • Cerrno
  • Climits
  • Clocale
  • Cmath
  • csetjmp
  • Csignal
  • Cstdarg
  • Cstddef
  • Cstdio
  • Cstdint
  • Cstdlib
  • CString
  • CTime

Reference: http://blog.csdn.net/weitian826/article/details/5995275

What you need to explain here is about C-style strings.

1. String literal value

String literals are constant characters, string literal constants are represented by 0 or more characters enclosed in double quotation marks, and in order to be compatible with the C language, all string literals in C + + have the compiler automatically add a null character at the end.

A. A string does not have a variable name and itself represents itself.

B. Character literals: ' A '

string literal: "A" contains 2 letters A and a null character

C. Links to string literals

D. A string can be assigned directly to a variable, but the memory space directly related to the string is in the read-only part, so it is an array of constant characters.

char* ptr= "Hello";

Prt[0]= ' a ';//This is the wrong compile can pass but the exception occurs when running

We use it when we're using it.

Const char* ptr= "Hello";

Prt[0]= ' a ';//compile to Error

When a string is directly assigned to the initialization of a character array, the string array is stored in the stack and is not allowed to reference memory elsewhere.

Therefore, the compiler copies the string directly into the stack's array memory. Therefore, the corresponding modifications can be made.

Char stactarray[]= "Hello";

Statctarray[0]= ' a ';//compiling and running can be done by

E.c++ style string

When using a C + + style string, think of it as a normal type, such as int, which avoids the problem of understanding the string as a class.

F.C style string

The type of string literal is essentially a const char array, and the C-style string is a null-terminated character array

Const char* cp= "some value";//the compiler automatically appends a null character to the string

while (cp!=null)//Determines whether the currently pointed character is null

{

++CP;
}

Standard library functions for C-style strings <string.h>

#include <cstring>

Strlen (s)//returns the length of S, excluding the string terminator null

strcmp (S1,S2)//When S1<S2 return value <0 when s1=s2 return value =0 when s1>s2 return value >0

strcat (S1,S2)//connect string S2 to S1 and return S1

strcpy (S1,S2)//copy S2 to S1 and return to S1

Strncat (s1, S2, N)//Connect prompt the first n characters of S2 to S1 and back S1

strncpy (s1, S2, N)//Copy the first n characters of S2 to S1 and return to S1

if (CP1 < CP2)//Compare addresses

const char *CP1 = "A string Example";

const char *CP2 = "A different string";

int i=strcmp (CP1, CP2); I>0

I=STRCMP (CP2, CP1); I<0

I=STRCMP (CP1, CP1); I=0

C + + Standard header file differs from C header file

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.