C++知识点
- GCC vs. Clang/LLVM: An In-Depth Comparison of C/C++ Compilers
- Clang on macOS (Using Clang in Visual Studio Code, Compile and debug)
- Using C++ on Linux in VS Code(Compile and debug)
- CMake Tools on Linux
- TOP 70 C++ Interview Questions And Answers
C++来读写文件
The standard input默认是keyboard,The standard output默认是the screen, 于是就要用到standard C++ library:#include <iostream>。其中的cin和cout的函数(endl是结束本行并换行)。
那么如果我们想从读某个文件或者写一个文件呢?也就是输入和输出不是标准的输入和输出。这是由我们要另一个standard C++ library: fstream。
#include <iostream> // 这个也必须要 #include <fstream>
在read或者write一个文件之前,必须先open该文件。首先要确定你希望将文件是什么样的fstream库的数据类型,一共有三种:
- ofstream: 该数据类型代表了 the output file stream ,用于1)新建一个文件, 2)写信息到文件里。
- ifstream:该数据类型代表了the input file stream ,用于读一个文件里的信息
- fstream:该数据类型是前两者数据类型的结合,可用于创建文件,也可以用于写文件,也可以读。
什么时候使用”::”, “->”,以及”.”?
- The three distinct operators C++ uses to access the members of a class or class object, namely the double colon
::
, the dot.
, and the arrow->
, are used for three different scenarios that are always well-defined.- Accessing members(attributes and functions) from an instance(or object) using the dot operator (
.
) - Accessing members(attributes and functions) from a pointer to an object(or created by
new
) using the pointer operator (->
) - Accessing static member functions from class itself without having an object as a handle using the double colon (
::
). [Note: you can also invoke the static member function from a instance with.
or->
which is not recommended]
- Accessing members(attributes and functions) from an instance(or object) using the dot operator (
为什么要使用a pointer to a pointer?
Let’s say you have a pointer pointer1
. Its value is an address. By doing pointer1 = pointer2
, you give pointer1
the address of pointer2
. But! If you do that within a function, and you want the result to persist after the function is done, you need to do some extra work. You need a new pointer3
just to point to pointer1
. Pass pointer3
to the function. So this pointer3
就是a pointer to a pointer. In simple words, Use **
when you want to preserve (OR retain change in) the Memory-Allocation or Assignment even outside of a function call.
为什么linked list要使用一个pointer指向下一个node?
因为Linked List is different from Array( 会自动分配连续的memory给array的每个element,我们之所以a[1]能直接得到第二元素,是因为他根据index,又知道每个元素占据多少memeory(array的数据类型),于是知道去哪个地址取该元素), 但是each item in Linked List(他的设计本来就是为了有dynamic size, 因为定义array你必须先要给定array的size,因此linked list有自由的memory allocation) is separated in the computer memory。那么如何知道下一个node在哪里?不知道地址在哪里,怎么按照地址去取元素呢?所以需要必须要用pointer。
为什么我们可以在一个类里面定义该类的pointer, 不能定义该类的obejct?
参考Stackoverflow回答
why can’t we declare object of a class inside the same class?
why can’t we declare object of a class inside the same class in C++ but it is allowed in Java?
class A
{
A a; // ERROR!
};
class A {
A* a; // Correct!
}
/*
Explaination:
The first snippet of code is wrong in C++. why can't we do this? It may have infinite size, it will turn into an infinite recursion. It can't determine the size of class A, thus it can't determine how much space the member variable a is going to take, therefore it will not compile it.
The second snippet of code is correct in C++! Because it doesn't require knowing the size of class A. Pointers generally have a fixed size, for ex. on a 32-bit executable they're usually 32-bit.
In Java, the first snippet of code can still work. If you have A a;, a is a reference to an object of class A in Java. But in C++, it's not a reference, it's a complete object. So the resulting object in C++ would be of infinite size, which isn't very practical.
*/I