473,395 Members | 1,608 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,395 software developers and data experts.

Vector deletion

15
Hi
I had a doubt about deleting from vectors. I have three vectors of types: void*, bool and int. I insert into these three vectors together and when the vector of bool type has the value true, I want to delete the elements in that position from all these three vectors. Can anyone please help me do that.


Expand|Select|Wrap|Line Numbers
  1. for(unsigned int i =0; i<Ptr.size() ; i++)
  2. {
  3.     if(Bool[i]==1)
  4.     {
  5.         iP=Ptr.begin();
  6.         iP=Ptr.erase(iP+i);
  7.  
  8.         iO=Int.begin();
  9.         iO=Int.erase(iO+i);
  10.  
  11.         iM=Bool.begin();
  12.         iM=Bool.erase(iM+i);
  13.  
  14.     }
  15. }
  16.  
  17.  
iP, iM and iO are iterators. I tried the above method but only one element got deleted. I know why but I don't know how to fix that.
Please help.
Apr 20 '09 #1
5 2711
newb16
687 512MB
What happens if you delete element at index 0? The rest of the vector shifts and what was at index 1 , now is at index 0. Then you advance the counter, and skip the now-first element. Advance it only when element is not deleted.
Apr 20 '09 #2
weaknessforcats
9,208 Expert Mod 8TB
First, a couple of things:

1) a vector<bool> is not a vector. Some wise guy thought he could implement C bitfields in C++ and failed at it. All vectors are required to be arrays. Therefore, you can always assign the address of a vector element to a pointer:

Expand|Select|Wrap|Line Numbers
  1. vector<int> v;
  2. v.push_back(10);
  3. int* ptr = &v[0];
However, this code will not complile for vector<bool>. Refer to Scott Meyers book Effective STL.

You are supposed to use a bitset for bool values.

2) You check a bool for true by testing it to be true and not equal to 1.

bool b = true;

Expand|Select|Wrap|Line Numbers
  1. if (b==true) etc...           //OK
  2. if(b==1) etc...               //not good. May generate warning about coinversion
  3.                                    //of 1 ro bool.
3) Your vectors are changing size.

For example, assume the bools are false true true.
Your loop counter , i, starts at 0. OK.
Nothing is erased. i increments to 1.
Now you erase an element. vector::size is now 2.
i increments to 2.
You exit the loop.

Notice that when vector::size() is 2, the elements of your vectors are 0 and 1.

I suggest you start at vector::begin() and run to vector::end(). You will need to do this until there are no erases. Then you are done.
Apr 20 '09 #3
Banfa
9,065 Expert Mod 8TB
@weaknessforcats
The erase function returns the itterator to the next item in the storage container so I have tended to do this for erase loops

Expand|Select|Wrap|Line Numbers
  1. vector<int> vecint;
  2. vector<int>iterator it;
  3.  
  4. for( it=vecint.begin(); it!=vecint.begin(); )
  5. {
  6.   if (<EraseConditionIsTrue>)
  7.   {
  8.     it = vecint.erase(it);
  9.   }
  10.   else
  11.   {
  12.     it++;
  13.   }
  14. }
  15.  
which voids the need to perform multiple loops checking to see if anything has been erased.


Additionally rather than 3 vectors of basic types it sounds to be like you would be better off with a single vector of a structure or maybe class or a pointer or better yet a handle to one of those.
Apr 20 '09 #4
JosAH
11,448 Expert 8TB
Better erase from the end to the start for reasons that become obvious once you've seen them, e.g. if I have to erase elements 1 and 2 from a vector with elements v0, v1, v2, v3, after erasing element v1 the vector contains v0, v2, v3 and v3 has become the new second element and will also be erased instead of the value v2 (if you erase from front to back).

kind regards,

Jos
Apr 21 '09 #5
vsachar
15
What happens if you delete element at index 0? The rest of the vector shifts and what was at index 1 , now is at index 0. Then you advance the counter, and skip the now-first element. Advance it only when element is not deleted.
Thank you all. It works.

Also, I think I should use a structure instead of 3 vectors. Thanks for that
Apr 25 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Rakesh Sinha | last post by:
This is about the vector template defined in standard C++ . Suppose I want to create a *huge* vector , as follows. void f1() { vector < int > data(1024); //may be not very huge, but for...
34
by: Adam Hartshorne | last post by:
Hi All, I have the following problem, and I would be extremely grateful if somebody would be kind enough to suggest an efficient solution to it. I create an instance of a Class A, and...
9
by: Amadeus W. M. | last post by:
I have a vector from which I want to erase elements between iterators i,j. If i<j, everything works as expected, but if j>i, an insertion is actually performed. Example: vector<double> x(10);...
0
by: rokuingh | last post by:
so what's the deal with a vector of pointers. i know that the pointers after some point can all be invalidated if an insertion or deletion is made or done, and all can be invalidated if the vector is...
11
by: Brian | last post by:
Dear Programmers, I have a class with a pointer to an array. In the destructor, I just freed this pointer. A problem happens if I define a reference to a vector of this kind of class. The...
7
by: JH Programmer | last post by:
Hi, is there any ways that allow us to delete an element in between? say int_val: 1 int_val: 2 int_val: 3
5
by: Alan | last post by:
I was wondering whether it is good programming practice or asking for trouble to modify a vector while iterating through it. That is, I want to do something like the following pseudocode in C++: ...
3
by: =?iso-8859-1?q?Erik_Wikstr=F6m?= | last post by:
I have some code where there's this vector of pointers to objects and I need to delete and erase some of them, the problem is that to know which I need to iterate through the vector and I'm trying...
13
by: prasadmpatil | last post by:
I am new STL programming. I have a query regarding vectors. If I am iterating over a vector using a iterator, but do some operations that modify the size of the vector. Will the iterator recognize...
5
by: cham | last post by:
Hi, I am working on c++ in a linux system ( Fedora core 4 ), kernel version - 2.6.11-1.1369_FC4 gcc version - 4.0.0 20050519 ( Red Hat 4.0.0-8 ) In my code i am creating a vector to store...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.