The basic concept of C ++ (character string processing)

First, the character string and related

The actual length of the string (10) and the length of the array (12) is not equal to 10 characters in addition to the above storage system for the last two elements of the array of characters to fill the empty automatic character '\0'.

char str [] = "I am happy"; char str [] = { "I am happy"}; and char str [] = { 'I', '', 'a', 'm', '', ' difference; h ',' a ',' p ',' p ',' y '}. The length of the former 11, each having a length of 10.

char str[20];

cin >> str; // array of characters with the name of the input string

cout <

C ++ provides cin stream getline function, for reading a row of characters (or a row of characters in the first several characters), safe and convenient to use.

And the string of characters related to the operation defined in the header file string.h and string.

1, strcat (char [], const char []); // function value obtained by function call, the address is the first character of the array.

2, string copy functions strcpy: strcpy (char [], const char []);

(1) when calling strcpy function, the first parameter must be an array name (e.g. str1), the second parameter may be a character array name may be a string constant.

(2) strcpy function can copy a string of several characters before the character to the array.

(3) can only be achieved by calling strcpy function to assign a string array of characters, rather than using an assignment statement or a string constant array of characters assigned directly to an array of characters.

3、strcmp(const char[],const char[]);

4, strlen (const char []); // strlen is an abbreviation for string length (character string length). It is a function of the length of the test string. Is a function of the actual length of the string, excluding '\0' account.

Second, the character string processing method of C ++ - string class string variable

1, the basic concept

C ++ provides a new data type - string type (string type), in use, and it char, int types, can be used to define the variables, this is the string variable - a character with a name on behalf of sequence. Indeed, the basic type string not having the C ++ language itself, which is a string type declared in the standard C ++ libraries, objects can be defined in this class. Each string variable is an object of the string class.

string string1; // string variable defined string1

string string2 = "China"; // initialize it at the same time the definition of string2

May be assigned to a string variable string constants, may be assigned to another string variable with a variable string. Such as 

string2 = string1; // string2 and assumptions have been defined as a string variable string1, string2 and does not require the same length of string1.

Without specifying the length of the string when defining variables, wherein the length of the string with a length varies.

It may operate on a character string variable, such as 

string word = "Then"; // define and initialize a string variable word

word [2] = 'a'; // Review No. 2, a character, a value modified word "Than"

2, related operations

cin >> string1; // keyboard input from a string to a string variable string1

cout << string2; // output string string2

When character string storage array, string functions use the string operation, such as strcat (connected), strcmp (comparison), strcpy (copy), while the string class object can use these functions, a simple and direct operator.

(1) Copy the string with an assignment

string1=string2;

The effect is "strcpy (string1, string2);" the same.

(2) a string with a plus sign

string string1 = "C ++"; // define string1 and initial value

string string2 = "Language"; // define the initial value and string2

string1 = string1 + string2; // connected string1 and string2

After connecting string1 is "C ++ Language".

(3) the direct string comparison relational operators

Can be used directly == (equal),> (greater than), <(less than),! = (Not equal),> = (greater than or equal to), <= (less than or equal to) relational operators like to a character string Compare.

String may be defined by an array of strings. Such as

string name [5]; // definition of a string array that contains string elements 5

string name [5] = { "Zhang", "Li", "Fun", "Wang", "Tan"}; // define and initialize an array of strings.

Each element is stored in a string array in a string, not a character, which is the difference between an array of strings and an array of characters. Each string contains only the elements and does not include the character string itself '\0'.

In fact, the compiler system is assigned for each 4-byte string variable, in this memory cell, the string itself is not directly stored, but the storage address of the string. In the present example, is the string "Zhang" address is stored in the name [0], the string "Li" is stored in the address name [1], the string "Fun" address is stored in the name [2] ....... Stored in a string variable is a pointer to a string (address of the string).

(supplement)

1. Declare a C ++ string

   Declare a string variable is simple:

   string Str;

    So we declare a string variable, but since it is a class, there are constructors and destructors. The above statement did not pass parameters, so the direct use of the default constructor string, this function does is put Str initialized to an empty string. The constructor and destructor String Class as follows:

a) string s; // creates an empty string s

b) string s (str) // copy constructor generated replica of str

c) string s (str, stridx) // inner string str "starting position stridx" as the initial value of the string portion

d) string s (str, stridx, strlen) // inner string str "begins and a length of at most stridx strlen" as the initial value of the string portion

e) string s (cstr) // string as the initial value of s, C

f) string s (chars, chars_len) // front chars_len character string C as the initial value of the string s.

g) string s (num, c) // generates a string containing a character c num

h) string s (beg, end) // interval to BEG; characters in the end (not included end) as the initial value of the string s

i) s. ~ string () // destroy all the characters, the release of memory

Very simple, I will not explain.

2, string manipulation functions

   Here is the key of C ++ string, I put out a list of various operating functions, do not like to read all those functions can function to find your favorite here, and then to see his detailed explanation later.

a) =, assign () // assigned to a new value

b) swap () // two strings of exchanging content

c) + =, append (), push_back () // add character in the tail

d) insert () // inserting characters

e) erase () // Delete character

f) clear () // delete all characters

g) replace () // replace characters

h) + // string of series

i) ==,! =, <, <=,>,> =, compare () // String Comparison

j) size (), length () // returns the number of characters

k) max_size () // returns the maximum number of characters possible

l) empty () // determines whether the string is empty

m) capacity () // Returns the previous character capacity reallocated

n) reserve () // retain a certain amount of memory to accommodate a certain number of characters

o) [], at () // single character access

p) >>, getline () // read values ​​from a stream

q) << // the value is written stream seek

r) copy () // will be assigned to a certain value C_string

s) c_str () // return the contents C_string

t) data () // returns the content in a form of a character array

u) substr () // return a substring

v) lookup function

w) begin () end () // provide similar support for the STL iterators

x) rbegin () rend () // reverse iterator

y) get_allocator () // Returns configurator

basic_string substr(size_type pos = 0,size_type n = npos) const;

The member function returns an object whose controlled sequence is a copy of 

up to n elements of the controlled sequence beginning at position pos.

const c_str E * () const;

The member function returns a pointer to a nonmodifiable C string 

constructed by adding a terminating null element (E(0)) to the controlled 

sequence. Calling any non-const member function for *this can invalidate the 

pointer.

In summary, C ++ string processing, there are two methods: one method is to use an array of characters, which is taken in the C language method, a method generally referred Cstring; is to define a string variable with the string class, called string method.

 

发布了208 篇原创文章 · 获赞 30 · 访问量 1万+

Guess you like

Origin blog.csdn.net/hopegrace/article/details/104250787