How to divide two evctor of vectors element by element ?

Dec 28, 2016 at 10:14am
Hi, I have two "vector of a vector"s:

1
2
 
vector < vector < double>> v1, v2;


I want to divide v1[1][0] by v2[1][0], how exactly can I do this ?
Last edited on Dec 28, 2016 at 10:15am
Dec 28, 2016 at 10:42am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using vector_2 = std::vector< std::vector<double> > ;

std::vector<double> divide( const std::vector<double>& a, const std::vector<double>& b )
{
    std::vector<double> result ;

    const std::size_t n = std::min( a.size(), b.size() ) ;
    std::transform( std::begin(a), std::begin(a)+n, std::begin(b),
                    std::back_inserter(result), std::divides<>{} ) ;
    return result ;
}

vector_2 divide( const vector_2& a, const vector_2& b )
{
    vector_2 result ;

    const std::size_t n = std::min( a.size(), b.size() ) ;
    for( std::size_t i = 0 ; i < n ; ++i ) result.push_back( divide( a[i], b[i] ) ) ;

    return result ;
}

http://coliru.stacked-crooked.com/a/d21be463708fd609
Dec 28, 2016 at 7:38pm
Hi, when I compile your code using g++ on my Ubuntu 16.04 terminal I get the following errors:

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
45
46
47
48
49
50
51
52
vec_div.cpp:7:7: error: expected nested-name-specifier before ‘vector_2’
 using vector_2 = std::vector< std::vector<double> > ;
       ^
vec_div.cpp: In function ‘std::vector<double> divide(const std::vector<double>&, const std::vector<double>&)’:
vec_div.cpp:14:21: error: ‘begin’ is not a member of ‘std’
     std::transform( std::begin(a), std::begin(a)+n, std::begin(b),
                     ^
vec_div.cpp:14:36: error: ‘begin’ is not a member of ‘std’
     std::transform( std::begin(a), std::begin(a)+n, std::begin(b),
                                    ^
vec_div.cpp:14:53: error: ‘begin’ is not a member of ‘std’
     std::transform( std::begin(a), std::begin(a)+n, std::begin(b),
                                                     ^
vec_div.cpp:15:62: error: wrong number of template arguments (0, should be 1)
                     std::back_inserter(result), std::divides<>{} ) ;
                                                              ^
In file included from /usr/include/c++/5/string:48:0,
                 from /usr/include/c++/5/bits/locale_classes.h:40,
                 from /usr/include/c++/5/bits/ios_base.h:41,
                 from /usr/include/c++/5/ios:42,
                 from /usr/include/c++/5/ostream:38,
                 from /usr/include/c++/5/iostream:39,
                 from vec_div.cpp:1:
/usr/include/c++/5/bits/stl_function.h:197:12: note: provided fortemplate<class _Tp> struct std::divides’
     struct divides : public binary_function<_Tp, _Tp, _Tp>
            ^
vec_div.cpp:15:63: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
                     std::back_inserter(result), std::divides<>{} ) ;
                                                               ^
vec_div.cpp: At global scope:
vec_div.cpp:19:1: error: ‘vector_2’ does not name a type
 vector_2 divide( const vector_2& a, const vector_2& b )
 ^
vec_div.cpp: In function ‘int main()’:
vec_div.cpp:31:11: error: ‘vector_2’ does not name a type
     const vector_2 a { {1,2,3}, {4,5}, {}, {6,7,8,9} } ;
           ^
vec_div.cpp:32:11: error: ‘vector_2’ does not name a type
     const vector_2 b { {9,8,7}, {6,5}, {}, {4,3,2,1} } ;
           ^
vec_div.cpp:34:16: error: ‘c’ does not name a type
     const auto c = divide( a, b ) ;
                ^
vec_div.cpp:35:22: error: ISO C++ forbids declaration of ‘vec’ with no type [-fpermissive]
     for( const auto& vec : c )
                      ^
vec_div.cpp:35:28: warning: range-based ‘for’ loops only available with -std=c++11 or -std=gnu++11
     for( const auto& vec : c )
                            ^
vec_div.cpp:35:28: error: ‘c’ was not declared in this scope
vec_div.cpp:38:25: warning: range-based ‘for’ loops only available with -std=c++11 or -std=gnu++11
         for( double d : vec ) std::cout << std::fixed << d << ' ' ;


I have the following compilers installed:

g++ 4:5.3.1-1ubuntu1 amd64 GNU C++

g++-5 5.4.0-6ubuntu1~16.04.4 amd64 GNU C++

And I am a beginner in c/c++.
Last edited on Dec 28, 2016 at 7:38pm
Dec 29, 2016 at 12:39am
> ... only available with -std=c++11 ...

Compile with -std=c++11 or -std=c++14

g++ -std=c++14 -O3 -Wall -Wextra -pedantic-errors
Last edited on Dec 29, 2016 at 12:40am
Dec 29, 2016 at 6:10pm
It did work without "-03", thanks.
Topic archived. No new replies allowed.