473,231 Members | 1,581 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,231 software developers and data experts.

doubling by copy

hi all,

is it legal to double the content of a vector c by

copy(c.begin(), c.end(), back_inserter(c));

when ensuring before

c.reserve(2*c.size());

???

best regards

k ahrens
Jul 11 '06 #1
8 1218
Klaus Ahrens wrote:
is it legal to double the content of a vector c by

copy(c.begin(), c.end(), back_inserter(c));

when ensuring before

c.reserve(2*c.size());

???
I don't see any problem with it. If no reallocation happens (and
'reserve' should take care of that), all iterators stay valid, and
you're just copy-constructing elements after the previous end.
The only one thing I'd worry about is that the 'end' changes, so
there can be an implementation in which the object returned by
'c.end()' can keep changing when you push_back (moving target).

If you need to simply grow the vector by copying its contents at
the end, why not try

c.reserve(..
c.insert(c.end(), c.begin(), c.end());

it might be slightly faster.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 11 '06 #2
In article <e9**********@news.datemas.de>,
"Victor Bazarov" <v.********@comAcast.netwrote:
Klaus Ahrens wrote:
is it legal to double the content of a vector c by

copy(c.begin(), c.end(), back_inserter(c));

when ensuring before

c.reserve(2*c.size());

???

I don't see any problem with it. If no reallocation happens (and
'reserve' should take care of that), all iterators stay valid, and
you're just copy-constructing elements after the previous end.
The only one thing I'd worry about is that the 'end' changes, so
there can be an implementation in which the object returned by
'c.end()' can keep changing when you push_back (moving target).
How could you have a moving target? c.end() is evaluated only once
before entering the copy function, isn't it?
Jul 11 '06 #3
Daniel T. wrote:
In article <e9**********@news.datemas.de>,
"Victor Bazarov" <v.********@comAcast.netwrote:

How could you have a moving target? c.end() is evaluated only once
before entering the copy function, isn't it?
Yes, if the copy() function is written properly. The following copy()
function never ends:

#include <vector>
#include <iostream>

using namespace std;

void copy( vector<int&v,
const vector<int>::iterator &b,
const vector<int>::iterator &e){
while(b!=e){
cout<<"size="<<v.size()<<endl;
v.push_back(*b);
}
}

int main(){
vector<intv;
v.push_back(1);
v.push_back(2);
copy(v,v.begin(), v.end());
}

Jul 11 '06 #4
jo******@gmail.com wrote:
Daniel T. wrote:
>In article <e9**********@news.datemas.de>,
"Victor Bazarov" <v.********@comAcast.netwrote:

How could you have a moving target? c.end() is evaluated only once
before entering the copy function, isn't it?

Yes, if the copy() function is written properly. The following copy()
function never ends:

#include <vector>
#include <iostream>

using namespace std;

void copy( vector<int&v,
const vector<int>::iterator &b,
const vector<int>::iterator &e){
while(b!=e){
cout<<"size="<<v.size()<<endl;
v.push_back(*b);
}
}
However, according to 25.2.1, that can't happen, as the iterators are
passed by value, not by reference. The footprint for std::copy is as
follows:

template<class InputIterator, class OutputIterator>
OutputIterator copy(InputIterator first, InputIterator last,
OutputIterator result);
Jul 11 '06 #5
jo******@gmail.com wrote:
Daniel T. wrote:
In article <e9**********@news.datemas.de>,
"Victor Bazarov" <v.********@comAcast.netwrote:

How could you have a moving target? c.end() is evaluated only once
before entering the copy function, isn't it?

