链表C++的语法
Syntax for linked list C++
我正在为我的C++类制作一个链表,我正在查看教授的一些代码:
void List::add(char c)
{
Node *newNode(new Node(c));
if (last == nullptr)
first = last = newNode;
else {
last->next = newNode;
last = newNode;
}
}
int List::find(char c)
{
Node *node(first);
int i(0);
while (node) {
if (node->c == c)
return i;
node = node->next;
i++;
}
return -1;
以下是头文件中的类声明:
class List
{
public:
List(void);
~List(void);
void add(char c);
int find(char c);
bool remove(int index);
int length(void);
friend std::ostream& operator<<(std::ostream& out, List& list);
private:
class Node
{
public:
Node(char c) : c(c), next(nullptr) { }
char c;
Node *next;
};
Node *first;
Node *last;
};
第一个问题:括号是什么意思?正确的使用方法是什么?
Node *newNode(new Node(c));
Node *node(first);
int i(0);
第二个问题:以下是什么意思?
Node(char c) : c(c), next(nullptr) { }
我过去曾使用结构定义过一个节点;语法不同是因为这是一个类吗?
此语句
Node *newNode(new Node(c));
Node *node(first);
int i(0);
相当于
Node *newNode = new Node(c);
Node *node = first;
int i = 0;
Node 类的这个构造函数
Node(char c) : c(c), next(nullptr) { }
使用mem初始值设定项列表: c(c), next(nullptr)
来初始化类的数据成员。即数据成员c
由构造函数参数c
初始化,而数据成员next
由指针文字nullptr
相关文章:
- 1d 智能指针不适用于语法 (*)++
- 助记符和指向成员语法的指针
- 有人能分解一下这个c++模板的语法吗
- C++避免重复声明的语法是什么
- QMetaObject invokeMethod的基于函数指针的语法
- 这个语法std::class<>{}(arg1, arg2) 在C++中是什么意思?
- 为什么包含windows.h会产生语法错误,从而阻止类的实例化?(C2146,C2065)
- 单独定义模板化嵌套类方法的正确语法
- 共享指针和具有自定义删除程序的唯一指针之间的语法差异背后的任何原因
- 错误 C2760:语法错误:映射迭代器上意外的标记"标识符",预期的";"
- 为什么我会收到错误 C2143 语法错误:缺少"*"之前的';'?
- 奇怪的代码抛出编译错误模板< J,int aSize=10> C2143:语法错误:在"<"之前缺少";"
- 使用基类指针调用基类的值构造函数的语法是什么?
- 很好的语法来获取对向量/数组数据的大小引用?
- C++语法运算符功能?
- C++使用 rand 定义函数语法
- 什么文件可以修改 atom 的C++语法?
- 创建模板嵌套类实例的语法?
- C++语法差异:二维和一维数组(指针算术)
- 将信息输入到下面显示的结构向量中的正确语法/格式是什么