hello, i have a problem with these classes. class: I am trying to create objects
ID: 3838568 • Letter: H
Question
hello, i have a problem with these classes.
class: I am trying to create objects of [VectorObjectData], in my class [VectorQueue]
I have tried made an insert function, and i think it works, but when making my remove functions, i cant seem to get the iterator to work
Any help or hints would be appreciated, thanks in advance :)
template
class VectorObjectData
{
public:
VectorObjectData(const T& data = T(), int value = 0)
: dataInsert(data), ValueInsert(value){}
T dataInsert;
int ValueInsert;
};
template
class VectorQueue
{
public:
// Constructor
// clears the vector (so that the size = 0)
VectorQueue()
{
myVector.clear();
}
// Destructor
~VectorQueue()
{
}
// makes an object of the class VectorObjectData
// Inserts data to this object
// Saves the object into myVector
void insert(const T& data, int value)
{
if (ticks > 0) {
VectorObjectData* object = new VectorObjectData;
object->dataInsert = data;
object->ValueInsert = value;
myVector.push_back(*object);
}
}
// Looks through all abjects
// removes the object with the lowest value
void remove(const T& data)
{
for (auto it = myVector.begin(); it != myVector.end(); ++it)
{
//(*it)->...???
}
//remove vector object with lowest value
}
private:
std::vector> myVector;
};
Explanation / Answer
PROGRAM CODE:
#include <iostream>
#include <vector>
using namespace std;
template<class T>
class VectorObjectData
{
public:
VectorObjectData(const T& data = T(), int value = 0)
: dataInsert(data), ValueInsert(value){}
T dataInsert;
int ValueInsert;
};
template<class T>
class VectorQueue
{
public:
// Constructor
// clears the vector (so that the size = 0)
VectorQueue()
{
myVector.clear();
}
// Destructor
~VectorQueue()
{
}
// makes an object of the class VectorObjectData
// Inserts data to this object
// Saves the object into myVector
void insert(const T data, int value)
{
//making this value instead of ticks
if (value > 0) {
VectorObjectData<T> *object = new VectorObjectData<T>;
object->dataInsert = data;
object->ValueInsert = value;
myVector.push_back(*object);
}
}
// Looks through all abjects
// removes the object with the lowest value
void remove(const T& data)
{
int minIndex = 0, count=0;
T min = myVector[0].ValueInsert;
for (auto it = myVector.begin(); it != myVector.end(); ++it)
{
if(*it->ValueInsert < min)
{
min = *it->ValueInsert;
minIndex = count;
}
count++;
}
auto it = myVector.begin();
VectorObjectData<T> data = *(it+minIndex);
// you can find the value and then use erase to delete it from the vector
myVector.erase(std::remove(myVector.begin(), myVector.end(), data), myVector.end());
//remove vector object with lowest value
}
private:
std::vector<VectorObjectData<T>> myVector;
};
int main() {
VectorQueue<int> newVec;
newVec.insert(1, 32);
newVec.insert(2, 53);
newVec.remove(2);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.