Hi,
Is this right any stl container (vector, deque, list, . . .)?
typedef vector container;
int main()
{
container<int> c1;
c1.push_back(1);
c1.push_back(2);
c1.push_back(3);
container<int>::iterator it=c1.begin();
while(it != c1.end())
{
if(*it=2)
c1.erase(it);
it++;
}
}
Thanks,
Jose Luis. 7 4436
jose luis fernandez diaz <jo**********************@yahoo.es> spoke thus: Is this right any stl container (vector, deque, list, . . .)?
Josuttis has a lovely table where he lists whether inserting or
removing from a standard container invalidates iterators (which I
gather is the gist of your question):
vector: on reallocation
deque: always
list: never
set: never
multiset: never
map: never
multipmap: never
I don't know whether erase() can cause reallocation for a vector, so I
don't know if your code is safe.
container<int>::iterator it=c1.begin(); while(it != c1.end()) { if(*it=2) c1.erase(it); it++; }
--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
"jose luis fernandez diaz" <jo**********************@yahoo.es> wrote in
message Is this right any stl container (vector, deque, list, . . .)?
All the containers' erase functions returns an iterator that points to the
next element. Thus,
container<int>::iterator it=c1.begin(); while(it != c1.end()) { if(*it=2) c1.erase(it); it++; }
should be rewritten as
container<int>::iterator it=c1.begin();
while(it != c1.end())
{
if(*it=2)
it = c1.erase(it);
else
++it;
}
As an aside, note that I used pre-increment ++it because on many compilers
this will be more efficient than it++.
Note that repeated calls to erase may degrade performance, and could make
the entire erase operation run in O(N^2) time.
You can use std::remove too
container<int>::iterator it= std::remove(c1.begin(), c1.end(), 2);
This transforms the array { 1, 2, 3, 4, 3, 2, 1 } into { 1, 3, 4, 3, 1, 2,
2 } and returns an iterator to the first '2'. Therefore you can call erase
to actually erase the elements:
c1.erase(it, it.end());
Note that class list has list::erase.
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote in message news:<c6**********@chessie.cirr.com>... jose luis fernandez diaz <jo**********************@yahoo.es> spoke thus:
Is this right any stl container (vector, deque, list, . . .)?
Josuttis has a lovely table where he lists whether inserting or removing from a standard container invalidates iterators (which I gather is the gist of your question):
vector: on reallocation deque: always list: never set: never multiset: never map: never multipmap: never
That refers to all allocators except the erased one, of course.
[ 23.1.2/8 ]
Regards,
Michiel Salters
"Siemel Naran" <Si*********@REMOVE.att.net> wrote in message news:<Rs********************@bgtnsc04-news.ops.worldnet.att.net>... "jose luis fernandez diaz" <jo**********************@yahoo.es> wrote in message
Is this right any stl container (vector, deque, list, . . .)?
All the containers' erase functions returns an iterator that points to the next element.
All _sequence_ containers. std::multiset<T>::erase(T) will return the
number erased, as does multimap::erase. Other associative_container::erase
methods return void.
Regards,
Michiel Salters
Michiel Salters <Mi*************@logicacmg.com> spoke thus: That refers to all allocators except the erased one, of course.
^^^^^^^^^^ Iterators? :)
One would hope that'd be fairly obvious...
--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
"Michiel Salters" <Mi*************@logicacmg.com> wrote in message "Siemel Naran" <Si*********@REMOVE.att.net> wrote in message
news:<Rsmhc.12091 All the containers' erase functions returns an iterator that points to
the next element.
All _sequence_ containers. std::multiset<T>::erase(T) will return the number erased, as does multimap::erase. Other associative_container::erase methods return void.
Thanks. Just one question. My version of the standard has
multimap::erase(iterator position) returning void, not the number (of
elements) erased. Can you comment?
Siemel Naran wrote in
news:qb********************@bgtnsc04-news.ops.worldnet.att.net in
comp.lang.c++: "Michiel Salters" <Mi*************@logicacmg.com> wrote in message "Siemel Naran" <Si*********@REMOVE.att.net> wrote in message news:<Rsmhc.12091
> All the containers' erase functions returns an iterator that points > to the > next element.
All _sequence_ containers. std::multiset<T>::erase(T) will return the number erased, as does multimap::erase. Other associative_container::erase methods return void.
Thanks. Just one question. My version of the standard has multimap::erase(iterator position) returning void, not the number (of elements) erased. Can you comment?
Think about it, iterator is either 'iterating' to 1 element
or it is multimap<>::end(), so you could write:
template < typename K, typename V >
int erase(
std::multimap< K, V > &map,
typename std::multimap< K, V >::iterator ptr
)
{
if ( ptr == map.end() ) return 0;
map.erase( ptr );
return 1;
}
But would you bother ?
My £0.02
Rob.
-- http://www.victim-prime.dsl.pipex.com/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Paras |
last post by:
Hi
What is the correct way to delete an element from STL list while
iterating through the list
list<A*> _ls;
A * a;
list<A*>::iterator si1;
|
by: Pieter Thysebaert |
last post by:
Hello,
I've got a question conerning erasing key-value pairs from a std::map while
iterating over it.
According to the STL docs, erasing an element in a map invalidates all
iterators pointing...
|
by: jose luis fernandez diaz |
last post by:
Hi,
Erase elements while iterating on a map don't invalidate the iterator
except the erased one, so the program below:
(1)
#include <map>
int main()
|
by: Chad E. Dollins |
last post by:
I would like to know how to remove an element from a vector. The following
give a segmentation fault perhaps someone can give me a proper explanation
why:
vector<T> * myCon;
//...some code that...
|
by: olanglois |
last post by:
Hi,
I was asking myself to following question. What is better to erase an
element from a STL map:
calling (option #1)
size_type erase(const key_type& k)
or calling (option #2)
|
by: sks |
last post by:
I have a question regarding std::multimap/iterators.
At the SGI website, it says "Erasing an element from a multimap also does
not invalidate any iterators, except, of course, for iterators that...
|
by: newbie |
last post by:
say I have a set containing pairs.
set< pair<AbstractClass*, double container;
container.insert(pair <objectPtrA, 0.0);
....
....
at here, I don't know what is the value in that pair, but I...
|
by: subramanian100in |
last post by:
Suppose I have
vector<intcontainer;
Suppose I store some values into "container".
Suppose that "left" and "right" are valid iterators into "container"
and that "right" is NOT...
|
by: Philip Mueller |
last post by:
Hi,
I am using multiple stl::list s of different types.
Now I want to write a function
list<type>::iterator s_erase(list<typel, list<type>::iterator it)
that takes an iterator and deletes...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
| |