容器类 [英] container class

查看:59
本文介绍了容器类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是书中的问题(Michael Min和Walter Savitch撰写的使用c ++的数据结构和其他对象)

一个袋子只能容纳一个物品的一个副本.例如,本章介绍了一个袋子中装有的物品
数字4和8的两个副本.此袋的行为不同于一组,只能包含一个
任何给定item的副本.编写一个名为"Set"的新容器类,该类类似于一个包,例如
set只能包含任何给定项目的一个副本.您将需要一个常量成员函数,例如:

this is the problem in the book(data structure and other objects using c++ by michael min and walter savitch)

A bag can contain only one copy of an item.For example, the chapter describes a bag contains
the number 4 and two copies of 8.This bag behaviour is different from a set,which can contain only one
copy of any given item.write a new container class called "Set", which is similar to a bag ,exept that a
set can contain only one copy of any given item.You''ll want a constant member function such as this:

bool set::contains(const item& target) const:
//post condition:the return value id the true if targetis is in the set other wise the return will be false 



明确声明集合ADT的不变性.
对每个操作进行时间分析.此时,不需要高效的实现方式
例如,只需将新项目添加到集合中将花费线性时间,因为您需要检查
新项目尚不存在.稍后我们将探索更有效的实现.

您可能还需要添加其他操作来设置ADT,例如减法运算符.

我不知道我的代码中缺少什么.请给我有关如何正确执行操作的想法..
这是书中的问题(由michael min和walter savitch撰写的使用c ++的数据结构和其他对象)

一个袋子只能容纳一个物品的一个副本.例如,本章介绍了一个袋子中装有的物品
数字4和8的两个副本.此袋的行为不同于一组,只能包含一个
任何给定item的副本.编写一个名为"Set"的新容器类,该类类似于一个包,例如
set只能包含任何给定项目的一个副本.您将需要一个常量成员函数,例如:



make an explicit statement of the invariant of the set ADT.
do a time analysis for each operation. At this point, an effecient implementaion is not needed
For the example,just adding a new item to a set will take linear time because you will need to check that the
new item is not already present.Later we wil explore more efficient implimentations.

you may also want to add additional operations to set ADT, such as an operator for subtraction.

i dont kwow whats missing in my code.kindly give me idea on how to do it right..
this is the problem in the book(data structure and other objects using c++ by michael min and walter savitch)

A bag can contain only one copy of an item.For example, the chapter describes a bag contains
the number 4 and two copies of 8.This bag behaviour is different from a set,which can contain only one
copy of any given item.write a new container class called "Set", which is similar to a bag ,exept that a
set can contain only one copy of any given item.You''ll want a constant member function such as this:

bool set::contains(const item& target) const:
//post condition:the return value id the true if targetis is in the set other wise the return will be false 



明确声明集合ADT的不变性.
对每个操作进行时间分析.此时,不需要高效的实现方式
例如,只需将新项目添加到集合中将花费线性时间,因为您需要检查
新项目尚不存在.稍后我们将探索更有效的实现.

您可能还需要添加其他操作来设置ADT,例如减法运算符.

我不知道我的代码中缺少什么.请给我有关如何正确执行操作的想法..



make an explicit statement of the invariant of the set ADT.
do a time analysis for each operation. At this point, an effecient implementaion is not needed
For the example,just adding a new item to a set will take linear time because you will need to check that the
new item is not already present.Later we wil explore more efficient implimentations.

you may also want to add additional operations to set ADT, such as an operator for subtraction.

i dont kwow whats missing in my code.kindly give me idea on how to do it right..

-----------------------------------.h
#ifndef SET_H
#define SET_H
#include <stdlib.h>
   class set
   {

   public:
      static const size_t CAPACITY = 30;
      typedef int Item;
      set(){used=0;}

      void insert(const Item& target);
      void remove(const Item& target);
      size_t size() const {return used;};
      bool contains(const Item& target)const;
      void display();

   private:
      Item data[CAPACITY];
      size_t used;
   };

#endif





-------------------------------.cpp
#include <iostream>
#include <assert.h>
#include <stdlib.h>
#include "set.h"
void set::insert(const Item& entry)
{
   assert(size() &lt; CAPACITY);
   //set::const;
   if(contains(entry==false))
   {
      data[used] = entry;
      used++;
   }
}
bool set::contains(const Item& target)const
   {
      for(int i = 0;i &lt; used;i++ )
      {
         if(data[i]==target)
            return true;
      }
      return false;
    }
void set::display()
{
   if(used==0)
   {
        cout&lt;&lt;"The bag is empty."&lt;&lt;endl;
  }
   else{
    for(size_t i=0; i&lt;used;&gt;      cout&lt;&lt;data[i]&lt;&lt;" ";
    }







---------------------------------------------------source.cpp
#include <iostream>
#include <conio.h>
#include "SET.h"
#include "set.cpp"
int main()
{
   set i;
   //int i;
   cout&lt;&lt; "enter a number:";
   cin&gt;&gt;i;
   //for(i=0; i &lt; used; i++)
   //if(target == data[i])
   //i++;
   set.insert(i);
   set display();
   getch();
   return 0;
}

推荐答案

我刚刚注意到您在课程中使用的是什么文字.你对我表示慰问.它的一个优点是,它是一团沉重的纸张,您应该可以用它威胁讲师,以勒索好分数.作为C ++或数据结构文本,它并不是特别好-我根据我的慷慨程度将其评为次优和无用之间.

如果您不打算将其用于课程,那么我建议您将其退还给卖方,如果您以前有编程经验,请购买Koenig和Moo的"Accelerated C ++"副本.

干杯,



PS:看起来这本书希望您描述类的不变性.一种看待这种情况的方法是编写一个验证对象的函数-即确保它处于可以使用的状态.如果您要研究整个按合同设计",甚至可以在离开任何公共成员函数之前调用此函数,以确保该对象仍然有效.

就您而言,关于唯一不变式,我看到的是项目数不超过30个.

所以对于您的班级...

I''ve just noticed what text you''re using in your course. You have my condolences. It has one advantage in that it''s a pretty heavy lump of paper and you should be able to threaten your lecturer with it to extort a good mark. As a C++ or data structures text it''s not particularly good - I''d rate it somewhere between suboptimal and useless depending on how generous I''m feeling.

If you''re not using it for a course then I suggest you return it to the seller and, if you''ve got previous programming experience, buy a copy of "Accelerated C++" by Koenig and Moo.

Cheers,

Ash

PS: it looks like the book wants you to describe what the invariant of the class is. One way of looking at this is to write a function which validates the object - i.e. makes sure it''s in a state it can be used. if you''re into the whole "design by contract" thing you can even call this function just before you leave any public member functions to make sure that the object is still valid.

In your case about the only invariant I can see is that the number of items is 30 or less.

So for your class...

void bag::check_invariant() const
{
    if( number_of_items_in_bag > 30 )
        throw std::logic_error( "bag''s invariant violated, awwooga!" );
}


if(contains(entry==false))


应该是


is probably supposed to be

if(contains(entry)==false)


甚至


or even

if(!contains(entry))


这篇关于容器类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