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

help understanding iterators with vector::insert

Hi everyone,

I am trying to get my head around iterators used on vectors. Let's take the
code below:

-------------
std::vector<intv1;
std::vector<intv2;

v1.push_back( 1 );
v1.push_back( 2 );
v1.push_back( 3 );

v2.push_back( 10 );
v2.push_back( 20 );
v2.push_back( 30 );

v1.insert(v1.end(), v2.begin(), v2.begin());

for(int j = 0; j < v1.size(); j++)
std::cout << v1[j] << " ";
-------------

I'm confused why we dont get the output 1 2 3 10

Doesn't the insert method insert to the back of v1 everything between
v2.begin() and v2.begin(), which is namely 10?

I realise that to get the desired result I need to instead use
v1.insert(v1.end(), v2.begin(), v2.begin()+1) but i fail to understand the
logic in this.

I know that v2.begin() points to 10, and v2.end() (which is the same as
v2.begin()+3) points to one-beyond-30.

I guess all it comes down to is if I specify v1.insert(v1.end(), v2.begin(),
v2.begin()+N) it will append to the back of v1 (namely as position
one-beyond-3) the first N values of v2.

Any help on understanding this whole concept would be great!

Cheers,
Peter
Jul 31 '07 #1
5 1791
On Jul 31, 8:23 am, "pwalker" <pwal...@nospam.comwrote:
Hi everyone,

I am trying to get my head around iterators used on vectors. Let's take the
code below:

-------------
std::vector<intv1;
std::vector<intv2;

v1.push_back( 1 );
v1.push_back( 2 );
v1.push_back( 3 );

v2.push_back( 10 );
v2.push_back( 20 );
v2.push_back( 30 );

v1.insert(v1.end(), v2.begin(), v2.begin());

for(int j = 0; j < v1.size(); j++)
std::cout << v1[j] << " ";
-------------

I'm confused why we dont get the output 1 2 3 10

Doesn't the insert method insert to the back of v1 everything between
v2.begin() and v2.begin(), which is namely 10?

I realise that to get the desired result I need to instead use
v1.insert(v1.end(), v2.begin(), v2.begin()+1) but i fail to understand the
logic in this.
In very simple terms, the version of vector::insert that you have used
takes 3 parameters:
param1: The position in vector where the insertion should take place
param2: The position of the first element in the range of elements to
be inserted
param3: The position of first element beyond the range of the elements
to be inserted.

In other words, for the insertion, the half-open interval is used :
[param2, param3)

-N
Jul 31 '07 #2
On Jul 31, 8:44 am, Neelesh Bodas <neelesh.bo...@gmail.comwrote:
On Jul 31, 8:23 am, "pwalker" <pwal...@nospam.comwrote:
Hi everyone,
I am trying to get my head around iterators used on vectors. Let's take the
code below:
-------------
std::vector<intv1;
std::vector<intv2;
v1.push_back( 1 );
v1.push_back( 2 );
v1.push_back( 3 );
v2.push_back( 10 );
v2.push_back( 20 );
v2.push_back( 30 );
v1.insert(v1.end(), v2.begin(), v2.begin());
for(int j = 0; j < v1.size(); j++)
std::cout << v1[j] << " ";
-------------
I'm confused why we dont get the output 1 2 3 10
Doesn't the insert method insert to the back of v1 everything between
v2.begin() and v2.begin(), which is namely 10?
I realise that to get the desired result I need to instead use
v1.insert(v1.end(), v2.begin(), v2.begin()+1) but i fail to understand the
logic in this.

In very simple terms, the version of vector::insert that you have used
takes 3 parameters:
param1: The position in vector where the insertion should take place
param2: The position of the first element in the range of elements to
be inserted
param3: The position of first element beyond the range of the elements
to be inserted.

In other words, for the insertion, the half-open interval is used :
[param2, param3)

-N
Hi,

you need to have this
v1.insert(v1.end(), v2.begin(), v2.begin()+1);

instead of
v1.insert(v1.end(), v2.begin(), v2.begin());

to get the output 1 2 3 10

Thanks

Jul 31 '07 #3
"pwalker" <pw*****@nospam.comwrote in message
news:46********@dnews.tpgi.com.au...
Hi everyone,

I am trying to get my head around iterators used on vectors. Let's take
the code below:

-------------
std::vector<intv1;
std::vector<intv2;

v1.push_back( 1 );
v1.push_back( 2 );
v1.push_back( 3 );

v2.push_back( 10 );
v2.push_back( 20 );
v2.push_back( 30 );

v1.insert(v1.end(), v2.begin(), v2.begin());

for(int j = 0; j < v1.size(); j++)
std::cout << v1[j] << " ";
-------------

I'm confused why we dont get the output 1 2 3 10

Doesn't the insert method insert to the back of v1 everything between
v2.begin() and v2.begin(), which is namely 10?

I realise that to get the desired result I need to instead use
v1.insert(v1.end(), v2.begin(), v2.begin()+1) but i fail to understand the
logic in this.

I know that v2.begin() points to 10, and v2.end() (which is the same as
v2.begin()+3) points to one-beyond-30.

I guess all it comes down to is if I specify v1.insert(v1.end(),
v2.begin(), v2.begin()+N) it will append to the back of v1 (namely as
position one-beyond-3) the first N values of v2.

Any help on understanding this whole concept would be great!
Because the third paramater is one past the one to be inserted, the reason
being so you can use .end() which is one past the end of a container. Thsi
way we can code:

v1.insert( v1.end(), v2.begin(), v2.end() );
which is more common and not have to do
v1.insert( v1.end(), v2.begin(), v2.end() - 1 );
Jul 31 '07 #4
Jim Langston wrote:
:: "pwalker" <pw*****@nospam.comwrote in message
:: news:46********@dnews.tpgi.com.au...
::: Hi everyone,
:::
::: I am trying to get my head around iterators used on vectors.
::: Let's take the code below:
:::
::: -------------
::: std::vector<intv1;
::: std::vector<intv2;
:::
::: v1.push_back( 1 );
::: v1.push_back( 2 );
::: v1.push_back( 3 );
:::
::: v2.push_back( 10 );
::: v2.push_back( 20 );
::: v2.push_back( 30 );
:::
::: v1.insert(v1.end(), v2.begin(), v2.begin());
:::
::: for(int j = 0; j < v1.size(); j++)
::: std::cout << v1[j] << " ";
::: -------------
:::
::: I'm confused why we dont get the output 1 2 3 10
:::
::: Doesn't the insert method insert to the back of v1 everything
::: between v2.begin() and v2.begin(), which is namely 10?
:::
::: I realise that to get the desired result I need to instead use
::: v1.insert(v1.end(), v2.begin(), v2.begin()+1) but i fail to
::: understand the logic in this.
:::
::: I know that v2.begin() points to 10, and v2.end() (which is the
::: same as v2.begin()+3) points to one-beyond-30.
:::
::: I guess all it comes down to is if I specify v1.insert(v1.end(),
::: v2.begin(), v2.begin()+N) it will append to the back of v1
::: (namely as position one-beyond-3) the first N values of v2.
:::
::: Any help on understanding this whole concept would be great!
::
:: Because the third paramater is one past the one to be inserted,
:: the reason being so you can use .end() which is one past the end
:: of a container. Thsi way we can code:
::
:: v1.insert( v1.end(), v2.begin(), v2.end() );
:: which is more common and not have to do
:: v1.insert( v1.end(), v2.begin(), v2.end() - 1 );

And also, the first version works even if v2 is empty. The last one
does definitely not!
Bo Persson
Jul 31 '07 #5
pwalker wrote:
Doesn't the insert method insert to the back of v1 everything between
v2.begin() and v2.begin(), which is namely 10?
There's nothing *between* v2.begin() and v2.begin(). It's a
zero-sized range, and thus contains nothing.
Jul 31 '07 #6

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

Similar topics

2
by: Steven C | last post by:
vector <int> vecThe; set <int> setThe; vecThe.insert (setThe.begin (), setThe.end ()); This gives an error about iterators not of the right type. Is there a way around this or do I have to...
2
by: Chris Kettenbach | last post by:
Sorry for x-post. Using Access 2000, what is wrong with this syntax? INSERT INTO Activity ( IDType, ID, ProjectID, ComponentID, SubComponentID, ProcessDate ) VALUES ( 'Employee', SELECT...
1
by: Lars Erik Hansen | last post by:
I want to write/insert the value of foundFiles into a table. How can i paste a value into a table using VB?? Can someone help? I want to get "FoundFiles(i)" and paste it into a table Set...
2
by: Andy_HR | last post by:
i have a table named tblClanovi now i would like to make a sql insert into command that will add new record in the database... Private Sub btnSpremi_Click() Dim broj Dim imepr broj =...
3
by: david | last post by:
I have following code, but I do not know where the problem is. Thank you for any help. David ----Code--- 'connection conn = New SqlConnection _ ("data source=localhost;integrated...
15
by: kostas | last post by:
Hi, Trying to dump a set<int(s) in a vector (v) my guess was that v.insert(v.end(),s.begin(),s.end()) would be at least as efficient as copy(s.begin(), s.end(), back_inserter(v)) It turn out...
3
by: ALaurie10 | last post by:
Hi I have code below that enables a user to browse for an excel file, import the spreadsheet data, and dump it into a table . I keep receiving 3134 syntax error for "INSERT INTO" statement. Could...
5
by: Boltar | last post by:
Hi Is there a way of inserting and erasing to/from a vector using an array index instead of an iterator or alternatively a way to map an index number to an iterator? eg: vector<intv;
2
by: lenygold via DBMonster.com | last post by:
Hi Everebody: I have a table: CREATE TABLE CROSS_REFERENCE (ROW# INTEGER NOT NULL ,KEY_WORD CHAR(16) NOT NULL ,QUERY_DESCR VARCHAR(330) NOT NULL ,PRIMARY KEY (ROW#,KEY_WORD)); It is a...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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...

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.