[Code] Talking about Pascal to C ++


In On the C ++ Pascal turn , we turn to talk with Pascal C ++.

sacnf () and printf ()

scanf and printf are actually functions provided by the C language, stored in <cstdio>or <stdio.h>header files. In general, the ratio of their speed cinand coutfaster more (if you want faster, use Fast Read and Fast Write ), and can easily control the input and output formats.

#include <bits/stdc++.h>

int main() {
	int x, y;
	scanf("%d%d", &x, &y);   // 读入 x 和 y
	printf("%d\n%d", y, x);  // 输出 y,换行,再输出 x
	return 0;
}

Among them, \nis a newline character, which %dmeans that the variable read or output is a signed integer (int type) variable.

Similarly, common ones are:

%sRepresents the string string (including character arrays).
%cRepresents characters.
%fRepresents a single-precision floating-point number (float).
%lfRepresents a double-precision floating-point number (double).
%lldRepresents a long integer (long long). Depending on the system, it may be% I64d.
%uRepresents unsigned int.
%lluRepresents an unsigned long long (unsigned long long), which may also be% I64u.

In addition to the type identifier, there are some ways to control the format. Many are not commonly used. The two commonly used lists are as follows:

%1dRepresents an integer of length 1. When reading in, you can read in digits one by one even without spaces. In the output, if the specified length is greater than the number of digits, it will be filled with spaces before the number. If the specified length is less than the number of digits, there is no effect.
%.2lfOr %0.2lf, for output, retain 2 decimal places.

Why is there & in scanf (), but not in printf ()?
Here, & is actually an addressing operator, and returns the address of the variable in memory. The parameter received by scanf is the address of the variable. It may be necessary to use pointers to make it completely clear, but now you just need to write it down.

Note: There are no examples
when reading strings and character arrays :

#include <bits/stdc++.h>
using namespace std;

int main() {
	char a[105];
	scanf("%s",a);
	return 0;
}

#define Macro definition

#defineIt is a preprocessing command used to define macros, which is essentially text replacement. E.g:

#include <bits.stdc++.h>
#define N 105;
using namespace std;

int main() {
	cout << N << endl;
	return 0;
}

N is not a variable, but the compiler will replace all N text in the code with 105, but as part of the identifier. N will not be replaced. For example, MN will not be replaced with M105. Similarly, the string will not be replaced.

For example, we can also:

#define re(i,a,b)  for(int i=a; i<=b; i++)
#define ms(i,a) memset(a,i,sizeof(a))
#define LL long long
#define ULL unsigned long long
#define max(a,b)  (((a)>(b)) ? (a):(b))
#define min(a,b)  (((a)<(b)) ? (a):(b))

Harmful, I almost forgot, the trinocular operator has not yet talked about.

Trinocular operator

Why is it called the trinocular operator?
Unary operator: Example: i ++ (only one operand of i)
Binocular operator: Example: a + b (a and b two operands)
Trinacular operator: a? ​​B: c (a, b, c Three operands)

a ? b : c

Equivalent to:

if(a) {
	return b;
} else {
	return c;
}

example:

inline int abs(int a) {
	return a>0 ? a : -a;
}
inline int gcd(int a,int b) {
    return b>0 ? gcd(b,a%b):a;
}

How to write other gcd functions: https://blog.csdn.net/Ljnoit/article/details/99319849

Struct

A struct can be seen as a combination of elements called members.

Can be seen as a custom data type.

Described on this page structis different from the C struct, in C ++ structit is similarly extended to classa class specifier.

Define structure

struct Object {
	int weight;
	int value;
} e[105];

const Object a;
Object b, B[105], tmp;
Object *c;

In the above example defines a named Objectstructure, two members of the elements value, weight, types to int.

In }the definition of the data type for the Objectconstant a, variable b, variables tmp, arrays B, pointers c. For some existing types, you can use this method to define constants, variables, pointers, arrays, etc.

Regarding pointers: Don't be forced to master.

Defining a pointer
If it is a pointer that defines a built-in type, it is the same as the usual definition of a pointer.

If the definition of the structure is a pointer, used in the definition StructName*defined.

struct Edge {
  /*
  ...
  */
  Edge* nxt;
};

The above example is just an example, without having to worry about the actual meaning.

Access / modify member elements
You can use variable names. Member element names for access (where double quotes are not written to the program, the same below).

