What is the type of a/C + + String function and how is it converted? Examples Show

Source: Internet
Author: User
Tags assert first string int size
The conversion between string functions, first to understand the composition of C + + strings, C + + provides two kinds of string representations: C-style strings and standard C + + introduced by the string class type.

1. C-style string

C-style strings originate from the C language and continue to be supported in C + +. Strings are stored in a character array and are typically manipulated by a pointer to a char* type.

The standard C library provides a set of functions for manipulating C-style strings, such as:

int strlen (const char*); Returns the length of a string

int strcmp (const char*, const char*); Comparison of two strings for equality

char* strcpy (char*, const char*); Copy the second string into the first string

The standard C library is included as part of the standard C + +. To use these functions, we must include the associated C header file #include <cstring>

1.1 Do not call library functions, implement the common basic functions of C-style string

#include <iostream> #include <cstring> #include <cassert>using namespace std;/* returns the string length */int Mystrlen (const char * ch) {assert (ch!=null); int I=0,count=0;const Char *t=ch;//is traversed with a temporary pointer to prevent changes to the original pointer pointing. while (t[i]!= ') {count++;i++;} return count;} /* Copy the second string to the first string and return the first pointer to the string. */char* mystrcpy (char *des,const char *src) {assert ((des!=null) && (src!=null)); int I=0;char *add=des;// Use Add to record the header of the DES. while (src[i]!= ') {des[i]=src[i];i++;} des[i]= ' + '; return add;} /* Compare two strings for equality. equals returns 0, and the previous string returns 1, or 1 if it is smaller than the next. */int mystrcmp (const char *ch1,const char *ch2) {assert ((ch1!=null) && (ch2!=null)); int I=0;const Char *str1=ch1 ;//define two temporary pointers. const char *str2=ch2;while ((str1[i]!= ') | | (str2[i]!= ')) {if (Str1[i]<str2[i]) {return-1;} else if (Str1[i]>str2[i]) {return 1;} else{i++;}} return 0;} int main () {char ch[]= "cavely"; char ch2[]= "Julia"; Cout<<mystrlen (CH) <<endl;//6cout<<mystrcmp (CH, CH2) <<endl;//-1/* The following two sentences cannot be written: Char ch3[100];ch3=mystrcpy (CH,CH2);//The array name is an address "constant". cannot be assigned a value */CHar *ch3;ch3=mystrcpy (CH,CH2); Cout<<ch3<<endl;//juliareturn 0;} 

2.string class type

To use a string type, you must first include the associated header file #include <string>

String str ("Hello"); ① defines a string with an initial value

String str2; ② defining an empty string

String Str3 (str); ③ initializes another string object with a String object

2.1 Basic Operations on string classes:

(1) The length of STR is returned by the size () operation (does not contain a terminating null character), such as the value of Str.size () is 5.

(2) Use the empty () operation to determine if the string is empty, for example: Str2.empty (). If the string does not contain a character, empty () returns the Boolean constant True, otherwise false is returned.

(3) You can also directly use the assignment operator = Copy string, for example: St2 = ST3; Copy the ST3 into the st2.

(4) You can concatenate two or more strings by using the plus operator + or a compound assignment operator that looks a bit weird. For example, give a two string

string S1 ("Hello,");

String S2 ("world\n");

We can concatenate two strings to form a third string as follows.

String s3 = s1 + s2;

You can use the + = operator If you want to attach S2 directly behind the S1

S1 + = s2;

(5) The string type supports accessing a single character through the subscript operator, for example, in the following code snippet, all periods in the string are underlined instead.

String str ("fa.disney.com"); int size = Str.size (); for (int ix = 0; ix < size; ++ix) if (str[ix] = = '. ')    str[IX] = ' _ ';

The implementation of the above code snippet can be replaced by the following statement:

Replace (Str.begin (), Str.end (), '. ', ' _ ');

Replace () is one of the generic algorithms, and the Begin () and end () operations return an iterator to the beginning and end of the string (iterator). Iterators are class abstractions of pointers, provided by the standard library. Replace () scans the characters between Begin () and end (). Each character equal to a period is replaced by an underscore.

2.2 C-style string and string object conversions

The string type can automatically convert a C-style string into a string object. For example, this allows us to assign a C-style string to a single string object.

string S1;

const char *PC = "a character array";

S1 = PC; Ok

However, a reverse conversion cannot be performed automatically. There is no support for implicitly converting a string object to a C-style strings type. For example, attempting to initialize STR with S1 will fail at compile time.

char *str = S1; Compile Time type Error

To implement this conversion, you must explicitly invoke an operation named C_str (). The name C_str () represents the relationship between the string type and the C-style string notation. Literally, give me a C-style string representation-the character pointer to the beginning of the character array. To prevent the character array from being processed directly by the program, C_STR () returns a pointer to a constant array, const char*

Therefore, the correct initialization should be: const char *STR = S1.C_STR (); Ok

Related Article

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.