c++ - vector::erase() not working -


removecontact_hi have created function remove element in vector:

void jnp::ipcomm_hosttype::removecontact_h( uint contactindex,std::vector<ipcontact>& l ) {     assert( contactindex < l.size() );     l.erase( l.begin() + contactindex ); } 

however not appear work. element not removed when call function. size of vector reduced 1, can still access all elements including deleted element coding

hostlist[some integer] 

i.e if hostlist had 2 elements , call

removecontact_h( 0, hostlist ) 

the size reduced 1, both elements still exits , accessed!

edit

unsurprisingly, error due coding , not vector::erase() function. had defined equals operator ipcontact class didn't copy data.

it undefined behavior access elements erased vector. work (your case) crash @ worst moment possible (i.e. during demos, or in front of family ;) ).

but if use std::vector::at access element erased, throw std::out_of_range exception.

the std::vector::operator[] not perform bounds checking.

the size reduced 1, both elements still exits , accessed!

like said before, undefined behavior , should not try access erased elements. try vector::at, throw exception.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -