Is there a fast way to crop a vector? I have a vector of size n, and
want to reduce it to some subregion (x,y ). Seems like you shouldn't
have to recopy all of those elements into a new vector, since they are
already contiguous in memory.
Thanks,
Isaac 6 4982 is******@gmail. com wrote: Is there a fast way to crop a vector? I have a vector of size n, and want to reduce it to some subregion (x,y ). Seems like you shouldn't have to recopy all of those elements into a new vector, since they are already contiguous in memory.
vector<blah> v;
...
vector<blah>(v. begin()+x, v.begin()+y).sw ap(v);
or
vector<blah> v;
...
v.resize(y+1);
v.erase(0, v.begin()+x);
Both approaches involve copying and destroying of the vector's elements.
In order to understand the performance you would need to time them.
V
On 2005-02-22 15:12:12 -0500, is******@gmail. com said: Is there a fast way to crop a vector? I have a vector of size n, and want to reduce it to some subregion (x,y ). Seems like you shouldn't have to recopy all of those elements into a new vector, since they are already contiguous in memory.
You will have to do some copying, as STL containers "own" the contained
objects. There is no way to transfer those objects to a new vector, or
make them outlive the container. It would be possible to write a
container that could do this (i.e. have it store five pointers:
"storage begin", "begin", "end" and "storage end"), but that could be
pretty wasteful in the general case.
There are several ways to accomplish what you want, but they all
involve copying in the general case:
Assuming:
v is a vector of <foo>
x and y are iterators pointing into v
1)
std::copy(x, y, v.begin());
v.resize(y-x);
2)
v.resize(y - v.begin());
v.erase(v.begin (), x);
3)
std::swap(v, std::vector<foo >(x,y));
.... among others. The only case that I can think of off the top of my
head where no copying is needed is if (x == v.begin()):
v.resize(y - x);
--
Clark S. Cox, III cl*******@gmail .com is******@gmail. com wrote: Is there a fast way to crop a vector? I have a vector of size n, and want to reduce it to some subregion (x,y ). Seems like you shouldn't have to recopy all of those elements into a new vector, since they are already contiguous in memory.
What exactly do you mean by "crop"? If you mean delete everything before x,
and after y(inclusive?), then look at using a deque. Deque provides
amortized constant time insert/delete to both the front and back of the
sequence.
Jeff Flinn is******@gmail. com wrote: Is there a fast way to crop a vector? I have a vector of size n, and want to reduce it to some subregion (x,y ). Seems like you shouldn't have to recopy all of those elements into a new vector, since they are already contiguous in memory.
The usual way to handle this is to rearrange the items in the vector so
the ones you want to keep are all at the beginning, followed by all
those you want to get rid of. std::remove will rearrange items into an
order like this.
Once you've done that, you use vector.erase to get rid of the items you
no longer need.
--
Later,
Jerry.
The universe is a figment of its own imagination.
Jerry Coffin wrote: is******@gmail. com wrote:
Is there a fast way to crop a vector? I have a vector of size n, and want to reduce it to some subregion (x,y ). Seems like you shouldn't have to recopy all of those elements into a new vector, since they are already contiguous in memory.
The usual way to handle this is to rearrange the items in the vector so the ones you want to keep are all at the beginning, followed by all those you want to get rid of. std::remove will rearrange items into an order like this.
Once you've done that, you use vector.erase to get rid of the items you no longer need.
I wonder, would it actually be any faster than, say, two erases?
My guess is that it wouldn't but I am open for a demonstration that
would prove me wrong.
V
Victor Bazarov wrote:
[ ... ] The usual way to handle this is to rearrange the items in the vector so the ones you want to keep are all at the beginning, followed by all those you want to get rid of.
[ ... ]
I wonder, would it actually be any faster than, say, two erases? My guess is that it wouldn't but I am open for a demonstration that would prove me wrong.
I suspect it'll depend on the implementation and probably on the
amounts of data involved as well as whether you need to maintain the
order of the items you keep.
If you're removing N items from the beginning of a vector of M items,
if you need to maintain the order, you have to move M-N items. If you
don't need to maintain the relative order, you only need to move N
items.
Whether this will save significant time will depend on the size of N
relative to M-N.
--
Later,
Jerry.
The universe is a figment of its own imagination. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Jason Heyes |
last post by:
To my understanding, std::vector does not use reference counting to avoid
the overhead of copying and initialisation. Where can I get a reference
counted implementation of std::vector? Thanks.
|
by: Janina Kramer |
last post by:
hi ng,
i'm working on a multiplayer game for a variable number of players and on the client side, i'm using a std::vector<CPlayer> to store
informatik about the players. CPlayer is a class that contains another std::vector<CPosition>. Because one of the players is the
client itself (and the size of the vector<CPlayer> doesn't change during a game), i thought i could store a
std::vector<CPlayer>::iterator "localplayer" that points to the...
|
by: Steve |
last post by:
Hi,
I'm using a std::vector to store a list of user defined objects. The vector
may have well over 1000 elements, and I'm suffering a performance hit. If I
use push_back I get a much worse perfomance than if I first define the
vector of a given size, then write to the elements with myvec =
However, I'm currently thinking that it isn't feasible to obtain the vector
size, so really need to resize the vector dynamically as I go. Is...
|
by: Anonymous |
last post by:
Is there a non-brute force method of doing this?
transform() looked likely but had no predefined function object.
std::vector<double> src;
std::vector<int> dest;
std::vector<double>::size_type size = src.size();
dest.reserve(size);
for (std::vector<int>::size_type i = 0;
|
by: zl2k |
last post by:
hi, c++ user
Suppose I constructed a large array and put it in the std::vector in a
function and now I want to return it back to where the function is called.
I can do like this:
std::vector<int> fun(){
//build the vector v;
return v;
}
| |
by: Peter Olcott |
last post by:
I got the previous alias to std::vector working, and found that it takes up the
space of a pointer. I want to find a way to do an alias to a std::vector that
does not take up any space. Is there any way to do this? (I tried #define Name1
Name2 and it didn't compile)
|
by: Peter Olcott |
last post by:
I need std::vector like capability for several custom classes. I already
discussed this extensively in the thread named ArrayList without Boxing and
Unboxing. The solution was to simply create non-generic (non C++ template)
std::vector like capability for each of these custom classes. (Solution must
work in Visual Studio 2002).
Since I have already written one std::vector for a YeOlde C++ compiler (Borland
C++ 1.0) that had neither...
|
by: jubelbrus |
last post by:
Hi
I'm trying to do the following.
#include <vector>
#include <boost/thread/mutex.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/tuple/tuple.hpp>
class {
|
by: Chris Forone |
last post by:
hello group,
for std::vector, what access is faster from experience, the index or via
iterator?
thanks!
cheers, chris
|
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.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 most users, this new feature is actually very convenient. If you want to control the update process,...
| |
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 launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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: 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...
| |