473,398 Members | 2,525 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,398 software developers and data experts.

Adding two STL vectors

vector<doublev1;
vector<doublev2;

What is the best way to add v1 to v2? (v2 = v1+v2 that is)? Simply
iterate through, or can an algorithm like transform be used? I have used
transform to add a single value to a vector, but not two vectors together.

Thank you c++ board.
Feb 23 '07 #1
11 15794
Chris Roth wrote:
vector<doublev1;
vector<doublev2;

What is the best way to add v1 to v2? (v2 = v1+v2 that is)? Simply
iterate through, or can an algorithm like transform be used? I have
used transform to add a single value to a vector, but not two vectors
together.
v2.resize(v1.size());
std::transform(v1.begin(), v1.end(), v2.begin(), v2.begin(),
plus<double>());

should do it...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Feb 23 '07 #2
Chris Roth wrote:
vector<doublev1;
vector<doublev2;

What is the best way to add v1 to v2? (v2 = v1+v2 that is)? Simply
iterate through, or can an algorithm like transform be used? I have used
transform to add a single value to a vector, but not two vectors together.
Single value: vector::push_back should be ok :)
To join two vectors you could use

vector::insert(), like here

#include <iostream>
#include <vector>
#include <iterator>

using namespace std;

int main()
{
vector<intv1;
vector<intv2;

v1.push_back(5);
v2.push_back(6);
v2.push_back(7);

v1.insert(v1.end(), v2.begin(), v2.end());
copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));

return 0;
}
Feb 23 '07 #3
Marcin Gil wrote:
Chris Roth wrote:
>vector<doublev1;
vector<doublev2;

What is the best way to add v1 to v2? (v2 = v1+v2 that is)? Simply
iterate through, or can an algorithm like transform be used? I have
used transform to add a single value to a vector, but not two vectors
together.
Ups.. Looks like I didn't catch what you mean :)

Cheers,
-Marcin
Feb 23 '07 #4
Victor Bazarov wrote:
Chris Roth wrote:
>>vector<doublev1;
vector<doublev2;

What is the best way to add v1 to v2? (v2 = v1+v2 that is)? Simply
iterate through, or can an algorithm like transform be used? I have
used transform to add a single value to a vector, but not two vectors
together.


v2.resize(v1.size());
std::transform(v1.begin(), v1.end(), v2.begin(), v2.begin(),
plus<double>());

should do it...

V
Didn't see the option to use two iterators in the documentation.

Note to self: RTM

Thanks and sorry.
Feb 23 '07 #5
On Feb 23, 8:40 am, Marcin Gil <marcin....@NOSPAMgmail.comwrote:
Chris Roth wrote:
vector<doublev1;
vector<doublev2;
What is the best way to add v1 to v2? (v2 = v1+v2 that is)? Simply
iterate through, or can an algorithm like transform be used? I have used
transform to add a single value to a vector, but not two vectors together.

Single value: vector::push_back should be ok :)
To join two vectors you could use

vector::insert(), like here

#include <iostream>
#include <vector>
#include <iterator>

using namespace std;

int main()
{
vector<intv1;
vector<intv2;

v1.push_back(5);
v2.push_back(6);
v2.push_back(7);

v1.insert(v1.end(), v2.begin(), v2.end());
copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));

return 0;

}- Hide quoted text -

- Show quoted text -
I wonder how transform works, since it seems to be more
efficient(faster) than vector insert?

#include <iostream>
#include <vector>
#include <iterator>

using namespace std;

int main(int argc, char *argv[])
{
if (argc < 2) {
cout << "invalid number of args: " << argc << endl;
exit(1);
}

vector<intv1;
vector<intv2;

v1.push_back(5);
v2.push_back(6);
v2.push_back(7);

for (int i = 0; i < atoi(argv[1]); i++) {
// Alternative 1
//v1.insert(v1.end(), v2.begin(), v2.end());

// Alternative 2
v2.resize(v1.size());
std::transform(v1.begin(), v1.end(), v2.begin(), v2.begin(),
plus<int>());
}

return 0;
}

Alternative 1
time a.out 100000

real 0m0.52s
user 0m0.48s
sys 0m0.01s

Alternative 2
time a.out 100000

