多集作为类模板,使用列表函数作为基础

Multi Set as a class template using list functions as basis

本文关键字:列表 函数 为基础      更新时间:2023-10-16

程序的基础是定义模板类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;
}

不是最漂亮或最高效的,但它有效。谢谢你的帮助。