In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, I would like to share with you about C++ character array, character array pointer and string class how to use the relevant knowledge points, the content is detailed, the logic is clear, I believe most people still know too much about this knowledge, so share this article for your reference, I hope you can get something after reading this article, let's take a look at it.
There are many ways to express strings in C++, according to what you currently know:
Character array
Character array pointer
Standard library string class
The above methods have their own advantages and disadvantages, according to their own point of view, if the task of dealing with strings is relatively simple, then using the first two methods takes up less memory, so it is more practical; if you need to perform functions such as string concatenation and comparison, then it is more appropriate to use the string class, because the character array does not contain processing functions.
1. Character array and pointer to character array
Defining a character array means using the char type. Examples of declaration and initialization of a character array are as follows:
Char duckWords [5] = "Eat"
The syntax for defining a pointer to a character array is as follows, and the character array name still represents the first address:
Char * pointerWords = duckWords
Character arrays and character array pointers are used in exactly the same way as normal array and normal array pointers:
Printf ("% c\ n", duckWords [5]); printf ("% c\ n", * pointerWords)
It is important to note that the string ends with "\ 0", so the string "Say it" actually contains seven characters, because the "\ 0" representing the end mark of the string is automatically added. In addition, string creation contains a variety of syntax, and it is important to initialize without specifying the array length:
Char duckName [6] = {'Delimitation,' David,'d'}; char duckName [6] = "David"; char duckName [] = "David"; 2. Standard library string class
From an object-oriented point of view, the string class is more consistent with string manipulation. It is important to note that string is a class rather than a basic data type.
The functions of the string class are mainly reflected in the following three facets:
Contains multiple constructors, so it can be initialized in a variety of ways
Contains a number of overloaded operators
Several member functions for string processing
The first example below uses the "+" operator for string concatenation:
String duckName = "David"; string duckAge = "12"; string duckDescribe = duckName + duckAge; / string.c_str () is required to output the full string printf ("% s\ n", duckDescribe.c_str ())
The second example is to use the string member function length () for string length statistics:
String duckName = "David"; printf ("% d\ n", duckName.length ())
Of course, there are many operators and member functions overloaded by the string class, but the methods of use are similar and do not belong to the syntax category, so I will not introduce them in detail.
3. Supplement 3.1cm + the common method with string class # include # include using namespace std; int main () {string str1 = "hello"; string* str2 = new string ("hello"); string str3 = "world"; / / get the string length int length = str1.length (); cout
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.