Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > vector of class

Reply
Thread Tools

vector of class

 
 
xyz
Guest
Posts: n/a
 
      06-30-2008
i have a vector of certain class which has certain parameters
xxx is my class
and here is my vector
std::vector<xxx > yyy;

in my vector i have the data as below:
12 abcde 34567 asdf 1
13 fjggkf 2343 fkjhk 3
12 fgfgfh 33434 fgh 2
34 dgdg 5454 fgfdg 2
.....
now i want to iterate through my vector inorder i have to delete 2nd
line in my vector
here is my iterator which goes through all elements of my vector

std::vector<xxx >::iterator iter;

i could able to do with the integer vector but i have problem with the
vector of certain class
thank you for any help
 
Reply With Quote
 
 
 
 
Daniel T.
Guest
Posts: n/a
 
      06-30-2008
On Jun 30, 12:34*pm, xyz <(E-Mail Removed)> wrote:

> i have a vector of certain class which has certain parameters
> xxx is my class
> and here is my vector
> std::vector<xxx > yyy;
>
> in my vector i have the data as below:
> 12 abcde 34567 asdf 1
> 13 *fjggkf 2343 fkjhk 3
> 12 fgfgfh 33434 fgh 2
> 34 dgdg 5454 fgfdg 2
> ....
> now *i want to iterate through my vector inorder i have to delete 2nd
> line in my vector
> here is my iterator which goes through all elements of my vector
>
> std::vector<xxx >::iterator iter;
>
> i could able to do with the integer vector but i have problem with the
> vector of certain class


You do it the same way.

> thank you for any help


Use the erase-remove idiom. If your class defines an operator== that
can compare two of them for equivelance, then simply:

yyy.erase(remove(yyy.begin(), yyy.end()), yyy.end());

If your class doesn't have an op==, then you will have to define a
predicate that returns true for the value(s) that needs to be removed
and use the remove_if algorithm:

bool mypred(const xxx& left, const xxx& right);
// returns true is left is like right.

yyy.erase(remove_if(yyy.begin(), yyy.end(), &mypred), yyy.end());

You could also use a function object (functor) for "mypred", possibly
even compose one from the standard ones that exist or using something
like the boost lambda library.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Initializing vector<vector<int> > and other vector questions... pmatos C++ 6 04-26-2007 05:39 PM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
how the vector is created, how to pass vector to webservices method apachesoap:Vector Rushikesh Joshi Perl Misc 0 07-10-2004 01:04 PM



Advertisments