PDA

View Full Version : No repeted value in QVector



phillip_Qt
23rd February 2009, 06:00
I ve a class like

class mycls
{
QString name;
int rank;
QString type;

};


in a function

Fillvalue(int rank, QString type, QString name)
{
mycls *ptr->rank; //Fiiling rank throuu defined set methods of class
ptr->type, ;
ptr->name;
//craete a vector
QVector(mycls *) vtr = value from member function;
if (vtr != NULL && !vtr->contains(ptr))
{
vtr->append(ptr);
}
}
I need to append only the values which are not their in side vector. that means no element should have same name and rank in the QVector.
But above method is not working fine. Can any bpdy help me??
Thanks

jogeshwarakundi
23rd February 2009, 07:46
Hi this would not obviously work...
you are checking for just the pointer value.
what you should be doing here is to iterate through the values and manually check if the name and rank match! QVector has no way of knowing that those two fields are the "primary key" right!!!

caduel
23rd February 2009, 07:48
your code is not syntactically correct in many places. Please fix it so we can grasp what you are trying to do.

Basically, you have to check if the vector contains an entry with the same rank.
To check that, you have to iterate over the vector


bool has_rank(const QVector<mycls*> &vec, int rank)
{
foreach(const mycls * const ptr, vec)
if (ptr->rank == rank) return true; // assuming you put no NULLs inside

return false;
}

// use like this
if (!has_rank(vec, ptr->rank))
vec.append(ptr);

By the way: are you sure you need to store pointers?

HTH

jpujolf
23rd February 2009, 07:49
have you tried to define operator == on class mycls ?



class mycls
{
QString name;
int rank;
QString type;

operator == ( const mycls & Other ) const
{
return ( name == Other.name ) && ( rank == Other.rank );
};
};


This must work, so contains needs operator == defined. If if no one is defined, the algorithm compares full struct ( I don't know if it does member-by-member or simply pointers ). Any of them is an erroneous comparison if you only want to consider name & rank.

caduel
23rd February 2009, 08:43
operator== does not help if he is storing pointers instead of values.

jpujolf
23rd February 2009, 08:58
Of course :o

I did'nt pay attention to the fact he was using pointers. Sorry.

Anyway, my sample is a clean way to implement if you don't use pointers.

wysota
23rd February 2009, 10:09
I would use QSet instead of QVector and get rid of pointers in favour of a regular value based structure. Mixing C++ and C-ish code is not really advised.


struct MyStruct{
QString name;
int rank;
QString type;
bool operator==(const MyStruct &other) const { return (name==other.name && rank==other.rank && type==other.type); }
bool operator<(const MyStruct &other) const { return ((rank < other.rank) || (rank==other.rank && name < other.name) || (rank==other.rank && name==other.name && type < other.type)); }
};

QSet<MyStruct> mySet;
MyStruct str1;
str1 = ...
mySet << str1;

phillip_Qt
24th February 2009, 06:04
Thank u caduel

bool has_rank(const QVector<mycls*> &vec, int rank)
{
foreach(const mycls * const ptr, vec)
if (ptr->rank == rank) return true; // assuming you put no NULLs inside

return false;
}

// use like this
if (!has_rank(vec, ptr->rank))
vec.append(ptr);
I did this way and it worked.