real 0m0.35s
user 0m0.30s
sys 0m0.02s
Feb 23 '07 #6
Manish wrote:
On Feb 23, 8:40 am, Marcin Gil <marcin....@NOSPAMgmail.comwrote:
>Chris Roth wrote:
>>vector<doublev1;
vector<doublev2;
>>What is the best way to add v1 to v2? (v2 = v1+v2 that is)? Simply
iterate through, or can an algorithm like transform be used? I have
used transform to add a single value to a vector, but not two
vectors together.

Single value: vector::push_back should be ok :)
To join two vectors you could use

vector::insert(), like here

#include <iostream>
#include <vector>
#include <iterator>

using namespace std;

int main()
{
vector<intv1;
vector<intv2;

v1.push_back(5);
v2.push_back(6);
v2.push_back(7);

v1.insert(v1.end(), v2.begin(), v2.end());
copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));

return 0;

}- Hide quoted text -

- Show quoted text -

I wonder how transform works, since it seems to be more
efficient(faster) than vector insert?

#include <iostream>
#include <vector>
#include <iterator>

using namespace std;

int main(int argc, char *argv[])
{
if (argc < 2) {
cout << "invalid number of args: " << argc << endl;
exit(1);
}

vector<intv1;
vector<intv2;

v1.push_back(5);
v2.push_back(6);
v2.push_back(7);

for (int i = 0; i < atoi(argv[1]); i++) {
// Alternative 1
//v1.insert(v1.end(), v2.begin(), v2.end());

// Alternative 2
v2.resize(v1.size());
std::transform(v1.begin(), v1.end(), v2.begin(), v2.begin(),
plus<int>());
}

return 0;
}

Alternative 1
time a.out 100000

real 0m0.52s
user 0m0.48s
sys 0m0.01s

Alternative 2
time a.out 100000

real 0m0.35s
user 0m0.30s
sys 0m0.02s
For 'alternative 1' you're doing 100000 inserts of 2 [more] elements
into 'v1', making it reallocate itself at least log2(100000) times
(or some such number). In 'alternative 2', 'resize' is executed
_always_ to the same size (1 from 2, which surely does not require
reallocation) and transform only changes 1 value (yes, 100000 times).
Your benchmark is not valid, unless I missed something.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Feb 23 '07 #7
On Feb 23, 9:33 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Manish wrote:
On Feb 23, 8:40 am, Marcin Gil <marcin....@NOSPAMgmail.comwrote:
Chris Roth wrote:
vector<doublev1;
vector<doublev2;
>What is the best way to add v1 to v2? (v2 = v1+v2 that is)? Simply
iterate through, or can an algorithm like transform be used? I have
used transform to add a single value to a vector, but not two
vectors together.
Single value: vector::push_back should be ok :)
To join two vectors you could use
vector::insert(), like here
#include <iostream>
#include <vector>
#include <iterator>
using namespace std;
int main()
{
vector<intv1;
vector<intv2;
v1.push_back(5);
v2.push_back(6);
v2.push_back(7);
v1.insert(v1.end(), v2.begin(), v2.end());
copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));
return 0;
}- Hide quoted text -
- Show quoted text -
I wonder how transform works, since it seems to be more
efficient(faster) than vector insert?
#include <iostream>
#include <vector>
#include <iterator>
using namespace std;
int main(int argc, char *argv[])
{
if (argc < 2) {
cout << "invalid number of args: " << argc << endl;
exit(1);
}
vector<intv1;
vector<intv2;
v1.push_back(5);
v2.push_back(6);
v2.push_back(7);
for (int i = 0; i < atoi(argv[1]); i++) {
// Alternative 1
//v1.insert(v1.end(), v2.begin(), v2.end());
// Alternative 2
v2.resize(v1.size());
std::transform(v1.begin(), v1.end(), v2.begin(), v2.begin(),
plus<int>());
}
return 0;
}
Alternative 1
time a.out 100000
real 0m0.52s
user 0m0.48s
sys 0m0.01s
Alternative 2
time a.out 100000
real 0m0.35s
user 0m0.30s
sys 0m0.02s

For 'alternative 1' you're doing 100000 inserts of 2 [more] elements
into 'v1', making it reallocate itself at least log2(100000) times
(or some such number). In 'alternative 2', 'resize' is executed
_always_ to the same size (1 from 2, which surely does not require
reallocation) and transform only changes 1 value (yes, 100000 times).
Your benchmark is not valid, unless I missed something.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask- Hide quoted text -

- Show quoted text -
My attempt is to compare the efficiency of the two alternatives.
How else should they be compared?

Regards,
Manish

Feb 23 '07 #8
On 23 Feb, 14:53, "Manish" <mar...@gmail.comwrote:
On Feb 23, 9:33 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Manish wrote:
#include <iostream>
#include <vector>
#include <iterator>
using namespace std;
int main(int argc, char *argv[])
{
if (argc < 2) {
cout << "invalid number of args: " << argc << endl;
exit(1);
}
vector<intv1;
vector<intv2;
v1.push_back(5);
v2.push_back(6);
v2.push_back(7);
for (int i = 0; i < atoi(argv[1]); i++) {
// Alternative 1
//v1.insert(v1.end(), v2.begin(), v2.end());
// Alternative 2
v2.resize(v1.size());
std::transform(v1.begin(), v1.end(), v2.begin(), v2.begin(),
plus<int>());
}
return 0;
}
For 'alternative 1' you're doing 100000 inserts of 2 [more] elements
into 'v1', making it reallocate itself at least log2(100000) times
(or some such number). In 'alternative 2', 'resize' is executed
_always_ to the same size (1 from 2, which surely does not require
reallocation) and transform only changes 1 value (yes, 100000 times).
Your benchmark is not valid, unless I missed something.
My attempt is to compare the efficiency of the two alternatives.
How else should they be compared?
Unless *I* missed something, your two "alternatives" are not
alternatives at all. They do completely different things. Have you
actually looked at what happens to the vectors - what they contain
after each of your alternatives? Comparing them isn't meaningful.

Gavin Deane

Feb 23 '07 #9
On Feb 23, 10:05 am, "Gavin Deane" <deane_ga...@hotmail.comwrote:
On 23 Feb, 14:53, "Manish" <mar...@gmail.comwrote:


On Feb 23, 9:33 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Manish wrote:
#include <iostream>
#include <vector>
#include <iterator>
using namespace std;
int main(int argc, char *argv[])
{
if (argc < 2) {
cout << "invalid number of args: " << argc << endl;
exit(1);
}
vector<intv1;
vector<intv2;
v1.push_back(5);
v2.push_back(6);
v2.push_back(7);
for (int i = 0; i < atoi(argv[1]); i++) {
// Alternative 1
//v1.insert(v1.end(), v2.begin(), v2.end());
// Alternative 2
v2.resize(v1.size());
std::transform(v1.begin(), v1.end(), v2.begin(), v2.begin(),
plus<int>());
}
return 0;
}
For 'alternative 1' you're doing 100000 inserts of 2 [more] elements
into 'v1', making it reallocate itself at least log2(100000) times
(or some such number). In 'alternative 2', 'resize' is executed
_always_ to the same size (1 from 2, which surely does not require
reallocation) and transform only changes 1 value (yes, 100000 times).
Your benchmark is not valid, unless I missed something.
My attempt is to compare the efficiency of the two alternatives.
How else should they be compared?

Unless *I* missed something, your two "alternatives" are not
alternatives at all. They do completely different things. Have you
actually looked at what happens to the vectors - what they contain
after each of your alternatives? Comparing them isn't meaningful.

Gavin Deane- Hide quoted text -

- Show quoted text -
transform: "Applies a specified function object to each element in a
source range or to a pair of elements from two source ranges and
copies the return values of the function object into a destination
range." http://msdn2.microsoft.com/en-us/lib...49(VS.80).aspx

If I understand correctly, the 'transform' is being used to do the
following:
v2 = v1 + v2

Now, the Alternative 1, "v1.insert(v1.end(), v2.begin(), v2.end());"
can also be used to achieve the same result. Right?

Regards,
Manish


Feb 23 '07 #10
Manish wrote:
[..]
transform: "Applies a specified function object to each element in a
source range or to a pair of elements from two source ranges and
copies the return values of the function object into a destination
range." http://msdn2.microsoft.com/en-us/lib...49(VS.80).aspx

If I understand correctly, the 'transform' is being used to do the
following:
v2 = v1 + v2