Yes, if the copy() function is written properly. The following copy()
function never ends:
[..]
void copy( vector<int&v,
const vector<int>::iterator &b,
const vector<int>::iterator &e){
while(b!=e){
cout<<"size="<<v.size()<<endl;
v.push_back(*b);
}
Oops, sorry, of course that doens't end. I thought that the iterator by
reference would change, but clearly it doesn't with the standard vector
class.

I guess if the c.end() in the original example were to return a
reference to an internal iterator, then a problem would occur.

Jul 11 '06 #6
jo******@gmail.com wrote:
jo******@gmail.com wrote:
>Daniel T. wrote:
>>In article <e9**********@news.datemas.de>,
"Victor Bazarov" <v.********@comAcast.netwrote:

How could you have a moving target? c.end() is evaluated only once
before entering the copy function, isn't it?

Yes, if the copy() function is written properly. The following copy()
function never ends:
[..]
>void copy( vector<int&v,
const vector<int>::iterator &b,
const vector<int>::iterator &e){
while(b!=e){
cout<<"size="<<v.size()<<endl;
v.push_back(*b);
}

Oops, sorry, of course that doens't end. I thought that the iterator
by reference would change, but clearly it doesn't with the standard
vector class.

I guess if the c.end() in the original example were to return a
reference to an internal iterator, then a problem would occur.
Even if 'copy' accepts an iterator by value, it's still possible for
that iterator to get the reference to the container, and perform some
kind of lookup based on the "real" (floating) end of the container when
'==' is used on it...

All those examples are of course beyond common sense, I guess.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 11 '06 #7
In article <e9**********@news.datemas.de>,
"Victor Bazarov" <v.********@comAcast.netwrote:
jo******@gmail.com wrote:
jo******@gmail.com wrote:
Daniel T. wrote:
In article <e9**********@news.datemas.de>,
"Victor Bazarov" <v.********@comAcast.netwrote:

How could you have a moving target? c.end() is evaluated only once
before entering the copy function, isn't it?

Yes, if the copy() function is written properly. The following copy()
function never ends:
[..]
void copy( vector<int&v,
const vector<int>::iterator &b,
const vector<int>::iterator &e){
while(b!=e){
cout<<"size="<<v.size()<<endl;
v.push_back(*b);
}
Oops, sorry, of course that doens't end. I thought that the iterator
by reference would change, but clearly it doesn't with the standard
vector class.

I guess if the c.end() in the original example were to return a
reference to an internal iterator, then a problem would occur.

Even if 'copy' accepts an iterator by value, it's still possible for
that iterator to get the reference to the container, and perform some
kind of lookup based on the "real" (floating) end of the container when
'==' is used on it...

All those examples are of course beyond common sense, I guess.
Personally I would consider that an implementation error. Does the
standard say one way or the other?
Jul 11 '06 #8
Victor Bazarov wrote:
jo******@gmail.com wrote:
>jo******@gmail.com wrote:
>>Daniel T. wrote:
In article <e9**********@news.datemas.de>,
"Victor Bazarov" <v.********@comAcast.netwrote:

How could you have a moving target? c.end() is evaluated only once
before entering the copy function, isn't it?
Yes, if the copy() function is written properly. The following copy()
function never ends:
[..]
>>void copy( vector<int&v,
const vector<int>::iterator &b,
const vector<int>::iterator &e){
while(b!=e){
cout<<"size="<<v.size()<<endl;
v.push_back(*b);
}
Oops, sorry, of course that doens't end. I thought that the iterator
by reference would change, but clearly it doesn't with the standard
vector class.

I guess if the c.end() in the original example were to return a
reference to an internal iterator, then a problem would occur.

Even if 'copy' accepts an iterator by value, it's still possible for
that iterator to get the reference to the container, and perform some
kind of lookup based on the "real" (floating) end of the container when
'==' is used on it...

All those examples are of course beyond common sense, I guess.

V
While I can't find anything that prevents end() from being implemented
in such a way, I do believe that a version of copy() that couldn't
handle that would be non-conforming, since the complexity requirements
are "Exactly last - first assignments."

--
Alan Johnson
Jul 12 '06 #9

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

Similar topics

0
by: simon | last post by:
When user clicks SAVE button, it creates a new order with some products. I open connection, set isolation level on adXactIsolated(1048576) - because I have trigger on tables, and then begin...
42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
6
by: meanster99 | last post by:
Can someone please help me! I have created a db in Access 2003 and the overall file size keeps doubling periodically. Before I noticed this phenomenon the DB was at 65Mb (could have been happening...
4
by: Nate | last post by:
Before I go through posting a lot of code, a quick question ... Has anyone come across a problem with a DataView showing duplicates of the desired data? I'm querying a SQL table and the DataView...
0
by: DC01 | last post by:
I have added a new measure successfully into the normal cube. I then add it to the virtual cube and reprocess all cubes. I can browse the normal cube successfully. Then when I try and browse the...
9
by: indi | last post by:
I've spent the last two hours trying every other solution listed, to no avail: this works, so I'm sharing it for the other folks who couldn't find a solution other than switching to another...
2
by: Blasting Cap | last post by:
I've got a gridview (that I converted over from a datagrid, which had been working properly), that is doubling up the number of rows returned. When it was running as a datagrid, the same code sent...
0
by: =?Utf-8?B?Z3V5?= | last post by:
I have an autogenerated GridView. I examine the first row of data and construct a set of edit boxes(if the Cell's control collection is empty a TextBox, if not, examine the control (in practice...
3
by: Xean45 | last post by:
Hey all, Sorry to be the noob with two posts asking a question, but I've been fighting with this for a couple days now, and as far as I can tell, this should be working, but PHP isn't my main...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.