Logo C++ Online Judge

C++

TA:
统计

An image tool stores pending jobs in vector. prepare should fill in any unspecified output widths and mark the jobs as ready. The caller then starts processing using that same batch.

A width of zero means unspecified; other widths are positive integers. The supplied show(const Job&) function only displays a job’s current width and ready flag without changing it.

struct Job {
    std::string name;
    int width;
    bool ready;
};

void prepare(std::vector<Job>& jobs, int default_width) {
    for (auto job : jobs) {
        if (job.width == 0) job.width = default_width;
        job.ready = true;
        show(job);
    }
}

The show(job) call inside prepare prints a.png with width 640 and b.png with width 320, and both jobs are ready. The caller then displays the jobs from its own vector and sees the original widths and readiness flags.

// In the caller:
std::vector<Job> jobs{{"a.png", 0, false}, {"b.png", 320, false}};
prepare(jobs, 640);
for (const auto& job : jobs) show(job);

Now the team adds a Preview button. For the alternatives below, assume prepare has been fixed. The preview should show what preparation would produce with the current default width while leaving the real jobs unchanged. The two alternatives are run separately. After preparation, the display only reads preview.

// A
auto preview = jobs;
prepare(preview, 640);

// B
auto& preview = jobs;
prepare(preview, 640);

提交

Written answers receive AI rubric assessment.

Up to 3 submission attempts per student. Sign in to see your remaining attempts.

Each submission includes all questions. Review your answers before submitting.

Short explanations are welcome. You may leave answers blank; unanswered questions receive zero credit.

1. Why do the two displays differ? How would you change prepare while keeping its intended responsibility? Explain why the change would affect the caller’s jobs. You need not rewrite the function. (55 points)
2. The Preview button should show what preparation would produce with the current default width, while leaving the real jobs unchanged. The two alternatives in the material are run separately. Which version meets that requirement? Explain what batch `prepare` changes in each version and whether the suggestion to “add & after every auto to avoid copying” is sound. (45 points)
Drafts are saved in this browser every 30 seconds.