类包名称集 [英] class bag name set

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

问题描述

您能帮我使用一个仅显示袋子中那些重复输入之一的袋子吗?它最多可以存储30个输入..我们将使用bool(const item& entry) const.:confused:

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

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

bool set :: contains(const item& target)const:
//后置条件:返回值id如果目标位于集合中则为true,否则返回false

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

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

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

-----------------------------------.h

can you help me to to use a bag namely set that display only one of those repeated inputs in the bag?and it can store up to 30 inputs..instead of using occurrences we will use bool(const item& entry)const.:confused:

[UPDATE]
this is the problem in the book(data structure and other objects using c++ by michael main 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

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 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


-------------------------------.cpp

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

int main()
{

set i;
//int i;
cout<< "enter a number:";
cin>>i;
//for(i=0; i < used; i++)
//if(target == data[i])
//i++;
set.insert(i);
set display();
getch();
return 0;
}



注意:我已经从您的虚假答案中复制了信息,并更新了您的原始问题-Nuri.
[/UPDATE]



NOTE: I''ve copied the information from your fake answers, and updated your original question - Nuri.
[/UPDATE]

推荐答案

很显然,这是您的家庭作业问题之一.除此之外,您编写的方式很少有人能理解它的含义.

这是询问者的期望:
1. 先尝试您要做什么!
2.制定看起来像问题/无法解决的问题.

尝试一下,告诉您是否遇到问题.
成员将很乐意为您提供帮助.
Clearly it''s one of your homework question. Adding to that, the way you have written rarely anyone can get the meaning of it.

Here is what is expected by enquirers:
1. TRY first what you want to do!
2. Formulate what was done by you that looks like an issue/not working.

Try them and tell if you face issues.
Members will be more than happy to help like this.


如果您指的是与Smalltalk含义相同的包,那么您想使用std :: multi 在C ++中设置.

只需将您想要的界面包装在std :: multi 集合上,即可完成工作!

干杯,



编辑时输入了错误的Sodding类名
If you mean a bag in the same sense as Smalltalk then you want to use a std::multiset in C++.

Just wrap the interface you want around std::multiset and job done!

Cheers,

Ash

Edited as I typed the wrong sodding class name


这是一个编程论坛,而不是超心理学的论坛.
至少解释一下上下文.我们每个人都无法读懂您的想法!
你所说的袋子"是什么意思?什么是项目"和输入"?
您想要实现的预期行为是什么?
This is a programming forum, not a parapsychology one.
At least explain the context. None of us is able tor read your mind!
What you mean by "bag"? what are "items" and "inputs"?
What is the expected behavior you want to achieve?


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

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