Such as: the output v var cout << var.vmembers: .

You can also use pointer name-> member element name or use (* pointer name). Member element name to access.

For example: Assign the member element v of the structure pointed to by the structure pointer ptr to tmp: (* ptr) .v = tmp or ptr-> v = tmp.

Why do you need a structure?
First of all, all roads lead to Rome, you can achieve the same effect without using structures. However, the structure can be explicitly member element (typically in competition algorithm variable) bundled together, as in this example Objectstructure, put value, weightput together (meaningful definition of the structure is a Item weight and value). The advantage of this is that it limits the use of member elements.
Imagine if you don't use a structure and there are two arrays value[], Value[]it is easy to write confusion. However, if you use a structure, you can reduce the chance of using a variable error.

And a different structure (structure type, such as Objector (an example of structure, such as the above different structure variable of this structure) earray) can have the same name of the member elements (eg tmp. value, b.value), A member of the element of the same name of each Independent (have its own memory, for example, modify tmp.value will not affect b.valuethe value).
The advantage of this is that you can use the same or similar variables to describe an item. For example, there is value in the Object member variables; we can also define a Carstructure, but also has value that members; if you do not use the structure, perhaps we need to define valueOfObject[], valueOfCar[]such as an array of different names to distinguish.

File operations

Read the file content:

freopen("data.in", "r", stdin);

data.in is the name of the file to be read, and it should be placed in the same directory as the executable file

Output to file:

freopen("data.out", "w", stdout);

data.out is the file name of the output file, which is in the same directory as the executable file

Close standard input / output stream

fclose(stdin);
fclose(stdout);

freopen()The function is in <cstdio>or <fstream>header file.

Namespaces

The C ++ namespace can be used to solve the problem of name conflicts in complex projects.

For example: everything in the C ++ standard library is defined in the std namespace. If you define a variable called cin, you can access the cin variable you defined through cin, and the cin of the standard library through std :: cin Objects without worrying about conflicts.

The following code declares a namespace named A:

namespace A {
	int cnt;
	void f(int x) { cnt = x; }
}  // namespace A

After the declaration, outside this namespace, you can access the f function inside namespace A through A :: f (x).

Namespace declarations can be nested, so the following code is also allowed:

namespace A {
	namespace B {
		void f() { ... }
	}  // namespace B
	
	void f() {
		B::f();  // 实际访问的是 A::B::f(),由于当前位于命名空间 A 内,所以可以省略前面的 A::
	}
}  // namespace A

void f()  //这里定义的是全局命名空间的 f 函数,与 A::f 和 A::B::f 都不会产生冲突
{
  A::f();
  A::B::f();
}

After the namespace is declared, if the members inside the namespace are accessed outside the namespace, they need to be added in front of the member name 命名空间::.

Is there any more convenient way for us to directly access the members in the namespace through the member name? The answer is yes. We can use the usingcommand.

using The instruction has the following two forms:

1 using 命名空间::成员名;.: This instruction allows us to omit the namespace before a member name and access the member directly by the member name, which is equivalent to importing this member into the current scope.

2 using namespace 命名空间;.: This instruction can directly access any member in the namespace through the member name, which is equivalent to importing all members of this namespace into the current scope.
Therefore, if executed using namespace std;, all names in std will be introduced into the global namespace. In this way, we can use cininstead of std::cinusing coutsubstitute std::cout.

Using instructions may cause naming conflicts!

Since the using namespace std;will stdof all the names in the introduction, so if declared with stdvariable or function the same name, it may be because naming conflict causes a compilation error.

Therefore, in the project, it is not recommended using namespacenamespace; instructions.

With the using directive, the code in the C ++ grammar base can have these two equivalents:

#include <iostream>

using std::cin;
using std::cout;
using std::endl;

int main() {
  int x, y;
  cin >> x >> y;
  cout << y << endl << x;
  return 0;
}
#include <iostream>

using namespace std;

int main() {
  int x, y;
  cin >> x >> y;
  cout << y << endl << x;
  return 0;
}

In some problems with multiple subtasks, we can open a namespace for each subtask and define the variables and functions we need to solve the subtask, so that each subtask does not interfere with each other to a certain extent. It is convenient to debug and improve the readability of the program.

Published 106 original articles · praised 156 · 40,000+ views

Guess you like

Origin blog.csdn.net/Ljnoit/article/details/104960875