can't deference out of range vector iterator报错

输入visit时候报错,page和user指令运行正常。
该修改哪部分代码呢

#include<iostream>
#include<algorithm>
#include<string>
#include<map>
#include<vector>

using namespace std;

struct Page {
    int id;
    string path;
    int counter;
    Page(int id, string path) {
        this->id = id;
        this->path = path;
        counter = 0;
    };
};

// This function can facilitate sorting
bool operator<(const Page& a, const Page& b) {
    return (a.id < b.id);
}

vector<Page> pages;

struct User {
    int id;
    vector<string> visits;
    User(int id) {
        this->id = id;
    };
    void add_visit(int page_id) {
        Page p(page_id, "");
        vector<Page>::iterator iter = lower_bound(pages.begin(), pages.end(), p);
        if (iter->id == page_id)
            visits.push_back(iter->path);
    };
    int size() const {
        return visits.size();
    };
    void print_visits() {
        sort(visits.begin(), visits.end());
        vector<string>::iterator iter;
        for (iter = visits.begin(); iter != visits.end(); iter++) {
            cout << "- " << *iter << endl;
        }
    }
};

vector<User> users;

// Please implement the following function to facilitate the sorting of users
bool operator<(const User& a, const User& b) {
    return a.visits.size() < b.visits.size();
}

// Please implement the following function
void add_page(const Page& p) {
    pages.push_back(p);
}

// Please implement the following function
bool cmp_page_count(const Page& a, const Page& b) {
   return a.counter > b.counter;

  
}

// Please implement the following function
void print_pages(int number) {
    for (int i = 0; i < number; i++) {
        cout << pages[i].counter << ":/" << pages[i].path << endl;
    }
}

// Please implement the following function
void add_user(User u) {
    users.push_back(u);
}

// Please implement the following function
void add_visit(int page_id) {
    vector<User>::iterator iter = users.end();
    iter->add_visit(page_id);
}

// Please implement the following function
void print_users(int number) {
    for (int i = 0; i < number; i++) {
        cout << users[i].size() << ":" << users[i].id << endl;
        users[i].print_visits();
    }
}



int main() {

    string type;
    while (cin >> type) {
        if (type == "USER") {
            int user_id;
            cin >> user_id;
            User u(user_id);
            add_user(u);
        }
        else if (type == "PAGE") {
            int page_id;
            string page_path;
            cin >> page_id;
            cin >> page_path;
            Page p(page_id, page_path);
            add_page(p);
        }
        else if (type == "VISIT") {
            int page_id;
            cin >> page_id;
            Page p(page_id, "");
            vector<Page>::iterator iter = lower_bound(pages.begin(), pages.end(), p);
            if (iter->id == p.id) {
                iter->counter++;
            }
            add_visit(p.id);
        }
    }
    sort(pages.begin(), pages.end(), cmp_page_count);
    cout << "*** 5 most popular pages ***" << endl;
    print_pages(5);
    sort(pages.begin(), pages.end());

    sort(users.begin(), users.end());
    cout << "*** 5 most active users ***" << endl;
    print_users(5);

    return 0;

}


img


标注// Please implement the following function是我自己写的代码,所以问题也大概在这范围内

你所有的迭代器,再使用前没有判断是否合法:
(1)第36行
if (iter->id == page_id)
改成
if ( iter != pages.end() && iter->id == page_id)
(2)第84行 add_visit函数中
vector< User >::iterator iter = users.end();这里,iter指向了vector的末尾,迭代器的末尾是vector最后一个元素的下一个位置,也就是说iter已经不在vector中了,所以,在第85行使用 iter->add_visit(page_id); 就是错误的。这个问题会让你的程序在运行visit指令的时候崩溃。这个函数的逻辑有问题,根据你的需要修改一下。我不知道你要实现什么功能,没法帮你改了。
(3)第121行,也是在使用前没有判断是否合法
if (iter->id == p.id)改成
if ( iter != pages.end() && iter->id == p.id)

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632