多集作为类模板,使用列表函数作为基础
Multi Set as a class template using list functions as basis
程序的基础是定义模板类miniMultiSet的头文件。该类使用列表结构作为multiset的实现结构。类是通过实现定义的类方法在头文件中实现的。
当我这样做的时候,我遇到了从main.cpp向头文件发送信息的问题。没有错误消息,它只是冻结了,我不得不关闭它。这让我觉得我的内存管理有错误。
头文件:
#ifndef MINIMULTISET_H_INCLUDED
#define MINIMULTISET_H_INCLUDED
#include <list>
#include <set>
using namespace std;
template <typename T>
class miniMultiSet
{
public:
typedef typename list<T>::iterator iterator;
typedef typename list<T>::const_iterator const_iterator;
// miniMultiSet iterators are simply list iterators
miniMultiSet();
// default constructor
bool empty() const{return l.empty();}
// is the multiset empty?
int size() const{return l.size();}
// return the number of elements in the multiset
iterator insert(const T& item)
{
l.insert(l.end(), item);
return l.end();
}
// insert item into multi set and return an
// iterator pointing at the new element.
private:
list<T> l;
// multiset implemented using a list
};
#endif // MINIMULTISET_H_INCLUDED
main.cpp
#include <iostream>
#include <list>
#include <set>
#include <algorithm>
#include "miniMultiSet.h"
using namespace std;
int main()
{
miniMultiSet<int> *A;
A=0;
*A->insert(90);
cout << A->size() << endl;
if(A->empty())
cout << "Set is empty." << endl;
else
cout << "Set contains data." << endl;
return 0;
}
我构建了它,并且没有错误语句。当我运行它时,我会得到"程序已停止工作,正在搜索解决方案。"。然后它结束程序,我得到"返回的进程-1073741819(0xC0000005)执行时间:4.421秒。按任意键继续。"
我不知道如何解决这个问题,任何建议都将不胜感激。
您的问题是,您永远无法构造miniMultiSet
,因为您声明了它的默认构造函数,但从未定义过它。您似乎应该简单地删除默认构造函数声明,因为编译器生成的声明可以正常工作,然后在没有指针的情况下执行miniMultiSet<int> A
。
上面的代码被修改为使多集像列表一样操作。
头文件:
#ifndef MINIMULTISET_H_INCLUDED
#define MINIMULTISET_H_INCLUDED
#include <list>
#include <set>
#include <algorithm>
#include <vector>
using namespace std;
template <typename T>
class miniMultiSet
{
public:
typedef typename list<T>::iterator iterator;
typedef typename list<T>::const_iterator const_iterator;
// miniMultiSet iterators are simply list iterators
//miniMultiSet();
// default constructor
bool empty() const{return l.empty();}
// is the multiset empty?
int size() const{return l.size();}
// return the number of elements in the multiset
int count (const T& item)
{
int tally = 0;
std::list<int>::iterator is;
for(is=l.begin();is!=l.end();++is)
{
if(*is==item)
tally++;
}
return tally;
}
// return the number of duplicate occurrences of item
// in the multiset
iterator find (const T& item)
{
std::list<int>::iterator it;
for(it=l.begin();it!=l.end();++it)
{
if(*it==item)
break;
}
return it;
}
// search for item in the multiset and return an iterator
// pointing at the first occurrence matching item, or end()
// if it is not found
const_iterator find (const T& item) const
{
int count=0;
std::list<int>::iterator it;
for(it=l.begin();it!=l.end();++it)
{
if(*it==item)
break;
}
}
// constant version
iterator insert(const T& item)
{
l.insert(l.end(), item);
return l.end();
}
// insert item into multi set and return an
// iterator pointing at the new element.
int erase(const T& item)
{
int count=0;
std::list<int>::iterator it;
std::list<int>::iterator il;
for(it=l.begin();it!=l.end();++it)
{
if(*it==item)
{
it=l.erase((it));
it--;
++count;
}
}
return count;
}
// erase all occurrences of item from the multi set
// and return the number of items erased.
iterator begin(){return l.begin();}
// return an iterator pointing at the first member
// in the multiset
const_iterator begin() const{return l.cbegin();}
// constant version
iterator end(){return l.end();}
// return an iterator pointing just past the last
// member in the muktiset
const_iterator end() const{return l.cend();}
// constant version
private:
list<T> l;
// multiset implemented using a list
};
#endif // MINIMULTISET_H_INCLUDED
主CPP文件
#include <iostream>
#include <list>
#include <set>
#include <algorithm>
#include "miniMultiSet.h"
using namespace std;
int main()
{
miniMultiSet<int> A;
A.insert(80);
A.insert(90);
A.insert(90);
A.insert(90);
A.insert(95);
A.insert(100);
A.insert(105);
A.insert(110);
A.insert(115);
A.insert(120);
if(A.empty())
cout << "Set is empty." << endl;
else
cout << "Set is NOT empty." << endl;
cout << endl;
cout << endl;
cout << "This size of the Multi Set is: " << A.size() << endl;
cout << endl;
cout << endl;
cout << "The first element is: " << *A.begin() << endl;
if(A.find(90)!=A.end())
cout << "90 was found" << endl;
cout << endl;
cout << endl;
cout << "90 was found " << A.count(90) << " times." << endl;
cout << endl;
cout << endl;
//pair<int, int>(90);
cout << "90 was found " << A.erase(90) << " times and erased." << endl;
cout << endl;
cout << endl;
cout << "This size of the Multi Set is: " << A.size() << endl;
cout << endl;
cout << endl;
cout << "90 was found " << A.count(90) << " times." << endl;
return 0;
}
不是最漂亮或最高效的,但它有效。谢谢你的帮助。
相关文章:
- 一对向量构造函数:初始值设定项列表与显式构造
- C++:如何使函数只返回作为列表一部分的字符串
- 错误 没有与参数列表匹配的重载函数"getline"实例
- 运行相同函数名称C++的多个类的列表
- 我使用向量来创建类对象列表.初始化向量时如何使用参数调用构造函数?
- C++模板函数中的初始化 - 新的初始值设定项表达式列表被视为复合表达式
- std::vector 没有重载函数的实例与参数列表匹配
- 从封装在对象中的函数 C++ 返回时为空的列表
- 构造函数/函数声明参数列表中的统一初始化
- C++:继承和列表函数作为类成员的问题
- 我一直在尝试递归中编写一个分类的链接列表函数
- C++列表函数返回列表
- 列表函数未按预期工作
- C++链表/多态性未运行列表函数
- 这是一个具有基于数组的列表函数的数据结构项目,我似乎无法摆脱这个无限循环。
- 多集作为类模板,使用列表函数作为基础
- Boost::mpl类型列表函数应用程序
- 这个变量参数列表函数调用是如何工作的
- C++STL列表函数使用空列表分段
- STL 列表数组无法访问列表函数