2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
Welcome to Pirate Cat Owl's blog——
It’s been a long hiatus, let’s continue studying hard!
Table of contents
2.C++ Input and Output Functions
In C/C++, there are a large number of variables, functions, and classes that we will learn later. The names of these variables, functions, and classes will all exist in the global scope, which may cause many conflicts. The purpose of using namespaces is to localize the names of identifiers to avoid naming conflicts or name pollution. The appearance of the namespace keyword is to address this problem.
In short, when there are multiple variables with the same name in the same project, it can prevent naming conflicts or name pollution, and it can also make it easier to distinguish. It can be said to be indispensable in the development of large projects.
namespace is the keyword of the namespace. Add the name of the namespace after the keyword and then add {} to limit the scope.
namespace hdmo { int a = 1; }Notice: There is no need to add a ';' after the last '}' of a namespace, which is different from a structure and similar to a function.
The essence of a namespace is that we create a scope that is separate from the global scope. Just like every function has a scope, the scope of parameters in each function is within the scope of the function.The scope of functions and variables in a namespace is limited by the namespace and can only be used by calling the namespace., but it should be noted thatFunction variables in namespaces have a global lifecycle
Namespaces can only be defined in the global scope and can also be nested.
In the same project, namespaces with the same name are considered to be the same namespace.
namespace hdmo { int a = 1; } namespace hdmo { int b = 1; }At this time, the two hdmo represent the same namespace.
The C++ standard library is stored in a namespace called std (standard) to prevent conflicts with functions in other languages. This is why most C++ programs contain a
using namespace std;
It is to include the C++ standard library.
Since a namespace is a domain, if you want to use the functions, variables and other contents in it externally, you need to use an operator to implement this operation like a structure.
The namespace call operator is two colons::
#include <stdio.h> namespace hdmo { int a = 1; } int a = 2; int main() { printf("%dn", a); printf("%dn", ::a); printf("%dn", hdmo::a); return 0; }
From the output results, we can know that when there is nothing in front of ::, the variable in the global scope is called by default.
There are three ways to use variables, functions, etc. in a namespace:
1.Single visit: Use :: to access variables or functions in a namespace individually, which is the safest way to use it;
2.Single expansion: Use using to expand a variable or function separately. After expansion, its scope becomes the global scope and is no longer limited to the namespace. It is recommended to use this method under non-conflicting conditions.
3.Expand all: Directly expand the specified namespace. It is not recommended to use in projects. The risk of conflict is high. It can be used in small programs (the standard library of the C++ standard library is fully expanded, so when we use cin and cout, we do not need to add std:: in front)
In C language, our input and output statements are scanf and printf respectively. C++ is also compatible with the input and output statements of C language, but it also has its own input and output functions, namely cin and cout.
To use the cin and cout functions, you must include<iostream>Standard input and output stream library, and must include the C++ standard function library
- #include <iostream>
- using namespace std;
When using cin, use ' >> ' for input;
When using cout, use '<<' to output.
<< is the stream insertion operator, >> is the stream extraction operator (<< and >> also represent left and right shift operators in C language)
- #include <iostream>
- using namespace std;
- int main()
- {
- int a = 0;
- cin >> a;
- cout << a << endl;
- printf("%dn", a);
- return 0;
- }
We can simply understand endl as a newline 'n'; we will not explain the basic part in detail and will supplement it in future studies.
From the above code, we can see that cin and cout do not use %d, %c, etc. to control the input and output format of variables like scanf and printf. This is becausecin and cout can automatically identify the type of variables and data, this is a relatively convenient place.
In the VS2022 environment, the above code does not contain<stdio.h> , you can also use scanf and prinf functions, because in VS<iostream>Indirectly includes<stdio.h>, so it can be used directly, but in other compilation environments it may be necessary to include<stdio.h> .
When you want to achieve operations such as control accuracy, it is recommended to use the input and output methods of C language directly to achieve it. It will be more troublesome to implement this operation in C++.
It should be noted that scanf and printf are faster than cin and cout. In competitions where the running time is limited, scanf and printf can be used directly. The following code can improve the efficiency of cin and cout, but it will not be explained in this article.
- #include <iostream>
- using namespace std;
- int main()
- {
- // 在io需求⽐较⾼的地⽅,如部分⼤量输⼊的竞赛题中,加上以下3⾏代码
- // 可以提⾼C++IO效率
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- return 0;
- }
A default parameter is a default value given to a function parameter when declaring or defining a function. When we call the function, if the default parameter is not passed a value, the defined default value is used. If a value is passed, the passed value is used.
Default parameters are divided into full default and semi-default. Full default means that all parameters are given default values, and semi-default means that some parameters are given default values. C++ stipulates that semi-default parameters must be given default values consecutively from right to left, and cannot be given default values in intervals.
- #include <iostream>
- using namespace std;
- namespace hdmo
- {
- //全缺省
- int Fun1(int x = 0, int y = 0)
- {
- return x + y;
- }
- //半缺省
- int Fun2(int x, int y = 0)
- {
- return x + y;
- }
- }
-
- int main()
- {
- cout << hdmo::Fun1() << endl;//全缺省可以不传参
- cout << hdmo::Fun1(1) << endl;
- cout << hdmo::Fun1(1,1) << endl;
- /*
- cout << hdmo::Fun2() << endl;//半缺省不能不传参
- */
- cout << hdmo::Fun2(2) << endl;
- cout << hdmo::Fun2(2,2) << endl;
- return 0;
- }
The above code results in:
Semi-default must ensure that the default parameters are defined from the rightmost to the left and the definition is continuous, with normal parameters on the left and default parameters on the right.
Function overloading means that there are multiple functions with the same name at the same time. The same function name represents multiple functions at the same time, which is overloading.
The difference between them is determined by the type and number of parameters, and has nothing to do with the return value type.Two functions with the same name that differ only in the return type cannot be overloaded。
- #include <iostream>
- using namespace std;
- namespace hdmo
- {
-
- int Add(int x, int y)
- {
- return x + y;
- }
- double Add(double x, int y)
- {
- return x + y;
- }
- double Add(int x, double y)
- {
- return x + y;
- }
- double Add(double x, double y)
- {
- return x + y;
- }
- }
- int main()
- {
- using hdmo::Add;
- cout << Add(1, 1) << endl;
- cout << Add(1.1, 1) << endl;
- cout << Add(1, 1.1) << endl;
- cout << Add(1.1, 1.1) << endl;
- return 0;
- }
The result is:
From the above code, we can see that the use of overloading can make it easier for us to use functions. If we want to achieve the above effect in C language, we need to use four different function names to define four functions, and distinguish them when using them. With overloading in C++, we can unify all addition functions into one name, and we don't need to distinguish them when using them. We can use them directly, which is very convenient.
This is the end of this article on the basics of C++. I will continue to explain other basic introductory knowledge later. You are welcome to continue to visit and give me your advice.
If you find any deficiencies, please send me a private message or point them out in the comment section!
Homepage:Pirate Catgull-CSDN Blog
This is Xiaoou! See you next time~(*¯︶¯*)~