adjacent_difference Getting 2 Errors

I'm getting the 2 warnings on line 65 and 67. Not sure of what exactly the error meanings are telling me. If someone has an explanation for me, I would appreciate an explanation. I'm self taught and very new. I included the whole source code below. If any other improvements are standing out, just give me an idea of what else you see could be improved. The D5 file has over 12,018,??? lines in it with 5 integers per line. I guess "pointers" would be more efficient?, but like I said, I'm very new, and I haven't a clue on how to use them yet. Plus "pointers" kinda scare me, the memory leaks, I just don't understand them. Anyway, here are what the warnings say.

Warning #1:
/Users/Brian/Documents/CPP-Projects/MyWorkspace/Filter_Testing/main.cpp:65:93: error: cannot convert '__gnu_cxx::__normal_iterator<int*, std::vector<int> >' to 'int' in initialization
int draw_del5 = std::adjacent_difference( d5[a].begin(), d5[a].end(), d5[a].begin() );

Warning #2:
C:/Users/Brian/Documents/CPP-Projects/MyWorkspace/Filter_Testing/main.cpp:67:89: error: no match for 'operator==' (operand types are '__gnu_cxx::__normal_iterator<int*, std::vector<int> >' and 'int')
if ( std::adjacent_difference ( d5[b].begin(), d5[b].end(), d5[b].begin() ) == draw_del5)

====2 errors, 87 warnings==== The 87 warnings are basically derived from the 2 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <array>
#include <cstdlib>
#include <numeric>
#include <iomanip>
#include <chrono>
#include <iterator>

using namespace std;

int main ()
{
    
    std::ifstream infile;
    std::string line;
    std::string d5file;
    
    std::cout << "Enter the Name of your D5 File :  ";
    std::cin >> d5file;
    infile.open(d5file);
    if (!infile) {
        std::cerr << "Problem opening file" << std::endl;
        return 1;
    }
    
    int number_of_lines = 0;
    while (std::getline(infile, line))
        ++number_of_lines;
        
    std::cout << "Your D5 File has " << number_of_lines << " lines" << std::endl;
    infile.clear();
    infile.seekg(0);
    std::cout << "\n";
    std::cout << "Loading D5 File.....\n";
    std::vector<std::vector<int>> d5(number_of_lines,std::vector<int>(5));
    
    while ( !infile.eof()) {
        for ( int i = 0; i < number_of_lines; ++i) {
            for ( int j = 0; j < 5; ++j) {
                infile >> d5 [i][j];
            }
        }
    }
    infile.close();
    
    int draws_reporting;
    std::cout << "How Many Draws to plot the reports?  ";
    std::cin >> draws_reporting;
    long long unsigned int layer_rep_start {1};
    int a {0};
    int line_number {1};
    while ( line_number <= draws_reporting) {
        long long unsigned int b {layer_rep_start};
        int sum_counter {0};
        int draw_sum = std::accumulate ( d5[a].begin ( ), d5[a].end ( ), 0);
        for ( ; b < d5.size () ; ++b, ++sum_counter ){
            if ( std::accumulate ( d5[b].begin ( ), d5[b].end ( ), 0) == draw_sum)
                break;
        }
//        int b {layer_rep_start};   Got "redeclaration of int b" error!  Hope b resets after ++b in each for loop.
        int del5_counter {0};
        int draw_del5 = std::adjacent_difference( d5[a].begin(), d5[a].end(), d5[a].begin() );
        for ( ; b < d5.size() ; ++b, ++ del5_counter ){
            if ( std::adjacent_difference ( d5[b].begin(), d5[b].end(), d5[b].begin() ) == draw_del5)
                break;
        }
    std::cout << line_number << "   " << sum_counter << "  " << del5_counter << endl;
    ++layer_rep_start;
    ++ a;
    ++ line_number;        
    }
    
    return 0;    
}
Last edited on
What do you think adjacent_difference does? And what are you using it for?
One thing it does not do is return an int.

One thing it does not do is return an int.


