Multi使用列表函数作为基础设置为类模板

时间:2014-10-14 00:52:03

标签: c++ list class templates set

程序的基础是一个头文件,它定义了模板类miniMultiSet。该类使用列表结构作为多集的实现结构。该类通过实现已定义的类方法在头文件中实现。

当我这样做时,我遇到了将main.cpp中的信息发送到头文件的问题。没有错误消息,它只是冻结,我必须关闭它。这让我觉得我的内存管理有误。

HEADER FILE:

#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秒。按任意键继续。”

我不知道如何解决这个问题,我们将非常感谢任何建议。

2 个答案:

答案 0 :(得分:0)

您的问题是您无法构造miniMultiSet,因为您声明了它的默认构造函数但从未定义它。看起来您应该简单地删除默认的构造函数声明,因为编译器生成的声明可以正常工作,然后在没有指针的情况下执行miniMultiSet<int> A

答案 1 :(得分:0)

上面的代码改为使多集合像列表一样运行。

HEADER FILE:

#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;
}

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