In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "dynamic array and dynamic structure in C++, example analysis of string class". In daily operation, I believe many people have doubts about dynamic array and dynamic structure and string class example analysis in C++. The editor consulted all kinds of data and sorted out simple and useful operation methods. I hope it will be helpful to answer the doubts about "dynamic array and dynamic structure in C++, string class example analysis". Next, please follow the editor to study!
I. dynamic array:
1. The difference between object-oriented programming and traditional procedural programming:
Object-oriented programming focuses on making decisions at run time (not at compile stage). The run phase refers to when the program is running, and the compile phase refers to when the compiler combines the program; a more vivid analogy: the run phase is like when you are on vacation, which scenic spots you choose to visit depends on the weather and your mood (this way is more flexible); while the compilation phase is more like sticking to the previous decision in any case (it is more rigid in this way).
2. Creation of dynamic array:
(1) first of all, you have to figure out what dynamic array, literally, that is, this array is dynamic and controllable, that is, the program-oriented programming we just mentioned, which focuses on the program at run time, which means that we have a dynamic array, and if the programmer really needs it at run time, the programmer can create it. If you don't need it, then programmers don't use it to create it.
(2) create a general writing format for dynamic arrays:
Type_name * pointer_name = new type_name [num_elements]
Let's look at practical examples:
The / / new operator returns the address of the first array element and assigns that address to the pointer p
Int * p = new int [10]
There are also two ways to create dynamic arrays: the first is to use the template class vector;, and the second is to use the template class array (more convenient and more secure)
# include
Using namespace std
Int n
Cin > > n
Vector (int) vi (3)
Vi [0] = 1
Vi [1] = 2
Vi [2] = 3
# include
Using namespace std
Array ai = {1, 2, 3, 4, 5}
(3) reclaim the memory space allocated using the new operator:
Delete [] p
Note: we use malloc to allocate memory size in C language and free to free the allocated memory size.
At the same time, we can see that there is a "[]" here, which is to pay attention to a usage rule: if you use new with "[]", you should also use delete to free memory, and vice versa. The following is an example of incorrect use:
Int * pt = new int
Short * ps = new short [34]
Delete [] pt; / / error
Delete ps; / / error
Second, dynamic structure:
1. Create a dynamic structure:
The concept of dynamic structure is consistent with the concept of dynamic array. Let's now look at how dynamic structures are created:
Inflatable * p = new inflatable
Here, the address of a block of available memory storing the inflatable (representing the structure type) structure is assigned to the pointer p. Practical examples are as follows:
Strcut ti {
Int good
Int bad
}
Ti p = new ti
3. String class:
As you all know, we don't use keywords to represent string data types in C language, but we can use string keywords to express string data types in C++. In the past, if we wanted to represent a string in C language, it was usually expressed in the following way:
Char a [12] = "hello world"
Now we can directly use string to represent a string definition in C++, instead of using the array method:
String str1
String str2 = "hello world"
Str1=str2
Str2 [1] = e
We can see that using string to define a string, you can assign it to another string variable (correctly, it should be an object instantiated by the string class). At the same time, you can access the elements in the string by using array subscript; at the same time, you can also do string merging:
Str3=str1+str2
At this point, the study on "dynamic arrays and dynamic structures in C++, example analysis of string classes" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.