dutch, Thanks! I can't believe I didn't catch that. I think it returns an array or vector of ints. Hmmm......or maybe not. Here is what I think it does: If I have a vector v { 6, 15, 21, 36, 37}
It returns the values of the difference between each value. So it returns 6 9 6 15 1. I believe that is correct. Now, How can I use this but not include the first difference? Meaning, the difference from 0 to 6. I would like the output to be 9 6 15 1 (only giving output for the actual values, NOT including the difference from zero to the first actual value. I'm using the output for comparison on the rows of the 2d vector called d5.
I think it returns an array or vector of ints.

Instead of guessing, read the reference page.
https://en.cppreference.com/w/cpp/algorithm/adjacent_difference
I wasn't guessing, I should rephrase myself: From my understanding, it returns the values of the differences within an array or vector. Sorry about that mbozzi!! I wasn't clear in my explanation. I did read the reference page...….that's where I got it from. As I stated earlier, I'm really new and a lot of the info on the reference page is still not yet clear to me. It's actually difficult to understand. Not giving up, I'll get there eventually. Thanks for your help and guidance on my question.
You might say that the "result" of adjacent_difference is a vector of differences, but the return value is an iterator which is probably not very useful. It seems like you want something like the following, although I don't understand the point of what you are doing so I'm not sure.

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <numeric>
#include <cstdlib>

using namespace std;

using Vec   = std::vector<int>;
using Vec2D = std::vector<Vec>;

const size_t D5LineSize{5};

Vec2D read_d5_file()
{
    string line;
    cout << "Enter the Name of your D5 File :  ";
    cin >> line;

    ifstream infile(line);
    if (!infile) {
        cerr << "Problem opening D5 file\n";
        exit(1);
    }

    Vec2D d5;

    size_t lineno{0};    
    while (getline(infile, line)) {
        ++lineno;
        // skip blank lines
        if (line.find_first_not_of(" \t\n\r") == line.npos) continue;
        Vec v;
        istringstream sin(line);
        for (int n; sin >> n; ) v.push_back(n);
        if (v.size() != D5LineSize) {
            cerr << "Line " << lineno << " has " << v.size() << " values.\n";
            exit(1);
        }
        d5.push_back(v);
    }

    return d5;
}

int main()
{
    auto d5{read_d5_file()};

    size_t draws;
    cout << "How many draws to plot the reports? ";
    cin >> draws;

    Vec2D diffs;
    Vec sums;

    for (size_t i{0}; i < d5.size(); ++i) {
        sums.push_back(accumulate(d5[i].begin(), d5[i].end(), 0));
        Vec diff(d5[i].size());
        adjacent_difference(d5[i].begin(), d5[i].end(), diff.begin());
        diffs.push_back(diff);
    }

    for (size_t a{0}; a < draws; ++a) {

        int sum_counter{0};
        for (size_t b{a + 1}; b < d5.size(); ++b, ++sum_counter)
            if (sums[b] == sums[a])
                break;

        int del5_counter{0};
        for (size_t b{a + 1}; b < d5.size() ; ++b, ++del5_counter)
            if (equal(diffs[a].begin() + 1, diffs[a].end(), diffs[b].begin() + 1))
                break;
                
        cout << a + 1 << "   " << sum_counter << "  " << del5_counter << '\n';
    }
}

Last edited on
OMG! WOW!!! THANKS dutch!!!! Looks like you spent a lot of time and effort in helping me out. I really do appreciate the help! Looks like it will workout for me. There are some parts I don't quite understand, so give me some time to re-read the code and do a little research for learning purposes. I may have to come back with a couple of questions. Hope you will be around for more help in the future. Hey, a "++" for you! Let me paste this in my IDE and see how it works out.

THANKS AGAIN!!!!
Make sure you use the latest version. The last update is from 6:10, so after your post.

You should also describe what exactly you're trying to do.
The algorithm as it stands looks a little odd.
Last edited on
Got it, will do!!! I tried the one before the last edit, and it worked great. Yeah, guess I should explain in greater detail on what I'm trying to do. It's a very lengthy explanation. Here is the short of it:

The main concern in this part of the program are the different "COUNTERS" that are involved, and their values.
Last edited on
I like the idea of creating a vector for the adjacent_differences! As in the code below.
1
2
3
4
5
6
7
8
9
10
  
    Vec2D diffs;
    Vec sums;

    for (size_t i{0}; i < d5.size(); ++i) {
        sums.push_back(accumulate(d5[i].begin(), d5[i].end(), 0));
        Vec diff(d5[i].size());
        adjacent_difference(d5[i].begin(), d5[i].end(), diff.begin());
        diffs.push_back(diff);
    }

A vector for the sums is not really necessary for my needs. I could exclude that process. I haven't figured it out yet, but is there anyway to create the vector for adj diff to not include the first number? So I wouldn't have to use the " .begin() +1" ?

And I liked the for loop for the adj diff stated below:

1
2
3
4
        int del5_counter{0};
        for (size_t b{a + 1}; b < d5.size() ; ++b, ++del5_counter)
            if (equal(diffs[a].begin() + 1, diffs[a].end(), diffs[b].begin() + 1))
                break;


I just thought if the first number (in the vector) for adj diff weren't there, I was hoping for the " if statement " would read as follows:
if ( diffs[a] == diffs[b] )
break;

That way I could use the vector for a few other for loops that will be included in the program. since the vector for adj diff is a 2d vector, I could access the elements individually for other uses. Ex:
1
2
if ( diffs [a][1] == diffs [b][1] )
     break;   // and so on. 


Thanks!


Change the push_back to emplace_back, which calls the constructor for the element type, in this case a vector of ints, and construct it with begin()+1 and end():

 
        diffs.emplace_back(diff.begin() + 1, diff.end());

Then you can do the if statement the way you said, comparing diffs' element vectors directly.
Thanks dutch! Appreciate the help!
Topic archived. No new replies allowed.