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 ).