[String] Functions or keywords for string manipulation in C++

#include <string>
#include <iostream>
using namespace std;
string str,str1,str2;
char sc[];

The strings discussed in this article all refer to the string structure in the header file <string>, so the header file must be included first.

ASCII code (from small to large) 48~57: 0~9 65~90: A~Z 97~122: a~z

【Input and output

cin>>str; // will read the first non-blank character until a space or newline is encountered. So when entering a string, press Enter to complete the input, but if you press a space after entering a string, you can continue to input, but the space after the space will not be read into str.

cin>>str1>>str2;//Read the first non-blank character until a space or a newline and pass it to str1, and then read a non-blank character until a space or a newline and pass it to str2. If only one string is input, the program will Wait for the user to enter the second string.

getline(cin,str); //Store all characters before the carriage return into str, which can be used for string input with spaces.

cout<<str;//Output string.

cout<<str[i];//The i+1th character of the output string, note that i can exceed the effective length by 1 bit , that is, output the last '\0' bit, the effect is nothing. In addition, the ASCII code of '\0' is 0, which is equivalent to NULL. But you can't continue to go beyond, and an error will occur after the end character of the string!

cout<<sc[i];//The character whose distance is i from the first character in the output character array, i can be any number, even if it exceeds the range of the array.

char *sc1=new char[n]; 

or char *sc1=sc;

This is the only use of the char* type, passing the head pointer of the character array to sc1. After that, the operation of sc1 is the same as that of sc. (You cannot pass a string type to a char * type.)

cout<<sc1[i]; //The character in the output character array whose distance is i from the first character, i can be any number, even if it exceeds the range of the array.

Note: When we define a string type, using printf("%s",s1); output will be problematic. This is because '%s' requires the first address of the following object. But string is not such a type. So there must be something wrong.

There is no problem with cout output, if you must printf output. Then you can do this: printf("%s",s1.c_str());


[str-series functions]

extern unsigned int strlen (char *s) It starts scanning from the position char* (can be the beginning of the string, somewhere in the middle, or even some indeterminate memory area) until it hits the first end of string ' \0', then return the counter value (length does not include '\0'). 

char * strcpy (char* dest, const char *src) Copies the string starting from the src address and containing the '\0' terminator to the address space starting with dest , and the return value is of type char*.

extern int strcmp(const char *s1,const char *s2)判断两个字符串s1和s2是否相同,相同返回true ,不同返回false.

[Conversion between strings and character arrays] (error-prone)

Only the conversion between string type and char * type is summarized here, and other equivalent analogies are made.

First, the char* type can be directly assigned to the string type. char* points to a memory address, string will start at this address until it encounters all the '\0'

characters as their own content. (Be careful not to make this section of memory uninitialized, and ensure that there is '\0', otherwise, although the assignment can be successful,

But it will be garbled! )

However, string type cannot be assigned to char * type, because char* is just a pointer.

The string structure itself provides three functions for copying the value to the memory pointed to by char*. They are: (assuming there is already a string str;)

str.data()

str.c_str ()

Both are used the same, but return a constant pointer. Example:

string str=“world”;
const char * p1 = str.c_str ();
const char *p2 = str.data();

copy(p,n,size_type_Off = 0) copies at most n characters from the string type object to the space pointed to by the character pointer p. Default from first character

Start, but can also specify, where to start (remember to start at 0). Returns the character actually copied from the object. The user must ensure that the space pointed to by p

Enough to hold n characters .

Of course, you can also directly assign str[i] to char[i] by using the method of loop assignment.




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325911940&siteId=291194637