In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about the basic concept of C-style string in C++, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following. I hope you can get something from this article.
C++ programming language as an upgraded version of C language, supports various functions of C language, experienced programmers can easily use this language. Today we will introduce the specific application of C-style strings in C++, so that you can experience the characteristics of C language.
C++ provides two C-style strings and standards for representing strings. Generally speaking, we recommend using the string class as the string class type introduced by C++. But in fact, in many program cases, it is necessary to understand and use the old-fashioned C++ C-style string. We will see an example in Chapter 7. It handles command line options, which are passed to the main () function as a C-style array of strings.
C-style strings originated in the C language and continue to be supported in C++. In fact, before C++, it was the only supported string except for the third-party string library class. Strings are stored in a character array usually manipulated by a pointer of type char*. The standard C library provides a set of functions for manipulating C-style strings in C++, such as:
/ / return the length of the string int strlen (const char*); / / compare whether two strings are equal int strcmp (const char*, const char*); / / copy the second string to * char* strcpy (char*, const char*)
The standard C library is included as part of the standard C++. In order to use these functions, we must include the relevant C header files.
# include
A character pointer to a C-style string in C++ always points to an associated character array, even when we write a string constant, such as:
Const char * st = "The expense of spirit\ n"
Internally, the system also stores string constants in a string array, and then st points to the * elements of the array. So how do we manipulate st in the form of strings? In general, we use the arithmetic operation of the pointer to traverse a C-style string, increasing the pointer by 1 each time until the terminating null character is reached, for example:
While (* st++) {...}
The pointer of type char* is dereferenced and the test points to either true or false. The true value is any character except the empty character (the empty character is not considered false in the judgment statement), and + + is the addition operator that points to the next character in the array. Generally speaking, when we use a pointer, it is necessary to test whether it points to an object before dereferencing it, otherwise the program is likely to fail, for example:
Int string_length (const char * st) {int cnt = 0; if (st) while (* st++) + + cnt; return cnt;} after reading the above, do you have any further understanding of the basic concept of C-style strings in C++? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.