Correction needed

Pages: 12
The possible implementation of http://www.cplusplus.com/reference/algorithm/search/ is incorrect.

The inner loop:
1
2
3
4
5
   while (*it1==*it2) {    // or: while (pred(*it1,*it2)) for version 2
        if (it2==last2) return first1;
        if (it1==last1) return last1;
        ++it1; ++it2;
    }


This compares the values the iterators are referencing, then afterwords checks to see if the ends of the ranges have been reached. This implies that it is acceptable to dereference last1 and last2.
Last edited on
closed account (E0p9LyTq)
You'll have to contact the admin, no one here can do anything about correcting errors.

Look to the bottom of the page for "Spotted an error? contact us"
Thanks!
closed account (E0p9LyTq)
Is cppreference also wrong? They have "possible implementations" shown that make the same comparison with the end iterators.

https://en.cppreference.com/w/cpp/algorithm/search
It looks like cppref checks for last before dereferencing, if I'm reading it correctly. Seems like it's only an issue on this side.

The actual code that's inside the implementation-part of <algorithm> (specifically, stl_algo.h in GCC) is hard to follow, but it definitely does some checking before it ever calls the predicate and also checks for "++__p == __last2" and "++__current == __last1" as well, similar to cppref but more condensed.
Last edited on
closed account (E0p9LyTq)
It looks like both bits of code, here and at cppreference, are not the end-all of how to implement the algorithm in full. It is pared-down slice of code to show what it does, without the overhead of all the code to make it actually work.

That's my story, and I'm sticking to it.
Yeah, they're both pared down, that's cool. I'm just concerned about sending students here and them seeing a comparison to last *after* a value comparison.
closed account (E0p9LyTq)
The cplusplus C++ reference is seriously outdated, sending students here IMO is really not recommended for reference material. Updates stalled around the time C++14 was either at the stages of being finalized or just afterwards is my guess by what is and isn't in the reference. The curse of a solo person as admin with that nasty real life taking precedence.

cppreference IS updated quite frequently, team effort. They have begun adding C++20 material as it gets put in the "done" out-basket.

Send student there, not here.

Asking questions, needing gentle nudges with written code, but of course here.
Last edited on
Agree with FurryGuy (beat me to the punch).

Also, cppreference.com is a Wiki, so you are more than welcome to fix the reference code (I think). Just be sure to get it right, or some other wikier will be obnoxious about it.

I tend to ignore "reference implementations" just because they tend to be a little off. Teach your students to read the documentation for what it is supposed to do, and remind them that the actual implementation can do it any way it likes, so long as the result matches the documented behavior.


Do go ahead and point out to students that even stuff they see online written by people who should know better will have fencepost errors. It is a distressingly common source of bugs in the software industry.

Perhaps point them to it and ask them if they can figure out what is wrong with it, and what to do to fix it.

Here is how the GNU Standard Library does it (simplified):
1
2
3
4
    while (*it1==*it2) {    // or: while (pred(*it1,*it2)) for version 2
        if (++it2==last2) return first1;
        if (++it1==last1) return last1;
    }

Hope this helps.
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <iostream>
#include <sstream> 
#include <string>
#include <fstream>
#include <cstring>
#include <iomanip>

using namespace std;

struct group
{
	int number;
	string subject;
	string profesor;
	string data;
};

struct studentsingroup
{
	string surename;
	int bal;
};

struct mark
{
	int otl;
	int hor;
	int ydv;
	int neydv;
};



