Answer:
// Heading
#include <iostream>
#include <vector>
#include <algorithm>
#include
<numeric>
using namespace std;
// Function absolute
int absolute (int x) {
return abs(x);
}
// Unary predicate "replace_5"
bool replace_5 (int x) {
return x>5;
}
// Unary predicate "replace_3"
bool replace_3 (int x) {
return x<3;
}
int main ( )
{
// Declaration
vector<int> V1,V2,V3,V4;
// Declaration of the
iterator pos
vector<int>
:: iterator pos;
// Assignment
V1.push_back(3);
V1.push_back(-2);
V1.push_back(25);
V1.push_back(-1);
V1.push_back(8);
V1.push_back(6);
V2.push_back(1);
V2.push_back(2);
V2.push_back(3);
V2.push_back(4);
V2.push_back(5);
V2.push_back(6);
V2.push_back(7);
V2.push_back(8);
// Display inner product
of
V1 and V2
cout << inner_product(V1.begin( ),V1.end(
),V2.begin(
),0) << endl;
// Display V1
cout << "V1=";
for(pos=V1.begin( );pos!=V1.end( );++pos) {
cout << *pos << " ";
}
cout << endl;
// Sort V1 in the deceasing
order
sort(V1.begin(
),V1.end( ),greater<int>( ) );
// Display V1
cout
<< "V1=";
for(pos=V1.begin( );pos!=V1.end( );++pos) {
cout << *pos << " ";
}
cout << endl;
// Replace the elements of V1
by
their absolute values
transform(V1.begin( ),V1.end( ),V1.begin(
),absolute);
// Display V1
cout
<< "V1=";
for(pos=V1.begin( );pos!=V1.end( );++pos) {
cout << *pos << " ";
}
cout << endl;
// Display V2
cout
<< "V2=";
for(pos=V2.begin( );pos!=V2.end( );++pos) {
cout << *pos << " ";
}
cout << endl;
// Copy V1 at the end of V2
copy(V1.begin(
),V1.end( ),V2.begin( )+2);
// Display V2
cout
<< "V2=";
for(pos=V2.begin( );pos!=V2.end( );++pos) {
cout << *pos << " ";
}
cout << endl;
// Copy V2 into V3 by
inserting
copy(V2.begin( ),V2.end(
),back_inserter(V3));
// Copy V2 into
V4 by inserting in the reverse order
reverse_copy(V2.begin( ),V2.end(
),back_inserter(V4));
// Display V3
cout
<< "V3=";
for(pos=V3.begin( );pos!=V3.end( );++pos) {
cout << *pos << " ";
}
cout << endl;
// Display V4
cout
<< "V4=";
for(pos=V4.begin( );pos!=V4.end( );++pos) {
cout << *pos << " ";
}
cout << endl;
// Replace the values greater
than
5 with 5 in V3
replace_if(V3.begin( ),V3.end( ),replace_5,5);
// Copy V3 in V2 replacing
the values less
than
3 with 3
replace_copy_if(V3.begin(
),V3.end( ),V2.begin(
),replace_3,3);
// Display
V1,V2,V3
cout
<<
"V1=";
for(pos=V1.begin( );pos!=V1.end( );++pos) {
cout << *pos << " ";
}
cout << endl;
cout
<< "V2=";
for(pos=V2.begin( );pos!=V2.end( );++pos) {
cout << *pos << " ";
}
cout << endl;
cout
<< "V3=";
for(pos=V3.begin( );pos!=V3.end( );++pos) {
cout << *pos << " ";
}
cout << endl;
cout
<< "V4=";
for(pos=V4.begin( );pos!=V4.end( );++pos) {
cout << *pos << " ";
}
cout << endl;
// Display the sum of the
elements
of V1, V2, V3, V4
cout << "Sum = " << accumulate(V1.begin(
),V1.end(
),0)
+ accumulate(V2.begin( ),V2.end(
),0) +
accumulate(V3.begin(
),V3.end( ),0)
+ accumulate(V4.begin(
),V4.end(
),0) <<
endl;
}