subtract values from elements in a vector

Write your question here.

I have two vectors with the values

2.123, 2.111, 9.222
1.22, 4.33, 2.113

I want to subtract the first element of each Vector with each other, the second element of each Vector, and the third. so

abs(2.123-1.22)
abs(2.111-4.33)
abs(9.222-2.113)

I'm pretty sure you have to create a for loop but I'm not sure how to approach this problem as I am new to C++. Thank you all for your help.

The code below is a general concept of what I have

1
2
3
4
5
6
7
8
9
10
std::vector<double> data1, vector<double> data2, vector<double> result;
std::cout << "Enter in data#1 ";
std::cin >> data1;

std::cout << "Enter in data#2 ";
std::cin >> data2;

for (int i=0;i<data1.size();i++){
    //start subtracting the values from each other and have them stored in another Vector
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cassert>
#include <iterator>

int main()
{
    const double inf = 1.0 / 0.0 ;
    const double nan = 0.0 / 0.0 ;
    std::vector<double> data1 { 2.123, 2.111, 9.222, -5.6, 6.8, 0.0, nan } ;
    std::vector<double> data2 { 1.22, 4.33, 2.113, 0.0, 13.2, inf, nan } ;
    // 0 - infinity == -infinity, std::abs(-infinity) is +infinity
    // NaN - NaN == NaN
    
    // ...

    assert( data1.size() <= data2.size() ) ;

    {
        // algorithm (uses iterators): works with all sequence containers
        std::vector<double> result ;

        std::transform( std::begin(data1), std::end(data1),
                         std::begin(data2),
                         std::back_inserter(result),
                         []( double a, double b ) { return std::abs(a-b) ; } ) ;

        for( double d : result ) std::cout << d << ' ' ;
        std::cout << '\n' ;
    }

    {
        // subscript: works with sequence containers which support random access
        std::vector<double> result ;

        for( std::size_t i = 0 ; i < data1.size() ; ++i )
            result.push_back( std::abs( data1[i] - data2[i] ) ) ;

        for( double d : result ) std::cout << d << ' ' ;
        std::cout << '\n' ;
    }
}

http://coliru.stacked-crooked.com/a/f343e4784b19b48a
here's a way without the C++11:

1
2
3
4
5
let's say you have:
[code]
typedef std::vector<double> Data;
Data data1; 
Data data2; 


You can make a function to do it for you:
1
2
3
4
5
6
7
8
9
10
11
Data subtract(Data& data1, Data& data2)
{
  Data output;
  for (Data::iterator it1 = data1.begin(), it2 = data2.begin();
         it1 != data1.end() && it2 != data2.end();
         ++it1,  ++it2 )
  {
    output.push_back( *it1 - *it2 );
  }
  return output;
}


or without iterators (for more simplicity):
1
2
3
4
5
6
7
8
9
Data subtract(Data& data1, Data& data2)
{
  Data output;
  
  for (int i = 0; i < min( data1.size(), data2.size() ); ++i)
    output.push_back( data1[i] - data2[i] );
  
  return output;
}
[/code]
Topic archived. No new replies allowed.