致力于在C ++中实现用于类分配的自定义映射类。程序可以正常编译,但是一旦程序尝试显示键,它就会崩溃。
Map.h文件
#ifndef MAP_H
#define MAP_H
#include<string>
template<typename K, typename V>
class Map {
public:
Map();
~Map();
V& operator[](const K&);
void traverse(void(*f)(const K&, const V&));
private:
class Node{ //Node Class
public:
Node(K k=K(0), V v=V(0), Node* l = nullptr, Node* r = nullptr) :
Key(k), Value(v), left(l), right(r) {}
K Key;
V Value;
Node* left;
Node* right;
};
Node* head;
//Helper Functions
//
void traverseHelp(void(*f)(const K&, const V&), Node*);
Node* find(Node*, const K&) const;
Node* insert(Node*, const K&);
void deleteNodes(Node*);
};
#include"./Map.cc"
#endif
Map.cc文件
#include<iostream>
#include"Map.h"
#include<string>
using namespace std;
//Map Constructor & Destructor
//
template<typename K, typename V>
Map<K,V>::Map(){
head = nullptr;
}
template<typename K, typename V>
Map<K,V>::~Map(){
deleteNodes(head);
}
template<typename K, typename V>
void Map<K,V>::deleteNodes(Node* start){
if(start->left){
deleteNodes(start->left);
start->left=nullptr;
}
if(start->right){
deleteNodes(start->right);
start->right=nullptr;
}
if(start->left==nullptr && start->right==nullptr){
delete start;
start=nullptr;
}
}
//operator[] and Helpers
//
template<typename K, typename V>
V& Map<K,V>::operator[](const K& keyIn)
{
Node* temp= find(head,keyIn);
if(temp == nullptr)
{
temp = insert(head,keyIn);
cout << "inserted " << temp->Key << endl;
}
cout << "returning " << temp->Value << endl;
return temp->Value;
}
template<typename K, typename V>
typename Map<K,V>::Node* Map<K,V>::insert(Node* start, const K& keyIn){
Node* ptr = start;
if(ptr==nullptr)
{
ptr = new Node(keyIn, 0, nullptr, nullptr);
return ptr;
}
while(true){
if(ptr->Key>keyIn){
if(ptr->left==nullptr){
ptr->left = new Node(keyIn, 0, nullptr, nullptr);
return ptr->left;
}
else{
ptr = ptr->left;
continue;
}
}
else{
if(ptr->right==nullptr){
ptr->right = new Node(keyIn, 0, nullptr, nullptr);
return ptr->right;
}
else{
ptr = ptr->right;
continue;
}
}
}
}
template<typename K, typename V>
typename Map<K,V>::Node* Map<K,V>::find(Node* temp, const K& keyIn ) const
{
Node* ptr = temp;
while(true){
if(ptr==nullptr){
return ptr;
}
if(ptr->Key==keyIn){
return ptr;
}
if(ptr->Key>keyIn){
ptr = ptr->left;
continue;
}
else{
ptr = ptr->right;
continue;
}
}
}
//Traverse function & helper
//
template<typename K, typename V>
void Map<K,V>::traverseHelp(void (*f)(const K& keyIn, const V& valIn), Map<K,V>::Node* root){
cout << "got here 2 " << endl;
if(root->left==nullptr && root->right==nullptr){
cout << "Apply F" << endl;
f(root->Key, root->Value);
}
else if(root->left!=nullptr){
cout << "Traverse left" << endl;
traverseHelp(*f, root->left);
}
else if(root->right!=nullptr){
cout << "Traverse right" << endl;
traverseHelp(*f, root->right);
}
cout << "Got here 3" << endl;
}
template<typename K, typename V>
void Map<K,V>::traverse(void (*f)(const K& keyIn, const V& valIn)){
cout << "got here" <<endl;
traverseHelp(*f, head);
}
freq.cc
#include<iostream>
#include<iomanip>
#include<algorithm>
#include"Map.h"
#include<string>
using namespace std;
void printMap(const string&, const int&);
int main() {
string input;
Map<string, int> words;
//Input for map values
//
cout << "Enter words seperated by a new line. Type -1 to end" << endl;
cin >> input;
while(input != "-1") {
transform(begin(input), end(input), begin(input), ::tolower);
words[input]++;
cout << "Value is now " << words[input] << endl;
cin >> input;
}
//First list sorted alphabetically
//
cout << "\n" << setw(17) << left << "Word" << setw(1) << setw(10) << right << "Frequency" << endl;
cout << setfill('-') << setw(17) << right << " " << setfill(' ') << setw(1) << setfill('-') << setw(10) << left << " " << setfill(' ') << endl;
words.traverse(printMap);
cout << "Traverse completed" << endl;
return 0;
}
//Function that prints the key and value of a node
//
void printMap(const string& key, const int& val){
cout << setw(17) << left << key << setw(1) << setw(10) << right << val << endl;
}
该代码有望输出按频率排序的键列表,但到达“此处2”这一行,然后返回分段错误。
答案 0 :(得分:0)
同样,遇到分段错误时,root是nullptr。
我可能会丢失一些东西,但是您在构造函数中将其设置为nullptr,我看不到它的变化位置。
我认为您认为您有时会在插入中设置它,但是您没有。
即使已修复---如果您不向地图添加任何内容,它也会崩溃,因为在deleteNotes中,您从未检查过nullptr,而是使用析构函数的head作为参数调用它(将为nullptr)。