Now, the Alternative 1, "v1.insert(v1.end(), v2.begin(), v2.end());"
can also be used to achieve the same result. Right?
8-| No.

Could you please elaborate on what makes you think 'insert' could
perform element-wise _arithmetic addition_ for two vectors of int?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Feb 23 '07 #11
Manish wrote:
On Feb 23, 10:05 am, "Gavin Deane" <deane_ga...@hotmail.comwrote:
>On 23 Feb, 14:53, "Manish" <mar...@gmail.comwrote:


On Feb 23, 9:33 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Manish wrote:
#include <iostream>
#include <vector>
#include <iterator>
using namespace std;
int main(int argc, char *argv[])
{
if (argc < 2) {
cout << "invalid number of args: " << argc << endl;
exit(1);
}
vector<intv1;
vector<intv2;
v1.push_back(5);
v2.push_back(6);
v2.push_back(7);
for (int i = 0; i < atoi(argv[1]); i++) {
// Alternative 1
//v1.insert(v1.end(), v2.begin(), v2.end());
// Alternative 2
v2.resize(v1.size());
std::transform(v1.begin(), v1.end(), v2.begin(), v2.begin(),
plus<int>());
}
return 0;
}
For 'alternative 1' you're doing 100000 inserts of 2 [more] elements
into 'v1', making it reallocate itself at least log2(100000) times
(or some such number). In 'alternative 2', 'resize' is executed
_always_ to the same size (1 from 2, which surely does not require
reallocation) and transform only changes 1 value (yes, 100000 times).
Your benchmark is not valid, unless I missed something.
My attempt is to compare the efficiency of the two alternatives.
How else should they be compared?

Unless *I* missed something, your two "alternatives" are not
alternatives at all. They do completely different things. Have you
actually looked at what happens to the vectors - what they contain
after each of your alternatives? Comparing them isn't meaningful.

Gavin Deane- Hide quoted text -

- Show quoted text -

transform: "Applies a specified function object to each element in a
source range or to a pair of elements from two source ranges and
copies the return values of the function object into a destination
range." http://msdn2.microsoft.com/en-us/lib...49(VS.80).aspx

If I understand correctly, the 'transform' is being used to do the
following:
v2 = v1 + v2

Now, the Alternative 1, "v1.insert(v1.end(), v2.begin(), v2.end());"
can also be used to achieve the same result. Right?
No: Alternative 1 concatenates vectors (increasing the length). What you
want is to add them elementwise (that will _not_ increase the length, but
affect the stored values).
Best

Kai-Uwe Bux
Feb 23 '07 #12

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

Similar topics

4
by: Noo | last post by:
Hi. I've got some code that uses vectors fairly extensively and it needs to be efficient. Therefore I'm using reserve() quite a bit. However what other functions (are supposed to) change the...
5
by: Simon Elliott | last post by:
I'd like to do something along these lines: struct foo { int i1_; int i2_; }; struct bar {
4
by: Dr. J.K. Becker | last post by:
Hi all, I have vectors that holds pointers to other vectors, like so: vector<whatever> x; vector<whatever*> z; z=&x; Now I add something to x
34
by: Adam Hartshorne | last post by:
Hi All, I have the following problem, and I would be extremely grateful if somebody would be kind enough to suggest an efficient solution to it. I create an instance of a Class A, and...
10
by: mahurshi | last post by:
I've got a gate structure that looks like this /* Defining sGATE structure */ struct sGATE { string name; vector<int> input; int output; };
5
by: Andy Leszczynski | last post by:
Hi, Short question: why (1,"abc",0.3)+(2,"def",10.2) != (3,"abcdef",10.5)? How to elegantly achieve (3,"abcdef",10.5) as a result of addition ... Andy
4
japuentem
by: japuentem | last post by:
Hi I have a main Vector and i'm adding another vectors inside the main, the question is ¿how to get each value of the secondary vectors?. I hope you understand the question.
1
by: Rob | last post by:
How would I do this? I want to be able to handle vectors of many different types of data and vectors that can contain any number of other vectors of data. Currently, I have a templated...
2
by: Laura Wilkinson | last post by:
I've got many differences between vectors/strings and arrays but I need more details. I guess I don't really understand it enough to do this but this is what I've concluded so far; An array is a...
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...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.