C++ programming basic exercises, necessary to get started

Exercises 1,1 Start with a simple program
C++ programming basic exercises, necessary to get started
1. Comment out the string header file and recompile the program. What will happen?

It has not yet been discovered what will happen.
C++ programming basic exercises, necessary to get started
2. Comment out using namespace std and recompile. What will happen?
C++ programming basic exercises, necessary to get started
3. Change the function name main() to my_main(), and then recompile, what is the result?
C++ programming basic exercises, necessary to get started
Exercise 1.2
to expand the content of the above program (1) require the user to enter the first name and last name at the same time; (2) modify the output result, and print the last name and first name at the same time.
C/C++ learning skirt [105+302+9869], no matter if you are a novice or an advanced person, you can learn about it and learn together! There are development tools in the skirt, a lot of dry goods and technical information to share!
 
1. Define two string objects: string first_name and last_name;
2. Define a vector and store two string objects: vector<string> usr_name(2);
C++ programming basic exercises, necessary to get started
Exercise 1.3
Write a program that can ask the user’s name and read the user What you entered. Please make sure that the name entered by the user is longer than two characters. If the user does enter a valid name, respond with some information.
Please implement it in two ways: the first uses C-style strings, and the second uses string objects.
1. C-style string
First, we must determine the length of user_name; next, use the strlen() function of the standard library to obtain the length of user_name. There is a declaration of strlen() in the cstring header file.
If the length of the string entered by the user is greater than the previously entered character, there is not enough space to store the terminating character (null character). To prevent this from happening, I use the iostream manipulator setw() to ensure that no more than 127 characters will be read. Since the setw() operator is used, the iomanip header file must be included.
C++ programming basic exercises, necessary to get started
2. String object (recommended)
C++ programming basic exercises, necessary to get started
Practice 1.4
Write a program that reads a string of integers from the standard input device, and puts the read integers into array and vector in turn, and then traverses these two containers to obtain numerical synthesis. Output the sum and average value to the standard output device.
 
The difference between the two
 The size of the array must be fixed, and the vector can dynamically expand the storage space as elements are inserted.
 Array does not store its own size.
C++ programming basic exercises, necessary to get started
C++ programming basic exercises, necessary to get started
Exercise 1.5
Use your most comfortable editing tool, enter two lines (or more) of text and save. Then write a program, open the text file, and read each word into a vector<string> object. Traverse the vector and display the content to cout. Then use the generic algorithm sort() to sort all text:
C++ programming basic exercises, necessary to get started
output the sorted result to another file.
C++ programming basic exercises, necessary to get started

Guess you like

Origin blog.51cto.com/14985843/2551435