473,398 Members | 2,380 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

How to remove elements of vector by index

Hello

I have a vector<int(aRemovecoll) which is a list of the indexes to
be removed from another vector. The other vecotr contains an object -
I will call it SomeObject. So the other vecotr is a
vector<SomeObject>.

I have created a function like this so far:

UINT uNumElements = aRemoveColl.GetSize();
vector<T>::reverse_iterator obCurrent = rbegin(); // work from
end back
for (UINT nLoop = uNumElements; nLoop != 0; nLoop--)
{
// Work from end of vecotr to beginning so that iterators are
always valid
intuIndex;
aRemoveColl.GetAt(nLoop, uIndex);
advance(obCurrent, uIndex);
erase(obCurrent);
}

My thinking is that I need to erase from the end backwards because
otherwise the iterator will become invalid. But then advance doesn't
see right. Is there a backward? Or should I be doing this totally
differently?

Jul 23 '07 #1
2 2361
On 23 ec, 12:19, Angus <anguscom...@gmail.comwrote:
Hello

I have a vector<int(aRemovecoll) which is a list of the indexes to
be removed from another vector. The other vecotr contains an object -
I will call it SomeObject. So the other vecotr is a
vector<SomeObject>.

I have created a function like this so far:

UINT uNumElements = aRemoveColl.GetSize();
vector<T>::reverse_iterator obCurrent = rbegin(); // work from
end back
for (UINT nLoop = uNumElements; nLoop != 0; nLoop--)
{
// Work from end of vecotr to beginning so that iterators are
always valid
intuIndex;
aRemoveColl.GetAt(nLoop, uIndex);
advance(obCurrent, uIndex);
erase(obCurrent);
}

My thinking is that I need to erase from the end backwards because
otherwise the iterator will become invalid. But then advance doesn't
see right. Is there a backward? Or should I be doing this totally
differently?
Hi.

You can use iterators, for example:
std::vector<intv;

// Here fill v with some values
// ...

v.erase(v.begin() + 4, v.begin() + 10); // Removes elements from index
4 to 10

Jul 23 '07 #2
On Jul 23, 12:19 pm, Angus <anguscom...@gmail.comwrote:
I have a vector<int(aRemovecoll) which is a list of the indexes to
be removed from another vector. The other vecotr contains an object -
I will call it SomeObject. So the other vecotr is a
vector<SomeObject>.
I have created a function like this so far:
UINT uNumElements = aRemoveColl.GetSize();
vector<T>::reverse_iterator obCurrent = rbegin(); // work from
end back
for (UINT nLoop = uNumElements; nLoop != 0; nLoop--)
{
// Work from end of vecotr to beginning so that iterators are
always valid
intuIndex;
aRemoveColl.GetAt(nLoop, uIndex);
advance(obCurrent, uIndex);
erase(obCurrent);
}
My thinking is that I need to erase from the end backwards because
otherwise the iterator will become invalid.
Not really.
But then advance doesn't see right.
I don't really understand your code. What's GetAt, for example?
The easiest way to do this would be something like:

for ( std::vector< int >::const_iterator it =
aRemoveColl.begin() ;
it != aRemoveColl.end() ;
++ it ) {
data.erase( data.begin() + *it ) ;
}

You don't remove anything from the vector you're iterating over,
so there is no problem.

Note however that this is O(n*m), with n the number of elements
of data, and m the number of elements to be erased. So you
might want to consider generating into a new vector, or even
defining a predicate for remove_if, and using it. (If
aRemoveColl is sorted, something along the lines of:

class SomeObject ;

class IndexIn
{
public:
typedef std::vector< size_t >
IndexTable ;
typedef std::vector< size_t >::const_iterator
IndexIter ;

explicit IndexIn( IndexTable const& indexes )
: myImpl( new Impl( indexes.begin(), indexes.end() ) )
{
}
bool operator()( SomeObject const& ) const
{
return (*myImpl)() ;
}

private:
class Impl
{
public:
Impl( IndexIter begin, IndexIter end )
: myCurrent( begin )
, myEnd( end )
, myIndex( 0 )
{
}
bool operator()()
{
bool result
= myCurrent != myEnd && myIndex == *myCurrent ;
++ myIndex ;
if ( result ) {
++ myCurrent ;
}
return result ;
}
private:
IndexIter myCurrent ;
IndexIter myEnd ;
size_t myIndex ;
} ;

boost::shared_ptr< Impl>
myImpl ;
} ;

// ...
v.erase( std::remove_if( v.begin(), v.end(),
IndexIn( aRemoveColl ) ),
v.end() ) ;

should do the trick.)
Is there a backward? Or should I be doing this totally
differently?
I'd do it differently.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jul 24 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: koperenkogel | last post by:
Dear cpp-ians, I am working with a vector of structures. vector <meta_segment> meta_segm (2421500); and the structure look like: struct meta_segment { float id; float num;
9
by: david wolf | last post by:
I want to delete all even numbers in a vector, I am not sure if there's any better way to do it. Following program is how I did it. Look at the part of code beginning from comments: //delete all...
8
by: imutate | last post by:
I have a std::vector with each element being a class, I push_back elements and then store values in the class object, later I look at these objects and the values are null. In essence: class...
5
by: Wing | last post by:
Hello, I am thinking an efficient way to remove all the elements (says they are less than 10) from a container vector<int>. Any suggestion? Thank you.
6
by: happyvalley | last post by:
Hi, I want to remove some elements from a vector, the following code doesn't work, seems it doesn't allow me to remove an element when iterating the vector. (make sense), just wonder, how to do...
7
by: mohammaditraders | last post by:
Write a program which overloads a binary Minus (+) operator, The program will contain a class Matrix, This class will contain a private data member Array which store int values. The class will...
6
jlandbw04
by: jlandbw04 | last post by:
Okay. Here's the deal. I have this assignment for college that has me completely puzzled. I need this assignment to do the following: 1. input 12 integers into an array from the user. 2. output...
19
by: arnuld | last post by:
/* C++ Primer - 4/e * chapter 4- Arrays & Pointers, exercise 4.28 * STATEMENT * write a programme to read the standard input and build a vector of integers from values that are read....
3
by: n.torrey.pines | last post by:
I'd like to be able to view two contiguous elements of a vector as a pair. Assuming I'm not accessing the last element, of course, and the element type is not bool, when is it safe to do so,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.