if (__comp(__a, __b))

this is the error referred to the file above.

basically, it's just about this question :
5-6. Rewrite the extract_fails function from §5.1.1/77 so that instead of erasing each failing student from the input vector v, it copies the records for the passing students to the beginning of v, and then uses the resize function to remove the extra elements from the end of v. How does the performance of this version compare with the one in §5.1.1/77?

So, for "remove the extra elements from the end of v. I need to sort the container again for filtering the failed students

sort(students.begin(), students.end(), indexings);
students.resize(size_count);

"indexings" is a predicate with this functions.

bool indexings(Student_info& A, Student_info& B)
{
    A.totals = grade(A.midterm, A.final, A.homework);
    B.totals = grade(B.midterm, B.final, B.homework);

    return A.totals < B.totals;
}

this is the structure that I used.

struct Student_info
{
    std::string name;
    double midterm, final, totals;
    std::vector<double> homework;
};

I don't know either the problems depend upon wrong using of member, parameter, functions (or anything)!

please help. thank you.

Recommended Answers

All 7 Replies

The problem is that indexings() takes its parameters by reference rather than by value or const reference. Unfortunately, there's not really a fix for that unless you remove the updating of totals and put it somewhere else. For example:

for (vector<Student_info>::size_type i = 0; i < students.size(); ++i)
    students[i].totals = grade(students[i].midterm, students[i].final, students[i].homework);

sort(students.begin(), students.end(), indexings);

Where indexings() is modified such that the only thing it does is a comparison:

bool indexings(const Student_info& A, const Student_info& B)
{
    return A.totals < B.totals;
}

Just an additional hint, you don't need to sort the elements to do the removal as asked in the exercise question. Read the question again and see if you can't find a better way to do it, without having to sort the elements. Remember, sorting takes O(N logN) time to execute, but this removal problem can be done in O(N) time.

I'm done with the task. Anyway, about the non-sort algorithm, I'm interested to know too.

I mean, it's actually kind of impossible to remove the extra element by using resize (especially from what I just learned), without the use of sort or "re-arranging" stuff.

because I would end up remove any last element (which is probably not the failed student which I wanted to remove).

Look at the standard function std::remove.

another error!

*__result = __unary_op(*__first);

These code will explain what I'm trying to do:

transform(wordstore.begin(), wordstore.end(),
          back_inserter(url_store), find_urls);

where the parameter is:

vector<string> wordstore;
vector<string> url_store;

if this is not obvious, then, find_urls is a function for finding url in wordstore and then append it into url_store. that's all, then the error above pop-up!

find_urls should be a function with this signature:

string find_urls(const string&);

If it doesn't you'll get an error. I say this because your description of what the function find_urls does is completely at odds with its use as the functor in a std::transform call. Please provide a more complete description (and code) for that function.

hmm, so the transformation demand string instead of the type which same with the type of the container where it's stored into? vector<string>, I think I need to use another algorithm then.

anyway this is the code for find_urls

vector<string> find_urls(const string& s)
{
    vector<string> ret;
    typedef string::const_iterator iter;
    iter b = s.begin(), e = s.end();

    // look through the entire input
    while (b != e) {

        // look for one or more letters followed by ://
        b = url_beg(b, e);

        // if we found it
        if (b != e) {
            // get the rest of the URL
            iter after = url_end(b, e);

            // remember the URL
            ret.push_back(string(b, after));

            // advance b and check for more URLs on this line
            b = after;
        }
    }
    return ret;
}

url_beg & url_end is the function to find the beginning and ending of an url.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.