473,586 Members | 2,490 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

delete vector element

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 the function i 've written:

void table::delel( int index )
{
vector< record >::iterator rm = db.begin();
for( int i = 0; i < index; i++ )
rm++;
db.erase( rm );
}

after i execute this function, the record isn't deleted, insted it
takes all the values from the
next one( if there is any ). else.. it crashes..

any idea?

Dec 16 '06 #1
5 12026
st*******@gmail .com wrote:
void table::delel( int index )
{
vector< record >::iterator rm = db.begin();
for( int i = 0; i < index; i++ )
rm++;
Vector iterators are random access, you could do:
db.erase(db.beg in() + index();
db.erase( rm );
}

after i execute this function, the record isn't deleted, insted it
takes all the values from the
next one( if there is any ). else.. it crashes..
That's to be expected. The actual object in the vector
belong to the vector. Deleting one typically involves
copying (by assignment) all the later ones to one position
earlier and then calling the destructor on the last.

The things you store in the vector must be copy contructable
and assignable, so that this sort of strategy works.
Dec 16 '06 #2
st*******@gmail .com wrote:
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 the function i 've written:

void table::delel( int index )
{
vector< record >::iterator rm = db.begin();
for( int i = 0; i < index; i++ )
rm++;
db.erase( rm );
}
void table::delel(st d::vector<recor d>::difference_ type index)
{
db.erase(db.beg in() + index);
}

Best regards,

Tom

Dec 16 '06 #3

st*******@gmail .com wrote:
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 the function i 've written:

void table::delel( int index )
{
vector< record >::iterator rm = db.begin();
for( int i = 0; i < index; i++ )
rm++;
db.erase( rm );
}

after i execute this function, the record isn't deleted, insted it
takes all the values from the
next one( if there is any ). else.. it crashes..
What are you doing? The code you have written appears to increment an
iterator that starts at the beginning the same number of times as
elements you have. So if you have 3 elements you will start at the
beginning and increment the iterartor 3 times which leaves your
iterator pointing to the end() which is one after the last element in
your list. Then you try to erase that item, which is not an item in
your vector. Please tell us which item do you actually want to erase?

---
Ivan
http://www.0x4849.net

Dec 16 '06 #4

Ō/« Ivan Novick ›„ŮŠÝŚ:
st*******@gmail .com wrote:
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 the function i 've written:

void table::delel( int index )
{
vector< record >::iterator rm = db.begin();
for( int i = 0; i < index; i++ )
rm++;
db.erase( rm );
}

after i execute this function, the record isn't deleted, insted it
takes all the values from the
next one( if there is any ). else.. it crashes..

What are you doing? The code you have written appears to increment an
iterator that starts at the beginning the same number of times as
elements you have. So if you have 3 elements you will start at the
beginning and increment the iterartor 3 times which leaves your
iterator pointing to the end() which is one after the last element in
your list. Then you try to erase that item, which is not an item in
your vector. Please tell us which item do you actually want to erase?

---
Ivan
http://www.0x4849.net
records are displayed, and user inputs what record he want to delete. (
this happens in other functions ).. the number of the record user
inputs is index, and is a parameter for the delete function.. the
iterator is incremented, because it starts from the beggining of the
vector, till it reaches the wanted point.( user will never want to
delete the first element, and the case that the vector is empty is
checked in another function ). then, what the iterator points to has to
be removed. that's all :)

Dec 16 '06 #5

StreamKid wrote:
records are displayed, and user inputs what record he want to delete. (
this happens in other functions ).. the number of the record user
inputs is index, and is a parameter for the delete function.. the
iterator is incremented, because it starts from the beggining of the
vector, till it reaches the wanted point.( user will never want to
delete the first element, and the case that the vector is empty is
checked in another function ). then, what the iterator points to has to
be removed. that's all :)
Ohhh, I see, than Tom's solution looks good.

--
Ivan
http://www.0x4849.net

Dec 17 '06 #6

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

Similar topics

8
3015
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 (if not more). By resize I mean somthing like: int* a = new int; delete a; a = new int; delete a;
11
5030
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 Through suggestions from this news group I was able to exactly duplicate the interface of std::vector, except for one aspect. What I need is a way to...
11
1774
by: DamonChong | last post by:
Hi, I am new to c++. I recently spend an enormous among of time troubleshooting a seeminly innocuous piece of code. Although I narrow down this piece of code as the culprit but I don't understand why. Can some guru help to enlighten me? Thank you. // I created an array of pointers to object pointers: Object ** obs = new Object * ;
9
3601
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 even numbers. My questions is actullay I have to put pos++ in the body of the loop(also in if, else, statement). Can I somehow put it in...
35
2624
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;
10
3209
by: Whybother | last post by:
I have this typedef map<__int64, int> Results_Map; __int64 is defined as 8 bytes ranging from -9,223,372,036.854,775,808 to 9,223,372,036.854,775,807 I have loaded it with approx 34 million hash keys and when the program is shutting down it takes forever (10+ mins) while its deleting the objects. The memory it uses according to task...
7
4340
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
12
2696
by: subramanian100in | last post by:
Suppose class Base { public: virtual ~Test() { ... } // ... }; class Derived : public Base
2
2240
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) Since I am calling new, I need to call delete somewhere else in the program.
0
8204
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7965
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8220
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6617
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, and deploymentówithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5712
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 presenter, Adolph Duprť who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3838
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3869
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2345
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 we have to send another system
1
1452
muto222
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.