algorithm 02

What is the correct answer:
a. runtime error
or
b. 1, 0
thank you
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 #include <deque>
#include <iostream>
#include <algorithm>
#include <set>
using namespace std;
class Pocket { 
        int value;
public:
        Pocket(int value):value(value){}
        int getValue() const  { return value; }  
        bool operator < (const Pocket & _Right) const { return value < _Right.value; } };
ostream & operator <<(ostream & stream, const Pocket & pocket) 
{         stream << pocket.getValue();         return stream;	}
int main() {
        Pocket mynumbers1[] = { 3, 9, 0, 2, 1, 4, 5 };
        Pocket mynumbers2[]={Pocket(3),Pocket(2),Pocket(4),Pocket(1)};
        deque<Pocket> d1(mynumbers1, mynumbers1+7);
        set<Pocket> s1(mynumbers1, mynumbers1+7);
        sort(d1.begin(), d1.end());
        sort(mynumbers1, mynumbers1+5);
        cout<<includes(d1.begin(),d1.end(), mynumbers1, mynumbers1+4)<<", "//LINE I
                <<includes(s1.begin(),s1.end(), mynumbers2, mynumbers2+4)//LINE II
                <<endl;        return 0;	}
> What is correct answer:

It is a badly formed program: bool operator < (const Pocket & _Right) const

In addition, some identifiers are reserved for use by C ++ implementations and shall not be used otherwise; no diagnostic is required.

Each identifier that contains a double underscore __ or begins with an underscore followed by an uppercase letter is reserved to the implementation for any use. - IS


There is no correct answer other than this, since the standard specifies: 'no diagnostic is required'

Caveat: the career teacher may find this answer quite unpalatable.
If the error in the program is fixed (use a valid identifier, say right instead of _Right), one would expect the answer to be b. 1, 0

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
#include <deque>
#include <iostream>
#include <algorithm>
#include <set>

using namespace std;

class Pocket {

    int value;

    public:
        Pocket(int value):value(value){}
        int getValue() const  { return value; }
        bool operator < (const Pocket & right) const { return value < right.value; }
};

ostream & operator <<(ostream & stream, const Pocket & pocket)
{ stream << pocket.getValue(); return stream; }

int main() {

    Pocket mynumbers1[] = { 3, 9, 0, 2, 1, 4, 5 };
    Pocket mynumbers2[] = {Pocket(3),Pocket(2),Pocket(4),Pocket(1)};

    deque<Pocket> d1(mynumbers1, mynumbers1+7); // Pockets with values [ 3, 9, 0, 2, 1, 4, 5 ]
    set<Pocket> s1(mynumbers1, mynumbers1+7);  // ordered: Pockets with values [ 0, 1, 2, 3, 4, 5, 9 ]

    sort(d1.begin(), d1.end()); // sorted: Pockets with values [ 0, 1, 2, 3, 4, 5, 9 ]

    sort(mynumbers1, mynumbers1+5); // sort the first 5 elements
        // after that, mynumbers1 has Pockets with values [ 0, 1, 2, 3, 9 ]

    // does the sequence d1 include the first four items in mynumbers1?
    // ie. does [ 0, 1, 2, 3, 4, 5, 9 ] include [ 0, 1, 2, 3 ]?
    // ie. is the sorted range [ 0, 1, 2, 3 ] a subset of the sorted range [ 0, 1, 2, 3, 4, 5, 9 ]?
    // answer: true (1)
    cout << includes( d1.begin(), d1.end(), mynumbers1, mynumbers1+4 ) <<", "//LINE I
    

    // does the sequence s1 include the first four items in mynumbers2?
    // ie. does [ 0, 1, 2, 3, 4, 5, 9 ] include [ 3, 2, 4, 1 ]?
    // ie. is the unsorted range [ 3, 2, 4, 1 ] a subset of the sorted range [ 0, 1, 2, 3, 4, 5, 9 ]?
    // this violates the requirement that the algorithm includes() expects sorted ranges
    // eg. includes() won't find 2 after it has moved to 3 in the (longer) sorted sequence
    // answer: false (0)
         << includes( s1.begin(), s1.end(), mynumbers2, mynumbers2+4 )//LINE II

         << endl;

    return 0;
}

http://coliru.stacked-crooked.com/a/9cbfe02db1fcd9bb
Topic archived. No new replies allowed.