Adding two STL vectors

C

Chris Roth

vector<double> v1;
vector<double> v2;

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.
 
V

Victor Bazarov

Chris said:
vector<double> v1;
vector<double> v2;

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
 
M

Marcin Gil

Chris said:
vector<double> v1;
vector<double> v2;

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<int> v1;
vector<int> v2;

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;
}
 
C

Chris Roth

Victor said:
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.
 
M

Manish

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<int> v1;
vector<int> v2;

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<int> v1;
vector<int> v2;

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
 
V

Victor Bazarov

Manish said:
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<int> v1;
vector<int> v2;

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<int> v1;
vector<int> v2;

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
 
M

Manish

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<int> v1;
vector<int> v2;
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
 
G

Gavin Deane

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<int> v1;
vector<int> v2;
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
 
M

Manish

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<int> v1;
vector<int> v2;
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/library/391xya49(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
 
V

Victor Bazarov

Manish said:
[..]
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/library/391xya49(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
 
K

Kai-Uwe Bux

Manish said:
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<int> v1;
vector<int> v2;
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/library/391xya49(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
 

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

memory leak problem 0
memory leak problem 2
memory leak problem 0
transform 6
Comparing 2 sorted vectors 1
Remove items from a stl vector 1
javaFX Ultimate tic tac toe issues 0
Isn't 'vector' a misnomer? 19

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top