473,473 Members | 1,974 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

ways to delete the element in between with vector?

Hi,

is there any ways that allow us to delete an element in between?

say

int_val[1]: 1
int_val[2]: 2
int_val[3]: 3

we want to delete 2 from int_val
so that it becomes

int_val[1]: 1
int_val[2]: 3

I have heard of using iterator with erase()
but it seemed a bit complicated for me

thank you very much.

jacky

Nov 7 '06 #1
7 4317
"JH Programmer" <ho********@gmail.comwrote:
is there any ways that allow us to delete an element in between?

say

int_val[1]: 1
int_val[2]: 2
int_val[3]: 3

we want to delete 2 from int_val
so that it becomes

int_val[1]: 1
int_val[2]: 3

I have heard of using iterator with erase()
but it seemed a bit complicated for me
Do you only want to erase that one element, or do you want to erase all elements with that value?

If the former, then simply "val.erase( val.begin() + pos );" where 'pos' is the index.

If the latter, then use the "erase-remove" idiom: "val.erase( remove( val.begin(), val.end(), 2 ), val.end() );"

--
To send me email, put "sheltie" in the subject.
Nov 7 '06 #2
Thank you so much!

###########################
Here are for others to search for this thread
=================================

If you want to delete a specific element in the vector "array"
you use this method

for (int i = 0; i < data.size(); i++){
if ( data[i] == data_buffer ){
pos = i;
}
}
data.erase(data.begin() + pos );

#############################
"Daniel T. дµÀ£º
"
"JH Programmer" <ho********@gmail.comwrote:
is there any ways that allow us to delete an element in between?

say

int_val[1]: 1
int_val[2]: 2
int_val[3]: 3

we want to delete 2 from int_val
so that it becomes

int_val[1]: 1
int_val[2]: 3

I have heard of using iterator with erase()
but it seemed a bit complicated for me

Do you only want to erase that one element, or do you want to erase all elements with that value?

If the former, then simply "val.erase( val.begin() + pos );" where 'pos' is the index.

If the latter, then use the "erase-remove" idiom: "val.erase( remove( val..begin(), val.end(), 2 ), val.end() );"

--
To send me email, put "sheltie" in the subject.
Nov 7 '06 #3
"JH Programmer" <ho********@gmail.comwrote:
Thank you so much!

###########################
Here are for others to search for this thread
=================================

If you want to delete a specific element in the vector "array"
you use this method

for (int i = 0; i < data.size(); i++){
if ( data[i] == data buffer ){
pos = i;
}
}
data.erase(data.begin() + pos );
Are you sure the above is what you want? It will erase the *last*
element that has the value data_buffer. So for example, given the array
{ 0, 1, 2, 2, 1, 0 } if you want to erase '1' then the above code will
produce { 0, 1, 2, 2, 0 } instead of { 0, 2, 2, 1, 0 }

You are using the loop to find something, so why not put it in a
function called "find". That will make your code more expressive:

template < typename T >
unsigned find( const vector<T>& vec, T buffer )
{
unsigned result = vec.size();
for ( unsigned i = 0; i < vec.size(); ++i )
if ( vec[i] == buffer )
result = i;
return result;
}

The above will return the last element that has the value 'buffer' or
vec.size() if no element has the value.

Of course, going through the whole container is needless, once you find
the element you want, you can stop looking...

template < typename T >
unsigned reverse_find( const vector<T>& vec, T buffer )
{
for ( unsigned i = vec.size(); i 0; --i )
if ( vec[i - 1] == buffer )
return i - 1;
return vec.size();
}

Notice that I'm going through the container backwards because I want to
find the *last* element with the particular value.

It just so happens that the standard already has a function like this,
defined in <algorithm>

vec.erase( find( vec.rbegin(), vec.rend(), buffer ).base() );

The above line will do the same thing your code segment does.

--
To send me email, put "sheltie" in the subject.
Nov 7 '06 #4
Thanks again

Since I am implementing the deletion of username in the login system,
because login name is unique, so it works fine.

but one thing I can think of is that
we can declare another vector array
vector<intpos;

so

if (data[i] == data_buf){
pos.push_back(i);
}

but to delete the elements in the array
we should delete it from the back
[ 0 1 0 2 3 4 1 0 2]
so if we want to delete [2]
pos[1] = 3
pos[2] = 8

for (int i = pos.size(); i >= 0; i--){
data.erase(data.begin() + pos[i]);
}
so delete from the back in vector "data"
will give correct output.
[0 1 0 3 4 1 0]

"Daniel T. дµÀ£º
"
"JH Programmer" <ho********@gmail.comwrote:
Thank you so much!

###########################
Here are for others to search for this thread
=================================

If you want to delete a specific element in the vector "array"
you use this method

for (int i = 0; i < data.size(); i++){
if ( data[i] == data buffer ){
pos = i;
}
}
data.erase(data.begin() + pos );

Are you sure the above is what you want? It will erase the *last*
element that has the value data_buffer. So for example, given the array
{ 0, 1, 2, 2, 1, 0 } if you want to erase '1' then the above code will
produce { 0, 1, 2, 2, 0 } instead of { 0, 2, 2, 1, 0 }

You are using the loop to find something, so why not put it in a
function called "find". That will make your code more expressive:

template < typename T >
unsigned find( const vector<T>& vec, T buffer )
{
unsigned result = vec.size();
for ( unsigned i = 0; i < vec.size(); ++i )
if ( vec[i] == buffer )
result = i;
return result;
}

The above will return the last element that has the value 'buffer' or
vec.size() if no element has the value.

Of course, going through the whole container is needless, once you find
the element you want, you can stop looking...

template < typename T >
unsigned reverse_find( const vector<T>& vec, T buffer )
{
for ( unsigned i = vec.size(); i 0; --i )
if ( vec[i - 1] == buffer )
return i - 1;
return vec.size();
}

Notice that I'm going through the container backwards because I want to
find the *last* element with the particular value.

It just so happens that the standard already has a function like this,
defined in <algorithm>

vec.erase( find( vec.rbegin(), vec.rend(), buffer ).base() );

The above line will do the same thing your code segment does.

--
To send me email, put "sheltie" in the subject.
Nov 7 '06 #5
"Daniel T." <da******@earthlink.netwrote in message
news:da****************************@news.west.eart hlink.net...
: It just so happens that the standard already has a function like this,
: defined in <algorithm>
:
: vec.erase( find( vec.rbegin(), vec.rend(), buffer ).base() );
: The above line will do the same thing your code segment does.

No! Caveat: &*revIter != &*(revIter.base()) !!

The reverse iterators had to bee shifted, because for example:
rbegin().base() == end()
but *rbegin() shall return the last element == *(end()-1)

So a corrected statement would be:
vec.erase( find( vec.rbegin(), vec.rend(), buffer ).base()-1 );
Tricky details...

hth -Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <http://www.brainbench.com

Nov 7 '06 #6
"JH Programmer" <ho********@gmail.comwrote:
>
Since I am implementing the deletion of username in the login
system, because login name is unique, so it works fine.
In that case, I question the wisdom of using a vector. A std::set would
support faster deletion and insure that there is only one of each name
in existence.
but one thing I can think of is that we can declare another vector
array
vector<intpos;

so

if (data[i] == data buf){
pos.push back(i);
}

but to delete the elements in the array
we should delete it from the back
[ 0 1 0 2 3 4 1 0 2]
so if we want to delete [2]
pos[1] = 3
pos[2] = 8

for (int i = pos.size(); i >= 0; i--){
data.erase(data.begin() + pos[i]);
}
so delete from the back in vector "data"
will give correct output.
[0 1 0 3 4 1 0]
The erase-remove idiom is more effecient in general for removing all
values from a container.

pos.erase( remove( pos.begin(), pos.end(), 2 ), pos.end() );

Writing the above out would look something like this:

unsigned shift_amt = 0;
unsigned i = 0;
while ( i < pos.size() ) {
pos[i - shift_amt] = pos[i];
if ( pos[i] == value_to_remove ) {
++shift_amt;
++i;
}
pos.resize( pos.size() - shift_amt );

This way, each cell is shifted only once, no matter how may of that
value exist in the container.

--
To send me email, put "sheltie" in the subject.
Nov 7 '06 #7
In article <7a***************************@news.hispeed.ch>,
"Ivan Vecerina" <_I*******************@ivan.vecerina.comwrote:
"Daniel T." <da******@earthlink.netwrote in message
news:da****************************@news.west.eart hlink.net...
: It just so happens that the standard already has a function like this,
: defined in <algorithm>
:
: vec.erase( find( vec.rbegin(), vec.rend(), buffer ).base() );
: The above line will do the same thing your code segment does.

No! Caveat: &*revIter != &*(revIter.base()) !!

The reverse iterators had to bee shifted, because for example:
rbegin().base() == end()
but *rbegin() shall return the last element == *(end()-1)

So a corrected statement would be:
vec.erase( find( vec.rbegin(), vec.rend(), buffer ).base()-1 );
Tricky details...
Good catch, I forgot about that (don't use reverse iterators much.)

--
To send me email, put "sheltie" in the subject.
Nov 7 '06 #8

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

Similar topics

8
by: Maximus | last post by:
Hi, I was wondering if it is bad to overuse the new & delete operator. In my application, e.g. I created my own list class and I will have to resize my variable maybe like 100 times during runtime...
11
by: Peter Olcott | last post by:
I have just built a class that provides the most useful subset of std::vector functionality for use by compilers that lack template capability. http://home.att.net/~olcott/std_vect.html ...
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...
35
by: Jon Slaughter | last post by:
I'm having a problem allocating some elements of a vector then deleting them. Basicaly I have something like this: class base { private: std::vector<object> V;
4
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: streamkid | last post by:
i have a class table, which has a vector of records(-db). i 'm trying to remove an element, but it doesn't seem to work.. i read this http://www.cppreference.com/cppvector/erase.html] and that's...
12
by: subramanian100in | last post by:
Suppose class Base { public: virtual ~Test() { ... } // ... }; class Derived : public Base
19
by: Daniel Pitts | last post by:
I have std::vector<Base *bases; I'd like to do something like: std::for_each(bases.begin(), bases.end(), operator delete); Is it possible without writing an adapter? Is there a better way? Is...
2
by: thelamb | last post by:
Hello all, I have a question about deleting objects(the objects are stored in a vector) I Fill the vector as following: servObj.push_back(new CGame(serversh, game_id)); (1) ...
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
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...
1
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...
1
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.