Hi everyone,
I am trying to get my head around iterators used on vectors. Let's take the
code below:
-------------
std::vector<int v1;
std::vector<int v2;
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.en d(), 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.en d(), 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.en d(), 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 5 1807
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<int v1;
std::vector<int v2;
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.en d(), 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.en d(), 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
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<int v1;
std::vector<int v2;
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.en d(), 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.en d(), 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.en d(), v2.begin(), v2.begin()+1);
instead of
v1.insert(v1.en d(), v2.begin(), v2.begin());
to get the output 1 2 3 10
Thanks
"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<int v1;
std::vector<int v2;
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.en d(), 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.en d(), 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.en d(),
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 );
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<int v1;
::: std::vector<int v2;
:::
::: 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.en d(), 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.en d(), 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.en d(),
::: 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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 set up a for loop and do
vecThe.push_back ()?
However I can do this:
|
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 EmployeeID FROM tbl1104Mess , 6, 28,341, #11/02/04#
)
The desired result is to place 1 record into Activity for each 1 record in
tbl1104Mess.
|
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 fs = Application.FileSearch
With fs
|
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 = Nz(DMax("ClanID", "tblClanovi")) + 1
Me!txtClanBroj = broj
imepr = Me!txtImePrezime
|
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 security=true;initial
catalog=DemoCRQdatabase")
| |
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 that it is almost two times slower (even without using
reserve() before copy) and that's because it traverses the set twice.
I'm posting it as an exception to the usual (and usually correct)
advise
"Prefer member functions to algorithms".
|
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 someone please help me out? Also when I copy and paste this code into a module in a new database, I get an error message for the "Dim dlgPickFiles As Office.FileDialog" statement. I didn't get this error message in the old database, and don't...
|
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;
|
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 cross reference table to my CATALOG Table based on key words.
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |