【问题标题】:Multi Set as a class template using list functions as basis以列表函数为基础的Multi Set作为类模板
【发布时间】:2014-10-14 00:52:02
【问题描述】:

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

当我这样做时,我遇到了将信息从 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 秒。按任意键继续。”

我不知道如何解决这个问题,任何建议将不胜感激。

【问题讨论】:

  • A 是一个空指针。 A 首先没有明显的理由成为指针。
  • 我最初没有尝试将 A 作为指针,但使用 miniMultSet A 并为我得到“对 miniMultSet::miniMultiSet() 的未定义引用的函数使用点”

标签: c++ list class templates set


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    头文件:

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

    不是最漂亮或最有效的,但它确实有效。谢谢大家的帮助。

    【讨论】:

      猜你喜欢
      • 2018-01-21
      • 1970-01-01
      • 2011-07-06
      • 1970-01-01
      • 2021-10-10
      • 1970-01-01
      • 1970-01-01
      • 2017-01-16
      • 1970-01-01
      相关资源
      最近更新 更多