Does the STL have a function like this one?
template <typename T>
void remove(std::vec tor<T> &v, std::vector<T>: :size_type index)
{
std::swap(v[index], v.back());
v.resize(index) ;
}
Unlike std::vector::er ase, 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. 8 4897
* Jason Heyes: Does the STL have a function like this one?
template <typename T> void remove(std::vec tor<T> &v, std::vector<T>: :size_type index) { std::swap(v[index], v.back()); v.resize(index) ; }
Unlike std::vector::er ase, 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?
Jason Heyes wrote: Does the STL have a function like this one?
template <typename T> void remove(std::vec tor<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::er ase, 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...
On Sun, 15 Jan 2006 14:14:21 GMT, Calum Grant
<bo*****@visula .nospamplease.o rg> wrote: template <typename T> void remove(std::vec tor<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::er ase, 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**********@Ho me.com
On Sun, 15 Jan 2006 17:16:07 +0100, Bob Hairgrove
<in*****@bigfoo t.com> wrote:
[snip]
PS -- what Alf said, re: v.resize(v.size ()-1);
--
Bob Hairgrove No**********@Ho me.com
Bob Hairgrove wrote: On Sun, 15 Jan 2006 14:14:21 GMT, Calum Grant <bo*****@visula .nospamplease.o rg> wrote:
template <typename T> void remove(std::vec tor<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::er ase, 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**********@Ho me.com
On Sun, 15 Jan 2006 17:35:29 GMT, Calum Grant
<bo*****@visula .nospamplease.o rg> 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**********@Ho me.com
Calum Grant wrote: Jason Heyes wrote: Does the STL have a function like this one?
template <typename T> void remove(std::vec tor<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::er ase, 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).
"Jason Heyes" <ja********@opt usnet.com.au> wrote in message
news:43******** *************** @news.optusnet. com.au... Does the STL have a function like this one?
template <typename T> void remove(std::vec tor<T> &v, std::vector<T>: :size_type index) { std::swap(v[index], v.back()); v.resize(index) ; }
Unlike std::vector::er ase, 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? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: enzo |
last post by:
hi all,
i don't understand what's wrong:
1)
std::vector<double> p(10);
doesn't compile:
|
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.
|
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 alignment specification of stack variables.
Upon examing the source of the error, it was due to the signatures of the
std::vector::resize being as follows:
template<class _Ty, class _Ax = allocator<_Ty> >
|
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());
}
|
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 me that if n is smaller than the
current size, simple adjust the internal size counter to n and be done.
| |
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 0xf340ef0 in partition
0x311b8c
Here is the example code I'm using to produce the error. Any idea's what is
wrong with the code?
|
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 advantage/disadvantage of using one over the other?
I checked the VS7.1 implementation, in which I couldn't find any
difference. Both of them are effectively calling erase(begin(), end()).
|
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 elements of the vector.
Thank you !
Mathieu
Code:
|
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 assignment is done of the
referenced OtherObj object (according to the compiler messages). This is
strange, since the referenced OtherObj is initialized using member
initialization lists. Anyone a clue?
thanks,
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |