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

Supplying an iterator when inserting into vector??

Why does insert only work when specifying an iterator plus the object to
be inserted:

std::vector<intt;
std::vector<int>::iterator it;
it = t.begin();

t.insert(it,33);

If I use push_back instead I don't need to supply the iterator. But what
are the reason the insert only works with an iterator?
Jun 4 '07 #1
12 4885
desktop wrote:
Why does insert only work when specifying an iterator plus the object
to be inserted:

std::vector<intt;
std::vector<int>::iterator it;
it = t.begin();

t.insert(it,33);

If I use push_back instead I don't need to supply the iterator. But
what are the reason the insert only works with an iterator?
Uh... 'push_back(value)' does 'insert(end(), value)'. How would you
insert into any place except the end if you don't give an iterator?

And if you wanted an overloaded 'insert' without an interator, what
would the difference be between it and 'push_back'? Do you expect it
to only do "insert into beginning"?

Do you not understand that 'insert' is a generic function to place
the value _anywhere_ in the vector?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 4 '07 #2
On Jun 4, 8:29 am, desktop <f...@sss.comwrote:
Why does insert only work when specifying an iterator plus the object to
be inserted:

std::vector<intt;
std::vector<int>::iterator it;
it = t.begin();

t.insert(it,33);

If I use push_back instead I don't need to supply the iterator. But what
are the reason the insert only works with an iterator?
push_back by default inserts at the end of the container while insert
allows you to mention a position where you can insert.

Jun 4 '07 #3
Naresh Rautela wrote:
On Jun 4, 8:29 am, desktop <f...@sss.comwrote:
>Why does insert only work when specifying an iterator plus the object to
be inserted:

std::vector<intt;
std::vector<int>::iterator it;
it = t.begin();

t.insert(it,33);

If I use push_back instead I don't need to supply the iterator. But what
are the reason the insert only works with an iterator?

push_back by default inserts at the end of the container while insert
allows you to mention a position where you can insert.
Ok thanks for the info. When I want to extract an element from the
vector I would use something like:

t.at(0);

But if I have a vector containing my own object that I have defined in a
class 'test' (with a getInt() function returning a private integer) I
would like to use iterators for this:

std::vector<test*t2;
std::vector<test*>::iterator it2;
it2 = t2.begin();
test* tt2 = new test(707);
t2.insert(it2,tt2);
*it2->getInt()

But this does not work. Are iterators only used for inserting and not
extracting custom objects?
Jun 4 '07 #4
On Jun 4, 8:50 am, desktop <f...@sss.comwrote:
Naresh Rautela wrote:
On Jun 4, 8:29 am, desktop <f...@sss.comwrote:
Why does insert only work when specifying an iterator plus the object to
be inserted:
std::vector<intt;
std::vector<int>::iterator it;
it = t.begin();
t.insert(it,33);
If I use push_back instead I don't need to supply the iterator. But what
are the reason the insert only works with an iterator?
push_back by default inserts at the end of the container while insert
allows you to mention a position where you can insert.

Ok thanks for the info. When I want to extract an element from the
vector I would use something like:

t.at(0);

But if I have a vector containing my own object that I have defined in a
class 'test' (with a getInt() function returning a private integer) I
would like to use iterators for this:

std::vector<test*t2;
std::vector<test*>::iterator it2;
it2 = t2.begin();

test* tt2 = new test(707);
t2.insert(it2,tt2);
*it2->getInt()

But this does not work. Are iterators only used for inserting and not
extracting custom objects?- Hide quoted text -

- Show quoted text -
You are storing the start of the vector(it2) and then inserting at the
start of the vector. Then it2 is no longer the start but start + 1.
Since there is no element at start+1, you may be getting a
segmentation violation.

Jun 4 '07 #5
On Jun 4, 8:50 am, desktop <f...@sss.comwrote:
Naresh Rautela wrote:
On Jun 4, 8:29 am, desktop <f...@sss.comwrote:
Why does insert only work when specifying an iterator plus the object to
be inserted:
std::vector<intt;
std::vector<int>::iterator it;
it = t.begin();
t.insert(it,33);
If I use push_back instead I don't need to supply the iterator. But what
are the reason the insert only works with an iterator?
push_back by default inserts at the end of the container while insert
allows you to mention a position where you can insert.

Ok thanks for the info. When I want to extract an element from the
vector I would use something like:

t.at(0);

But if I have a vector containing my own object that I have defined in a
class 'test' (with a getInt() function returning a private integer) I
would like to use iterators for this:

std::vector<test*t2;
std::vector<test*>::iterator it2;
it2 = t2.begin();

test* tt2 = new test(707);
t2.insert(it2,tt2);
*it2->getInt()

But this does not work. Are iterators only used for inserting and not
extracting custom objects?- Hide quoted text -

- Show quoted text -
try this

void main()
{
std::vector<test*t2;
std::vector<test*>::iterator it2;
//it2 = t2.begin();

test* tt2 = new test(707);
t2.insert(it2,tt2);
it2 = t2.begin();
std::cout << (*it2)->getInt();
}

Jun 4 '07 #6
desktop <ff*@sss.comwrote in news:f4**********@news.net.uni-c.dk:
Naresh Rautela wrote:
>On Jun 4, 8:29 am, desktop <f...@sss.comwrote:
>>Why does insert only work when specifying an iterator plus the
object to be inserted:

std::vector<intt;
std::vector<int>::iterator it;
it = t.begin();

t.insert(it,33);

If I use push_back instead I don't need to supply the iterator. But
what are the reason the insert only works with an iterator?

push_back by default inserts at the end of the container while insert
allows you to mention a position where you can insert.

Ok thanks for the info. When I want to extract an element from the
vector I would use something like:

t.at(0);
If you want bounds-checking... if not, then you can also use:

t[0]

Of course, assuming that there is a 0-th object in the vector.
But if I have a vector containing my own object that I have defined in
a class 'test' (with a getInt() function returning a private integer)
I would like to use iterators for this:

std::vector<test*t2;
std::vector<test*>::iterator it2;
it2 = t2.begin();
it2 == t2.begin(). And since the vector is empty, it2 == t2.end() as
well.
test* tt2 = new test(707);
t2.insert(it2,tt2);
With this insertion, you may or may not have invalidated any and all
iterators into the container.
*it2->getInt()
This may or may not be dereferencing an invalid iterator. If it is, then
it is Undefined Behaviour. Anything can happen. And, IIRC, the
precedence rules say you'd have to write that as:

(*it2)->getInt()

-(member selection) has a higher precedence than * (dereferencing).
But this does not work. Are iterators only used for inserting and not
extracting custom objects?
They're used for both. But you need to understand when iterators become
invalid. For vectors, any and all iterators into the vector may become
invalid upon insertion if the insertion causes a reallocation of the
vector.
Jun 4 '07 #7
Naresh Rautela wrote:
> std::vector<test*t2;
std::vector<test*>::iterator it2;
it2 = t2.begin();

test* tt2 = new test(707);
t2.insert(it2,tt2);
*it2->getInt()
You are storing the start of the vector(it2) and then inserting at the
start of the vector. Then it2 is no longer the start but start + 1.
Nonsense. The right answer is that it2 gets invalidated when items
are inserted in the vector.
Even if it didn't get invalidated (eg. because of a reserve()) it
would still point to the first element of the vector. Inserting
elements in the vector doesn't change any existing iterator.

Your answer may be correct for eg. a std::list where iterators
are not invalidated after insertion.
Jun 4 '07 #8
On Jun 4, 9:15 am, Naresh Rautela <nraut...@gmail.comwrote:
On Jun 4, 8:50 am, desktop <f...@sss.comwrote:
std::vector<test*t2;
std::vector<test*>::iterator it2;
it2 = t2.begin();
test* tt2 = new test(707);
t2.insert(it2,tt2);
*it2->getInt()
But this does not work. Are iterators only used for inserting and not
extracting custom objects?- Hide quoted text -

You are storing the start of the vector(it2) and then inserting at the
start of the vector. Then it2 is no longer the start but start + 1.
Actually, after the insertion the iterator it2 is no longer valid.
Inserting into a vector invalidates any pre-existing iterators into
that vector. Try changing the second-to-last line to:

it2 = t2.insert(it2,tt2);

Also, consider using a vector<testinstead of a vector<test*>. As the
code is written now, you (not the vector) are responsible for deleting
all the test objects you instantiate using new. Also, your code will
leak a test instance if vector::insert throws an exception.

Jun 4 '07 #9
Naresh Rautela wrote:
>
void main()
Illformed code. main *ALWAYS* returns int.

int main()
{
std::vector<test*t2;
std::vector<test*>::iterator it2;
//it2 = t2.begin();

test* tt2 = new test(707);
t2.insert(it2,tt2);
it2 = t2.begin();
std::cout << (*it2)->getInt();
}
Jun 4 '07 #10
On Jun 4, 9:56 am, red floyd <no.s...@here.dudewrote:
Naresh Rautela wrote:
void main()

Illformed code. main *ALWAYS* returns int.

int main()
{
std::vector<test*t2;
std::vector<test*>::iterator it2;
//it2 = t2.begin();
test* tt2 = new test(707);
t2.insert(it2,tt2);
it2 = t2.begin();
std::cout << (*it2)->getInt();
}- Hide quoted text -

- Show quoted text -
Thanks for pointing these out and help me learn.

Jun 4 '07 #11
Andre Kostur wrote:
desktop <ff*@sss.comwrote in news:f4**********@news.net.uni-c.dk:
>Naresh Rautela wrote:
>>On Jun 4, 8:29 am, desktop <f...@sss.comwrote:
Why does insert only work when specifying an iterator plus the
object to be inserted:

std::vector<intt;
std::vector<int>::iterator it;
it = t.begin();

t.insert(it,33);

If I use push_back instead I don't need to supply the iterator. But
what are the reason the insert only works with an iterator?
push_back by default inserts at the end of the container while insert
allows you to mention a position where you can insert.
Ok thanks for the info. When I want to extract an element from the
vector I would use something like:

t.at(0);

If you want bounds-checking... if not, then you can also use:

t[0]

Of course, assuming that there is a 0-th object in the vector.
>But if I have a vector containing my own object that I have defined in
a class 'test' (with a getInt() function returning a private integer)
I would like to use iterators for this:

std::vector<test*t2;
std::vector<test*>::iterator it2;
it2 = t2.begin();

it2 == t2.begin(). And since the vector is empty, it2 == t2.end() as
well.
> test* tt2 = new test(707);
t2.insert(it2,tt2);

With this insertion, you may or may not have invalidated any and all
iterators into the container.
> *it2->getInt()

This may or may not be dereferencing an invalid iterator. If it is, then
it is Undefined Behaviour. Anything can happen.
To avoid this I could just update the iterator after insertion with :
it2 = t2.begin() As long as I make sure to update the iterator
afterwards I am sure that it is still valid right?
Jun 4 '07 #12

desktop wrote in message ...
Andre Kostur wrote:
desktop wrote in ....
But if I have a vector containing my own object that I have defined in
a class 'test' (with a getInt() function returning a private integer)
I would like to use iterators for this:

std::vector<test*t2;
std::vector<test*>::iterator it2;
it2 = t2.begin();
it2 == t2.begin(). And since the vector is empty, it2 == t2.end() as
well.
test* tt2 = new test(707);
t2.insert(it2,tt2);
With this insertion, you may or may not have invalidated any and all
iterators into the container.
*it2->getInt()
This may or may not be dereferencing an invalid iterator. If it is,
then
it is Undefined Behaviour. Anything can happen.

To avoid this I could just update the iterator after insertion with :
it2 = t2.begin() As long as I make sure to update the iterator
afterwards I am sure that it is still valid right?
Just for kicks, try:

// .... fill the vector with something
it2 = t2.begin(); // update the iterator
// *it2->getInt(); // ?? *( it2->getInt() ) ??
int num = (*it2)->getInt();

--
Bob R
POVrookie
Jun 5 '07 #13

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

Similar topics

8
by: ding feng | last post by:
I am a beginner. So this question could be very stupid. Would anyone help me to solve this problem? A formatted txt file is read. Then i need to look into a vector who is a member of a class to...
5
by: Ernst Murnleitner | last post by:
Hello, is it possible to derive from std::vector and derive also its iterator? If I do it like in the example below, I get a problem when I need the begin of the vector: begin() returns the...
3
by: Rüdiger Knörig | last post by:
I've a nasty problem here (g++ 3.4/4.0): I've a template datatype mltreenode<T> from which I instanced a STL vector vector< mltreenode<T> * > without any problems. Calling methods on this object...
3
by: Joachim Klassen | last post by:
Hi all, first apologies if this question looks the same as another one I recently posted - its a different thing but for the same szenario:-). We are having performance problems when...
0
by: Nick | last post by:
How do I go about setting a default value for a row when inserting a new record with the DetailsView ? Effectively I need to access the underlying data source and set a column to be a default value...
3
by: PengYu.UT | last post by:
Suppose I want a ring_iterator, which is almost like the vector::iterator. But it will equals begin(), when it passed the end(). It is easy to write an adaptor for it. But I'm wondering if there is...
1
by: atomik.fungus | last post by:
Hi, I'm re-writting my matrix class to practice my programming and the computer doesn't let me compile the next code: ( this example come from the constructor of the class) //the matrix is made...
1
by: DanielLauJJ | last post by:
When inserting a record into a table, I want SQL Server to generate a number automatically for the Primary Key. (e.g. OrderID is 1, 2, 3 and so on) How to do it? (This behavior is similar to the...
2
by: Jajjo | last post by:
Hey all, First time posting here and just a simple question to begin with. I'm trying to create an iterator for a vector of POINT objects and for some reason, I'm getting this error: error...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
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...
0
agi2029
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,...

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.