c++ - 使用矢量时 C++ 超出范围
问题描述
因此,我通过将节点放入向量中来创建二叉搜索树 (BST)。这些节点存储 3 个值,用户输入int
ID、用户输入int
年龄和用户string
输入名称。
将这些节点插入向量中时,它们按升序存储。
目前我正在使用两个节点。
104 10 鲍勃
102 11 史蒂夫
推回第一个节点时,没有问题;但是,当尝试推回第二个节点时,我收到了向量类抛出的 out_of_bounds 错误。
我认为在尝试切换这两个节点的位置时,我的插入功能有问题,但是我无法准确判断问题出在哪里。
#include "BinaryTree.h"
#include <string>
#include <iostream>
#include <vector>
using namespace std;
int index;
struct Node
{
int ID;
int age;
string name;
Node()
{
}
Node(int id, int Age, string nm)
{
this->ID = id;
this->age = Age;
this->name = nm;
}
};
vector<Node> binaryTree;
BST::BST()
{
}
void BST::start()
{
int choice;
cout << "What would you like to do?" << endl;
cout << "1. Add a node to the tree" << endl;
cout << "2. Delete a node from the tree" << endl;
cout << "3. Find a node in the tree" << endl;
cout << "4. Report the contents of the tree" << endl;
cout << "5. Exit program" << endl;
cin >> choice;
if (choice == 1)
{
insert();
}
if (choice == 3)
{
find();
}
if (choice == 4)
{
report();
}
}
void BST::insert()
{
int ID;
int AGE;
string NAME;
cout << "Please enter the ID number, age and name" << endl;
cin >> ID >> AGE >> NAME;
Node *tree = new Node(ID, AGE, NAME);
if (index == 0)
{
binaryTree.push_back(*tree);
index++;
}
if (index > 0)
{
if ((binaryTree.at(index - 1).ID) < ID)
{
binaryTree.push_back(*tree);
index++;
}
}
if (index > 0)
{
if ((binaryTree.at(index - 1).ID) > ID)
{
Node *temp = new Node();
*temp = binaryTree.at(index - 1);
binaryTree.at(index - 1) = *tree;
binaryTree.at(index) = *temp;
index++;
}
}
cout << "Added! Size: " << binaryTree.size() << endl;
cout << " " << endl;
start();
将不胜感激!谢谢!
解决方案
执行此操作时,您的矢量不会调整大小:binaryTree.at(index) = *tree;
然后push_back()
尝试排序
binaryTree.push_back(*tree;)
std::sort(binaryTree.begin(),binaryTree.end(),[](const Node& n1, const Node& n2){//do your comparations});
或者干脆使用std::set
如果您想使用 std::vector 而不崩溃,那么您的 insert() 必须如下所示:
void BST::insert()
{
int ID;
int AGE;
string NAME;
cout << "Please enter the ID number, age and name" << endl;
cin >> ID >> AGE >> NAME;
//Node *tree = new Node(ID, AGE, NAME); // Don't use new here, there is no need in this
Node tree(ID, AGE, NAME);
binaryTree.push_back(tree);
std::sort(binaryTree.begin(), binaryTree.end(), [](const Node& n1, const Node& n2)
{
//compare your nodes here
return (n1.ID > n2.ID);
});
cout << "Added! Size: " << binaryTree.size() << endl;
cout << " " << endl;
start();
}
但这不会是二叉树。您需要其他数据结构来创建二叉树,std::vector
不能是二叉树。有一个现成的解决方案,看看std::set
,它会插入你需要的元素,你需要添加你的自定义比较功能std::set
,一切都会好起来的。这是std::set
给你的例子:
class Node
{
public:
Node(int id):ID(id){}
int ID;
};
class NodeComparator
{
public:
bool operator()(const Node& n1,const Node& n2)
{
return n1.ID < n2.ID;
}
};
int main()
{
std::set<Node, NodeComparator> set1;
set1.insert(10);
set1.insert(8);
set1.insert(14);
set1.insert(2);
return 0;
}
推荐阅读
- javascript - 通过循环构建 JSON 数组
- python - Python:生成绝对路径
- git - (合并后)在拉取请求中未看到的更改在提交中看到
- macos - IOUSBInterfaceOpen 失败,但我可以通过卸下相机让它工作......这是怎么回事?
- jscript - 如何为 Summernote 的插入图像对话框设置图像扩展名
- python - 在 python 中迭代列表时扩展新列表中的元素
- c# - 使用 C# 请求 AWS EC2 SpotInstance 失败,参数 groupId 的 Value () is invalid
- javascript - 我们如何使用 Puppeteer 编写脚本/自动化 Electron 应用程序?
- angular6 - 带有角度 6 的 Dialogflow 我应该使用 api-ai-javascript 还是基本 url?
- ios - 如何在 iPhone X 上将 ARSCNView 适配到 SafeArea?