Differences between string strings in c ++ and char */char [] strings in c

Source: Internet
Author: User

Concept Differentiation
In c, there is no data of the string type. However, the header file <string. h> is available in C. It is easy to mistake c for data of the string type.
Differentiate the meaning of string:
1) (IN c and c ++) If string represents string data, c contains string data (declared and defined using char [] or char ). But there is no string data. The function prototype declared in the header file <string. h> in c is all about char array operations, such as strcmp, strcpy, and strcat.
2) (IN c ++) If string represents the string type, it is available in c ++ and not in c. String is a special class in c ++. String is of the standard library type like vector and list. The string type supports variable-length strings. The C ++ standard library manages memory related to storage characters and provides various useful operations.
Required:
# Include <string>
Using std: string;
String class in c ++, for example:
[Cpp]
# Include <iostream>
# Include <string>
Using std: string;
Using namespace std;
Int main (){
String s = "abcdefg ";
String: iterator I; // supports iterator
For (I = s. begin (); I! = S. end (); I ++)
Cout <* I <"; // outputs elements in string s one by one
System ("pause ");
Return 0;
}

Differences from vector containers, for example:
[Cpp]
# Include <iostream>
# Include <vector>
Using namespace std;
Int main ()
{
Vector <string> s (5, "abcdefg ");
Vector <string >:: iterator I;
For (I = s. begin (); I! = S. end (); I ++)
Cout <* I <"; // outputs elements in vector s one by one
System ("pause ");
Return 0;
}


C, refer:
Basic memo: new \ delete, malloc \ free, and memset
String pointer and char pointer Array
Basic memo: basic functions for character array, string, and string processing
Error example:
Int main (int argc, char * argv [])
{
Char * n;
N = new char [20];
N = "Hello World ";
Printf ("% s \ n", n );
Return 0;
}
The above code is incorrect and three of them are pointed out:
First, there is no new in C.
C:
Char * n;
N = (char *) malloc (sizeof (char) * 20 );
Second, the allocated space is in the stack and cannot be directly used to wait for constants in the heap.
N = "Hello, World! "; // Error
It should be strcpy (n, "Hello, World! ");
Third, the space allocated in C/C ++ is to be released.
In C ++, if new, delete is required, where new [] and delete [] are paired, and new and delete are paired.
The memory allocated by malloc in C corresponds to free.
Therefore, free (n) is required in the above Code ). Concept Differentiation
In c, there is no data of the string type. However, the header file <string. h> is available in C. It is easy to mistake c for data of the string type.
Differentiate the meaning of string:
1) (IN c and c ++) If string represents string data, c contains string data (declared and defined using char [] or char ). But there is no string data. The function prototype declared in the header file <string. h> in c is all about char array operations, such as strcmp, strcpy, and strcat.
2) (IN c ++) If string represents the string type, it is available in c ++ and not in c. String is a special class in c ++. String is of the standard library type like vector and list. The string type supports variable-length strings. The C ++ standard library manages memory related to storage characters and provides various useful operations.
Required:
# Include <string>
Using std: string;
String class in c ++, for example:
[Cpp]
# Include <iostream>
# Include <string>
Using std: string;
Using namespace std;
Int main (){
String s = "abcdefg ";
String: iterator I; // supports iterator
For (I = s. begin (); I! = S. end (); I ++)
Cout <* I <"; // outputs elements in string s one by one
System ("pause ");
Return 0;
}

Differences from vector containers, for example:
[Cpp]
# Include <iostream>
# Include <vector>
Using namespace std;
Int main ()
{
Vector <string> s (5, "abcdefg ");
Vector <string >:: iterator I;
For (I = s. begin (); I! = S. end (); I ++)
Cout <* I <"; // outputs elements in vector s one by one
System ("pause ");
Return 0;
}


C, refer:
Basic memo: new \ delete, malloc \ free, and memset
String pointer and char pointer Array
Basic memo: basic functions for character array, string, and string processing
Error example:
Int main (int argc, char * argv [])
{
Char * n;
N = new char [20];
N = "Hello World ";
Printf ("% s \ n", n );
Return 0;
}
The above code is incorrect and three of them are pointed out:
First, there is no new in C.
C:
Char * n;
N = (char *) malloc (sizeof (char) * 20 );
Second, the allocated space is in the stack and cannot be directly used to wait for constants in the heap.
N = "Hello, World! "; // Error
It should be strcpy (n, "Hello, World! ");
Third, the space allocated in C/C ++ is to be released.
In C ++, if new, delete is required, where new [] and delete [] are paired, and new and delete are paired.
The memory allocated by malloc in C corresponds to free.
Therefore, free (n) is required in the above Code ).

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.