0

想象有一个帐户对象数组

[
    {"account_number":1,"customer_name":"John"}, 
    {"account_number":2,"customer_name":"Sally"}
]

我想编写一个方法,该方法接受一个帐号并返回匹配的帐户对象。我最初想出了这个

Account AccountDb::get_account(int account_number) {
for (auto &account_element : account_list_) {
    Account account = account_element;

    bool account_found = (account.account_number() == account_number);
    if (account_found) {
        return account;
    }
}

// no matching Account
return ???;
}

但我不确定这是一个好方法,特别是因为如果没有匹配的帐户,它就行不通。任何想法如何解决这个问题?

我对 c++ 很陌生,我正在使用 nlohmann-json 进行 json 解析。任何帮助,将不胜感激。谢谢!

4

0 回答 0