当前位置: 首页 > news >正文

C++类模板的运用

使用vector实现一个简单的本地注册登录系统

注册:将账号密码存入vector里面,注意防重复判断

登录:判断登录的账号密码是否正确

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

using namespace std;

// 用户账户结构体
struct UserAccount {
    string username;
    string password;
    
    UserAccount(const string& uname, const string& pwd) 
        : username(uname), password(pwd) {}
};

// 账户管理系统类
class AccountSystem {
private:
    vector<UserAccount> accounts; // 存储所有账户
    
    // 检查用户名是否已存在
    bool isUsernameExist(const string& username) const {
        return any_of(accounts.begin(), accounts.end(), 
            [&username](const UserAccount& acc) {
                return acc.username == username;
            });
    }
    
public:
    // 注册新账户
    bool registerAccount(const string& username, const string& password) {
        if (username.empty() || password.empty()) {
            cout << "用户名和密码不能为空!" << endl;
            return false;
        }
        
        if (isUsernameExist(username)) {
            cout << "用户名已存在,请选择其他用户名!" << endl;
            return false;
        }
        
        accounts.emplace_back(username, password);
        cout << "注册成功!" << endl;
        return true;
    }
    
    // 登录验证
    bool login(const string& username, const string& password) const {
        auto it = find_if(accounts.begin(), accounts.end(), 
            [&username](const UserAccount& acc) {
                return acc.username == username;
            });
        
        if (it == accounts.end()) {
            cout << "用户名不存在!" << endl;
            return false;
        }
        
        if (it->password != password) {
            cout << "密码错误!" << endl;
            return false;
        }
        
        cout << "登录成功!欢迎," << username << "!" << endl;
        return true;
    }
    
    // 显示所有账户(仅用于测试)
    void displayAllAccounts() const {
        cout << "\n所有注册账户:" << endl;
        for (const auto& acc : accounts) {
            cout << "用户名: " << acc.username << ", 密码: " << acc.password << endl;
        }
    }
};

// 主菜单
void displayMenu() {
    cout << "\n===== 账户系统 =====" << endl;
    cout << "1. 注册" << endl;
    cout << "2. 登录" << endl;
    cout << "3. 退出" << endl;
    cout << "请选择操作: ";
}

int main() {
    AccountSystem system;
    int choice;
    
    while (true) {
        displayMenu();
        cin >> choice;
        
        if (choice == 3) {
            cout << "感谢使用,再见!" << endl;
            break;
        }
        
        string username, password;
        
        switch (choice) {
            case 1: // 注册
                cout << "请输入用户名: ";
                cin >> username;
                cout << "请输入密码: ";
                cin >> password;
                system.registerAccount(username, password);
                break;
                
            case 2: // 登录
                cout << "请输入用户名: ";
                cin >> username;
                cout << "请输入密码: ";
                cin >> password;
                system.login(username, password);
                break;
                
            default:
                cout << "无效选择,请重新输入!" << endl;
                break;
        }
        
        // 测试用:显示所有账户
        // system.displayAllAccounts();
    }
    
    return 0;
}

牛客网练习:

相关文章:

  • spring功能汇总
  • 动态规划-杨辉三角
  • Python字典实战: 三大管理系统开发指南(班级+会议+购物车)(附源码)
  • git修改已经push的commit的message
  • Spring Boot中Spring MVC相关配置的详细描述及表格总结
  • C4D XP 粒子动画云端渲染指南
  • 控制理论-传递函数
  • 【docker】
  • 前后端 Mock 技术实践指南【大模型总结】
  • 1、window 下SDL 下载使用, 测试环境搭建
  • 位运算题目:N 天后的牢房
  • [数据集]The Natural Scenes Dataset (NSD)介绍,申请及使用方法
  • 【蓝桥杯】算法笔记6
  • CubeMX配置STM32VET6实现网口通信(无操作系统版-附源码)
  • win11 nacos2.2.1 报错curl -X post 报错找不到和名称X匹配参数;不是命令
  • excel经验
  • (三十七)Dart 中使用 Pub 包管理系统与 HTTP 请求教程
  • leetcode0410. 分割数组的最大值-hard
  • 程序化广告行业(69/89):DMP与PCP系统核心功能剖析
  • STM32看门狗应用实战:独立看门狗与窗口看门狗深度解析(下) | 零基础入门STM32第九十五步
  • 建设动漫网站的目的/网络营销的营销理念
  • dwcs5怎么把做的网站适屏/千锋教育官方网
  • 公司网站建设情况/国内电商平台有哪些
  • 易语言怎么做视频网站/推广网站怎么制作
  • 个人网站的制作步骤/百度seo和sem
  • 微信商城网站哪家做的好/活动营销