Question:
C++ Programming. A class containing an array of structs?
mhmm
2013-01-20 22:46:11 UTC
I was asked to add const to the appropriate member functions and replace the std::string with another name with typedef so that the user could change it if they'd like and only in one place. I am suppose to implement the member functions, please do not implement them for me! I just wanted to make sure the const were right. I would like to implement the functions myself :) I just don't know how to handle the array. As you can see I am tempted to put the given constant in there, but that doesn't seem right.

#ifndef MULTISET_H
#define MULTISET_H

typedef std::string ItemType;

const int DEFAULT_MAX_ITEMS = 200;

struct Item
{
ItemType m_item; //these are case sensitive cuMin != cumin
int m_count; //counts if repeated, instead of adding another
};

class Multiset
{
public:

Multiset(); // Create an empty multiset.

bool empty() const; // Return true if the multiset is empty, otherwise false.

int size() const;
// Return the number of items in the multiset. For example, the size
// of a multiset containing "cumin", "cumin", "cumin", "turmeric" is 4.

int uniqueSize() const;
// Return the number of distinct items in the multiset. For example,
// the uniqueSize of a multiset containing "cumin", "cumin", "cumin",
// "turmeric" is 2.

bool insert(const ItemType& value);
// Insert value into the multiset. Return true if the value was
// actually inserted. Return false if the value was not inserted
// (perhaps because the multiset has a fixed capacity and is full).

int erase(const ItemType& value);
// Remove one instance of value from the multiset if present.
// Return the number of instances removed, which will be 1 or 0.

int eraseAll(const ItemType& value);
// Remove all instances of value from the multiset if present.
// Return the number of instances removed.

bool contains(const ItemType& value) const;
// Return true if the value is in the multiset, otherwise false.

int count(const ItemType& value) const;
// Return the number of instances of value in the multiset.

int get(int i, ItemType& value) const;
// If 0 <= i < uniqueSize(), copy into value an item in the
// multiset and return the number of instances of that item in
// the multiset. Otherwise, leave value unchanged and return 0.
// (See below for details about this function.)

void swap(Multiset& other);
// Exchange the contents of this multiset with the other one.

private:
Item m_set[DEFAULT_MAX_ITEMS]; //I did this
int m_totalItems; //this too
};

#endif

My attempt at a constructor. I am sure once I figure out the array this will be much prettier!

Multiset::Multiset() // Create an empty multiset.
{
m_totalItems = 0;

for(int list = 0 ; list < DEFAULT_MAX_ITEMS ; list++ )
{
m_set[list].m_count = 0;
m_set[list].m_item = NULL;
}
}

All kinds of ridiculous I know. Any help with this ARRAY will be much appreciated!

Again do not implement the functions, just need help with the array and maybe some syntax in the constructor! Thank you
Three answers:
David
2013-01-21 05:33:12 UTC
Have you considered using a vector (std::vector) instead of an array? It contains most of the methods you're making, so they would be easier to implement inside your class methods. And yes, your const modifiers are correctly placed, so is your typedef. But maybe it can be moved into the class since that is the only place you use it.
?
2016-12-03 14:28:49 UTC
no longer an extremely theory out theory, yet study values from archives and save them in an array. Have a considerable () function call the procedures on your document coping with type with a view to create an merchandise document and open it for reading.
mariela
2014-11-13 01:56:47 UTC
sophisticated aspect. browse over the search engines. that could help!


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...