STL vector

  • Thread starter Christian Christmann
  • Start date
C

Christian Christmann

Hi,

what is the most efficient way to insert a new element into an
STL vector before a given element?

My idea was:

vector< Objects > myVector;
myVector.push_back( ...
myVector.insert(
find( myVector.begin(), myVector.end(), existElement ), newElement );

[where newElement and existElement are entities of Objects's class].

Does this look OK for you? ;-)
Or are there simpler approaches?

Regards,
Chris
 
C

Christian Christmann

Hi,

what is the most efficient way to insert a new element into an
STL vector before a given element?

Sorry, that's just half the truth. What I actually need is:
A new element has to be inserted into an STL vector before an
element that is specified by its position in the form of an
integer value.
My idea was:

vector< Objects > myVector;
myVector.push_back( ...
myVector.insert(
find( myVector.begin(), myVector.end(), existElement ), newElement );

So, IMHO, the insertion should look like this:

myVector.insert( find( myVector.begin(), myVector.end(),
myVector.at(n) ), newElement );

where 'n' is the position the new element 'newElement' should be inserted
before.

Looks somehow cumbersome. ;-)
 
P

Paul

Christian Christmann said:
On Thu, 17 Aug 2006 12:37:26 +0200, Christian Christmann wrote:

Sorry, that's just half the truth. What I actually need is:
A new element has to be inserted into an STL vector before an
element that is specified by its position in the form of an
integer value.

I don't know why you didn't do the obvious:

myVector.insert( myVector.begin() + n, newElement);

Paul
 
C

Clark S. Cox III

Sorry, that's just half the truth. What I actually need is:
A new element has to be inserted into an STL vector before an
element that is specified by its position in the form of an
integer value.


So, IMHO, the insertion should look like this:
myVector.insert( find( myVector.begin(), myVector.end(),
myVector.at(n) ), newElement );

where 'n' is the position the new element 'newElement' should be inserted
before.

Looks somehow cumbersome. ;-)

Yes, it does. If you already know the position, then the find is
redundant and wasteful:

myVector.insert(myVector.begin() + n, newElement);
 
D

Daniel T.

"Paul" <[email protected]> said:
I don't know why you didn't do the obvious:

myVector.insert( myVector.begin() + n, newElement);

or

myVector.insert( myVector.begin()[n], newElement );

Of course, with these you will have to first make sure that 'n' is
within bounds.
 
T

Thomas Tutone

Daniel said:
myVector.insert( myVector.begin()[n], newElement );

There is no guarantee under the standard that this would work. In
fact, this would only work if std::vector used pointers for iterators.
This may be true for some implementations, but is not true for many
others (including modern versions of gcc).

Best regards,

Tom.
 
R

Rolf Magnus

Thomas said:
Daniel said:
myVector.insert( myVector.begin()[n], newElement );

There is no guarantee under the standard that this would work. In
fact, this would only work if std::vector used pointers for iterators.
This may be true for some implementations, but is not true for many
others (including modern versions of gcc).

Actually, it won't even work with pointers for iterators, because
myVector.begin()[n] is not a pointer.
 
P

Philip Potter

Thomas Tutone said:
Daniel said:
myVector.insert( myVector.begin()[n], newElement );

There is no guarantee under the standard that this would work. In
fact, this would only work if std::vector used pointers for iterators.

It wouldn't even work then. If iterators were pointers, you could say:
myVector.insert( &myVector.begin()[n], newElement );

But this is the same as the originally-suggested:
myVector.insert( myVector.begin() + n, newElement );
anyway.
 
T

Thomas Tutone

Rolf said:
Thomas said:
Daniel said:
myVector.insert( myVector.begin()[n], newElement );

There is no guarantee under the standard that this would work. In
fact, this would only work if std::vector used pointers for iterators.
This may be true for some implementations, but is not true for many
others (including modern versions of gcc).

Actually, it won't even work with pointers for iterators, because
myVector.begin()[n] is not a pointer.

Oops. You're right. I guess I was thinking of
"myVector.insert(&myVector.begin()[n], newElement);" which my comment
would apply to. I wonder what Daniel intended?

Best regards,

Tom
 
D

Daniel T.

"Thomas Tutone said:
Daniel said:
myVector.insert( myVector.begin()[n], newElement );

There is no guarantee under the standard that this would work. In
fact, this would only work if std::vector used pointers for iterators.
This may be true for some implementations, but is not true for many
others (including modern versions of gcc).

According to "The C++ Programming Language", op[] is a required operator
for random access iterators. Was Stroustrup wrong?
 
D

Daniel T.

"Thomas Tutone said:
Rolf said:
Thomas said:
Daniel T. wrote:

myVector.insert( myVector.begin()[n], newElement );

There is no guarantee under the standard that this would work. In
fact, this would only work if std::vector used pointers for iterators.
This may be true for some implementations, but is not true for many
others (including modern versions of gcc).

Actually, it won't even work with pointers for iterators, because
myVector.begin()[n] is not a pointer.

Oops. You're right. I guess I was thinking of
"myVector.insert(&myVector.begin()[n], newElement);" which my comment
would apply to. I wonder what Daniel intended?

Yes, I missed the '&' in my original post. However Stroustrup's book
says that op[] exists for random access iterators. Is TC++PL wrong?
 
G

Gavin Deane

Daniel said:
Thomas Tutone said:
Daniel said:
myVector.insert( myVector.begin()[n], newElement );

There is no guarantee under the standard that this would work. In
fact, this would only work if std::vector used pointers for iterators.
This may be true for some implementations, but is not true for many
others (including modern versions of gcc).

According to "The C++ Programming Language", op[] is a required operator
for random access iterators. Was Stroustrup wrong?

Unsurprisingly, no he wasn't. 24.1.5 Random Access Iterators agrees
with him. But (and now we're into my interpretation, not Stroustrup's)

myVector.insert( myVector.begin()[n], newElement );

would be equivalent to

myVector.insert( *(myVector.begin() + n), newElement );

not to

myVector.insert( myVector.begin() + n, newElement );

so I don't think it would work.

Gavin Deane
 
K

Kai-Uwe Bux

Daniel said:
Thomas Tutone said:
Rolf said:
Thomas Tutone wrote:

Daniel T. wrote:

myVector.insert( myVector.begin()[n], newElement );

There is no guarantee under the standard that this would work. In
fact, this would only work if std::vector used pointers for
iterators. This may be true for some implementations, but is not true
for many others (including modern versions of gcc).

Actually, it won't even work with pointers for iterators, because
myVector.begin()[n] is not a pointer.

Oops. You're right. I guess I was thinking of
"myVector.insert(&myVector.begin()[n], newElement);" which my comment
would apply to. I wonder what Daniel intended?

Yes, I missed the '&' in my original post. However Stroustrup's book
says that op[] exists for random access iterators. Is TC++PL wrong?

No, it is not. However, if r is a random access iterator pointing to type T
then all that is required (by 24.1.5, table 76) for the expression r[n] is
that it returns something convertible to type T. I cannot find anything
that requires &r[n] to yield an iterator back into the vector equal to r+n.


Best

Kai-Uwe Bux
 
G

Gavin Deane

Gavin said:
Daniel said:
Thomas Tutone said:
Daniel T. wrote:

myVector.insert( myVector.begin()[n], newElement );

There is no guarantee under the standard that this would work. In
fact, this would only work if std::vector used pointers for iterators.
This may be true for some implementations, but is not true for many
others (including modern versions of gcc).

According to "The C++ Programming Language", op[] is a required operator
for random access iterators. Was Stroustrup wrong?

Unsurprisingly, no he wasn't. 24.1.5 Random Access Iterators agrees
with him. But (and now we're into my interpretation, not Stroustrup's)

myVector.insert( myVector.begin()[n], newElement );

would be equivalent to

myVector.insert( *(myVector.begin() + n), newElement );

not to

myVector.insert( myVector.begin() + n, newElement );

so I don't think it would work.

Sorry. I missed the post where you said you accidentally omitted the &

However, if you meant

myVector.insert( &(myVector.begin()[n]), newElement );

that would be equivalent to

&(*(myVector.begin() + n))

Dereferencing the iterator (myVector.begin() + n) will yield an object.
Unless that type overloads the address-of operator, applying it to that
object will yield an address, not an iterator, and vector::insert needs
an iterator. So I think it will only work if the vector uses pointers
as its iterator type.

Gavin Deane
 
I

Ivan Vecerina

: On Thu, 17 Aug 2006 12:37:26 +0200, Christian Christmann wrote:
[...]
: Sorry, that's just half the truth. What I actually need is:
: A new element has to be inserted into an STL vector before an
: element that is specified by its position in the form of an
: integer value.
[...]
: So, IMHO, the insertion should look like this:
:
: myVector.insert( find( myVector.begin(), myVector.end(),
: myVector.at(n) ), newElement );

What is the desired behavior if another element equal to myVector[n]
is present earlier in the vector?

: Looks somehow cumbersome. ;-)

It seems easier to write:
myVector.insert( myVector.begin()+n, newElement );


hth -Ivan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
474,007
Messages
2,570,266
Members
46,863
Latest member
montyonthebonty

Latest Threads

Top