473,396 Members | 2,151 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,396 software developers and data experts.

Removing a vector element using std::swap and std::vector::resize.

Does the STL have a function like this one?

template <typename T>
void remove(std::vector<T> &v, std::vector<T>::size_type index)
{
std::swap(v[index], v.back());
v.resize(index);
}

Unlike std::vector::erase, it calls T::operator= only three times no matter
what size of vector you are removing from and no matter where the removed
element is located.
Jan 15 '06 #1
8 4861
* Jason Heyes:
Does the STL have a function like this one?

template <typename T>
void remove(std::vector<T> &v, std::vector<T>::size_type index)
{
std::swap(v[index], v.back());
v.resize(index);
}

Unlike std::vector::erase, it calls T::operator= only three times no matter
what size of vector you are removing from and no matter where the removed
element is located.


Possibly you meant

v.resize( v.size() - 1 );

And possibly, when you write "the STL" you mean the C++ standard library,
not the STL.

Regarding whether there is such a function, do read the documentation.

Cheers,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jan 15 '06 #2
Jason Heyes wrote:
Does the STL have a function like this one?

template <typename T>
void remove(std::vector<T> &v, std::vector<T>::size_type index)
{
std::swap(v[index], v.back());
v.resize(index);
Do you mean v.pop_back()?
}

Unlike std::vector::erase, it calls T::operator= only three times no matter
what size of vector you are removing from and no matter where the removed
element is located.


I suggest you try using that function to see what happens! I don't
think it works quite the same as erase...

Jan 15 '06 #3
On Sun, 15 Jan 2006 14:14:21 GMT, Calum Grant
<bo*****@visula.nospamplease.org> wrote:
template <typename T>
void remove(std::vector<T> &v, std::vector<T>::size_type index)
{
std::swap(v[index], v.back());
v.resize(index);


Do you mean v.pop_back()?


He means v.back() -- how could he mean anything else?
}

Unlike std::vector::erase, it calls T::operator= only three times no matter
what size of vector you are removing from and no matter where the removed
element is located.


I suggest you try using that function to see what happens! I don't
think it works quite the same as erase...


True, it doesn't work the same. But as long as the remaining elements
don't have to be in any specific order, I suppose it would be much
faster than calling (v.size()-index) times T::operator=(). Indeed,
this would perform in O(1) time for any given type T, whereas
v.erase() could only do O(n) at best (where n == v.size()-index).

As an aside, it should work for v.front() as well as v.back() (or *it
for any iterator it of v where it != v.end()). As it is, there should
be a little more sanity-checking; e.g., v cannot be empty, and
v.back() should point to a different element than v[index].

--
Bob Hairgrove
No**********@Home.com
Jan 15 '06 #4
On Sun, 15 Jan 2006 17:16:07 +0100, Bob Hairgrove
<in*****@bigfoot.com> wrote:

[snip]

PS -- what Alf said, re: v.resize(v.size()-1);

--
Bob Hairgrove
No**********@Home.com
Jan 15 '06 #5
Bob Hairgrove wrote:
On Sun, 15 Jan 2006 14:14:21 GMT, Calum Grant
<bo*****@visula.nospamplease.org> wrote:

template <typename T>
void remove(std::vector<T> &v, std::vector<T>::size_type index)
{
std::swap(v[index], v.back());
v.resize(index);
Do you mean v.pop_back()?

He means v.back() -- how could he mean anything else?


I mean, v.pop_back() instead of v.resize(index) -- how could I mean
anything else ;-)

Personally I prefer v.pop_back() to v.resize(v.size()-1).
}

Unlike std::vector::erase, it calls T::operator= only three times no matter
what size of vector you are removing from and no matter where the removed
element is located.

I suggest you try using that function to see what happens! I don't
think it works quite the same as erase...

True, it doesn't work the same. But as long as the remaining elements
don't have to be in any specific order, I suppose it would be much
faster than calling (v.size()-index) times T::operator=(). Indeed,
this would perform in O(1) time for any given type T, whereas
v.erase() could only do O(n) at best (where n == v.size()-index).

As an aside, it should work for v.front() as well as v.back() (or *it
for any iterator it of v where it != v.end()). As it is, there should
be a little more sanity-checking; e.g., v cannot be empty, and
v.back() should point to a different element than v[index].