void main() {

	studentsingroup students;
	studentsingroup students_count[25];
	group group1;
	group group_count[9];
	mark marks;

	marks.otl = 0;
	marks.hor = 0;
	marks.ydv = 0;
	marks.neydv = 0;


	ifstream myfile;
	myfile.open("D:/#NILE/#АХМАДУЛЛИН/КФУ/Информатика/Задания/Задание 17/myfile.txt");
	myfile >> group1.number >> group1.subject >> group1.profesor >> group1.data;
	setlocale(LC_ALL, "Rus");

	for (int y = 1; y < 9; ++y)
	{
		myfile >> group1.number >> group1.subject >> group1.profesor >> group1.data;
		cout << group1.number << group1.subject << group1.profesor << group1.data << endl;

		for (int i = 1; i < 15; ++i)
		{

			myfile >> students.surename >> students.bal;
			students_count[i] = students;
			cout << students.surename << students.bal << endl;

			if (students_count[i].bal >= 85)
			{
				marks.otl += 1;
			}
			else if (students.bal > 64)
			{
				marks.hor += 1;
			}
			else if (students.bal > 56)
			{
				marks.ydv += 1;
			}
			else if (students.bal < 57)
			{
				marks.neydv += 1;
			}
		}

	}

	


	

	

	cout << "RESULTS:\nОТЛ: " << marks.otl << endl;
	cout << "ХОР: " << marks.hor << endl;
	cout << "УДВ: " << marks.ydv << endl;
	cout << "НЕУД: " << marks.neydv << "\n" << endl;






	cin.get();
}



@NAxmd
Um, don’t post like that in other people’s threads. If you need help, create your own thread, and be sure to actually ask a question.
closed account (E0p9LyTq)
Perhaps point them to it and ask them if they can figure out what is wrong with it, and what to do to fix it.

Now THAT is a viewpoint I hadn't thought of, but should have. :)
closed account (E0p9LyTq)
It's a funny thing, I do use the reference section a lot. To look up examples of how to use a language feature.

How an implementation MIGHT make the feature work isn't more than a passing intellectual fancy.

I don't even look except on very rare occasions at the "possible implementations."

I don't care HOW it works most of the time, I simply need to remember how to USE it.
Last edited on
^this.

That is the way it is supposed to be. It is the difference between abstraction and too-much-detail.

The power of an abstraction is something that C++ prizes in its specification.
closed account (E0p9LyTq)
One very teeny tiny part of the abstraction that IMO went a bit too far, and so makes finding out what it means to use it in the "normal" way is (one method) how to seed a C++ random engine.

std::chrono::system_clock::now().time_since_epoch().count();

chrono's system_clock has a method to return what the current time is, expressed as a time_point object.

A time_point object has a method for finding the amount of time since "the epoch." Whatever the epoch is. :|

The time since the predefined epoch is a duration object.

A duration object has the count() method that returns the number of ticks represented by the duration object.

cplusplus reference has links to each returned object that made it easy to fall down this rabbit hole. cppreference doesn't.

Both explain what each object does.
Last edited on
Whatever the epoch is
The world was created at midnight 00:00:00 on January 1st, 1970. Everything else that allegedly happened before that date is just a bug.
Last edited on
closed account (E0p9LyTq)
I was being somewhat sarcastic about "the epoch."

On a serious note, is the epoch defined by the C++ standard to be a specified date/time or left up to each implementation.

Visual C++ 2017 has its epoch set to Wed Dec 31 16:00:00 1969

Looks like the latter, though via a quick (emphasis on quick) search on the interwebz I can't find any definitive answer one way or the other.
closed account (z05DSL3A)
https://en.wikipedia.org/wiki/Epoch_(computing)
closed account (E0p9LyTq)
That doesn't answer my question, really.

What does the C++ standard have to say about how "epoch" is implemented?

1. Does it state a specific date/time must be used?

2. Does it state how epoch is implemented left up to the implementation?

3. Is it silent, no word about how it is to be implemented? So again it boils down to each implementation "rolls its own."

Your link, Grey Wolf, shows that it is either 2 or 3. Something every other reference or mention of epoch I've found online implies. But nothing definitive..."The C++ standard says/mandates....." or even some wording that gives a hint.

While I don't know the exact standard specifications every reference I've run across regarding std::random_device indicates (in a roundabout way) what the standard is for implementing it. Because of varying hardware/OS parameters.

"Ok, compiler makers, you decide if you want a truly non-deterministic engine."
Last edited on
closed account (z05DSL3A)
FurryGuy, Sorry, it was not meant to answer your question directly (I don't know what the C++ standard says about what epoch to use) it was just meant to show there is more than one in use.
Pages: 12