reference operator*()const { return *cur; } pointer operator->()const { return &(operator*()); }
时间: 2024-05-18 12:16:09 浏览: 112
这段代码是C++中重载迭代器的解引用操作,其中:
1. `reference operator*() const` 表示重载了 `*` 操作符,返回值类型为 `reference`,即返回当前指向元素的引用。
2. `pointer operator->() const` 表示重载了 `->` 操作符,返回值类型为 `pointer`,即返回当前指向元素的指针。
这两个操作符是让迭代器对象可以像指针一样使用,方便访问迭代器指向的元素。
相关问题
任务描述 建立一个继承体系,List 是基类,ArrayList 和 LinkedList 是其派生类。并且编写实现下述函数并达到如下效果。 ostream& operator << (ostream&os, const List&rhs); 做一个流输出运算符重载,其第二个参数是List的常引用类型。我们知道子类的对象天生可以作为父类类型使用,因此 ArrayList a; LinkedList b; operator << (cout,a); operator << (cout,b); 这上面的调
用会根据对象的实际类型调用对应的重载函数,从而实现输出不同派生类的对象的功能。下面是代码示例:
```c++
#include <iostream>
#include <vector>
using namespace std;
class List {
public:
virtual void push_back(int val) = 0;
virtual int size() const = 0;
virtual int operator[](int index) const = 0;
};
class ArrayList : public List {
public:
void push_back(int val) override {
data.push_back(val);
}
int size() const override {
return data.size();
}
int operator[](int index) const override {
return data[index];
}
private:
vector<int> data;
};
class LinkedList : public List {
private:
struct Node {
int val;
Node* next;
Node(int v) : val(v), next(nullptr) {}
};
Node* head = nullptr;
int len = 0;
public:
void push_back(int val) override {
if (!head) {
head = new Node(val);
} else {
Node* cur = head;
while (cur->next) {
cur = cur->next;
}
cur->next = new Node(val);
}
++len;
}
int size() const override {
return len;
}
int operator[](int index) const override {
Node* cur = head;
while (index--) {
cur = cur->next;
}
return cur->val;
}
};
ostream& operator << (ostream& os, const List& rhs) {
int n = rhs.size();
os << "[";
for (int i = 0; i < n; ++i) {
if (i) os << ",";
os << rhs[i];
}
os << "]";
return os;
}
int main() {
ArrayList a;
a.push_back(1);
a.push_back(2);
a.push_back(3);
cout << a << endl;
LinkedList b;
b.push_back(4);
b.push_back(5);
b.push_back(6);
cout << b << endl;
return 0;
}
```
输出结果:
```
[1,2,3]
[4,5,6]
```
#include <stdio.h> #include <queue> using namespace std; #define MAX 11 #define INF 0x3f3f3f3f //问题表示 int A[MAX][MAX]={ //一个带权有向图 {0,1,4,INF,INF}, {INF,0,INF,1,5}, {INF,INF,0,INF,1}, {INF,INF,2,0,3}, {INF,INF,INF,INF,INF} }; int n=5; //求解结果表示 int bestlen=INF; //最优路径的路径长度 int bestcount=0; //最优路径的条数 struct NodeType { int vno; //顶点的编号 int length; //当前结点的路径长度 bool operator<(const NodeType &s) const //重载>关系函数 { return length>s.length; } //length越小越优先 };转Java语言
import java.util.*;
public class Main {
private static final int MAX = 11;
private static final int INF = 0x3f3f3f3f;
private static int[][] A = {{0, 1, 4, INF, INF},
{INF, 0, INF, 1, 5},
{INF, INF, 0, INF, 1},
{INF, INF, 2, 0, 3},
{INF, INF, INF, INF, INF}};
private static int n = 5;
private static int bestlen = INF;
private static int bestcount = 0;
private static class NodeType implements Comparable<NodeType> {
int vno;
int length;
@Override
public int compareTo(NodeType s) {
return length - s.length;
}
}
public static void main(String[] args) {
PriorityQueue<NodeType> queue = new PriorityQueue<>();
boolean[][] vis = new boolean[n][1 << n];
queue.offer(new NodeType(){{vno = 0; length = 0;}});
while (!queue.isEmpty()) {
NodeType cur = queue.poll();
if (cur.length > bestlen) {
break;
}
if (cur.vno == n - 1) {
bestlen = cur.length;
bestcount++;
continue;
}
if (vis[cur.vno][1 << cur.vno]) {
continue;
}
vis[cur.vno][1 << cur.vno] = true;
for (int i = 0; i < n; i++) {
if (A[cur.vno][i] != INF && !vis[i][1 << i]) {
queue.offer(new NodeType(){{vno = i; length = cur.length + A[cur.vno][i];}});
}
}
}
System.out.println(bestlen);
System.out.println(bestcount);
}
}
阅读全文