It certainly wouldn't work if you used v.front(). Doing a
resize()/pop_back() would erase the wrong element.

Your suggestion might work on a std::deque however. This does have an
efficient pop_front() function.

The algorithm would certainly work if index == size()-1. std::swap
works to swap an item with itself. I would follow the general precedent
of the standard library - the caller is responsible for ensuring the
validity of the iterator.

--
Bob Hairgrove
No**********@Home.com

Jan 15 '06 #6
On Sun, 15 Jan 2006 17:35:29 GMT, Calum Grant
<bo*****@visula.nospamplease.org> wrote:
He means v.back() -- how could he mean anything else?


I mean, v.pop_back() instead of v.resize(index) -- how could I mean
anything else ;-)

Personally I prefer v.pop_back() to v.resize(v.size()-1).


LOL ... my bad ... sorry about the FUD!

Thought you meant "pop_back() instead of back()". Well, I guess that
was more than a little stupid of me.

--
Bob Hairgrove
No**********@Home.com
Jan 15 '06 #7
Calum Grant wrote:
Jason Heyes wrote:
Does the STL have a function like this one?

template <typename T>
void remove(std::vector<T> &v, std::vector<T>::size_type index)
{
std::swap(v[index], v.back());
v.resize(index);


Do you mean v.pop_back()?
}

Unlike std::vector::erase, it calls T::operator= only three times no
matter what size of vector you are removing from and no matter where
the removed element is located.


I suggest you try using that function to see what happens! I don't
think it works quite the same as erase...


1. should use v.at(index) instead of v[indexx]
2. Is not safe for empty vector (but item 1 takes care of that).
Jan 15 '06 #8
"Jason Heyes" <ja********@optusnet.com.au> wrote in message
news:43***********************@news.optusnet.com.a u...
Does the STL have a function like this one? template <typename T>
void remove(std::vector<T> &v, std::vector<T>::size_type index)
{
std::swap(v[index], v.back());
v.resize(index);
} Unlike std::vector::erase, it calls T::operator= only three times no
matter what size of vector you are removing from and no matter where the
removed element is located.


Even though the example above is incorrect (v.resize(index) should be
v.resize(v.size()-1) or, better yet, v.pop_back()), it's hard to imagine
that this operation would be useful enough to merit a standard function to
implement it.

For one thing, it's only two statements. For another, it doesn't preserve
the ordering of the vector elements, which rather limits its usefulness.
For a third, most of the operations on vectors use iterators, not indices.

Can you show us an example of where such a function would be useful?
Jan 15 '06 #9

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

Similar topics

4
by: enzo | last post by:
hi all, i don't understand what's wrong: 1) std::vector<double> p(10); doesn't compile:
4
by: Jason Heyes | last post by:
What harmful effects can using std::vector have on the performance of my program? How do I protect my program from these effects? Thanks.
0
by: solosnake | last post by:
Hello, and merry Christmas! When trying to compile some code that had specific alignment requirements, I found that the Visual Studio .NET compiler gave me compile time errors. It forbids...
6
by: Lieven | last post by:
Consider: std::vector<int> vec(100); vector<int>::iterator it(vec.begin()); for(int i(0); i < 10000000; i++){ vec.push_back(rand()); }
10
by: vsgdp | last post by:
On Page 67 of Effective STL, Meyers writes about resize: "If n is smaller than the current size, elements at the end of the container will be destroyed." What does "destroyed" mean? It seems to...
1
by: Daniel J Watkins | last post by:
Hi, When I attempt to resize a column in a 2d vector I receive the error listed below. I'm using the libraries supplied with VxWorks. 0xf3fe450 (tExcTask): memPartFree: invalid block...
5
by: atreya | last post by:
Hi, I'm trying to figure out if there are any differences between the implementation of resize(0) and clear() that a programmer using std::vector should be aware of! Could there be any...
4
by: mathieu | last post by:
Hello, I am looking at the API of std::vector but I cannot find a way to specify explicitely the size of my std::vector. I would like to avoid vector::resize since it first initializes the...
3
by: Bram Kuijper | last post by:
Hi all, I am trying to resize a vector of objects (MyObj below), which contain references to other objects (OtherObj, see below). However, apparently somewhere in the resize operation an...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
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,...

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.