472,347 Members | 2,383 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,347 software developers and data experts.

std::vector - fast way to crop?

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

Jul 23 '05 #1
6 4733
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).swap(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
Jul 23 '05 #2
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

Jul 23 '05 #3
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
Jul 23 '05 #4
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.

Jul 23 '05 #5
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
Jul 23 '05 #6
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.

Jul 23 '05 #7

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

Similar topics

27
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...
18
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...
11
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...
20
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;...
32
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...
21
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...
82
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...
13
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...
2
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
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...

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.