在 C++ 中搜索结构向量并输出元素

Searching through struct vector and outputting the element in C++

好的,我的结构如下所示:

struct Account{
    int accountNumber;
    string lastName;
    string firstName;
    double accountBalance;
    bool active;
};

而且我已经将帐户硬编码到其中,即

vector<Account> subAccount = *account;
Account newA;
newA.accountNumber = 2345;
newA.lastName = "test1";
newA.firstName = "test1";
newA.accountBalance = 100.00;
newA.active = true;

subAccount.push_back(newA);

还有第二个帐户:

Account newB;
newB.accountNumber = 1234;
newB.lastName = "test2";
newB.firstName = "test2";
newB.accountBalance = 178.1;
newB.active = true;

subAccount.push_back(newB);

*account = subAccount;

所以现在我需要能够在 type void 函数中打印个人帐户。当系统提示用户输入帐号时,搜索 vector/struct 的最佳方法是什么?我试过这样做:

for (int i = 0; i < subAccount.size(); i++){

    if (TempActNum == subAccount[i].accountNumber) {
        cout << "Account Number: " << subAccount[i].accountNumber << "      Balance: " << subAccount[i].accountBalance
             << endl;
        cout << "Last Name: " << subAccount[i].lastName << "        First Name: " << subAccount[i].firstName << endl;
    break;
    }

    if (TempActNum != subAccount[i].accountNumber) {
            cout << "This account does not exist." << endl;
    }

} }

此方法的问题在于,如果我输入第二个帐号 (1234),它会打印 "This account does not exist." 以及所有帐户详细信息。那么什么是更好的搜索和打印方式呢?或者什么可以解决我的问题?谢谢你的时间。

auto it = std::find_if(
  begin(subAccount), end(subAccount),
  [&](auto&& account){ return TempActNum == account.accountNumber; }
);
if (it == end(subAccount)) {
  std::cout << "This account does not exist.\n";
} else {
  std::cout << "Account " << it->accountNumber << " exists.\n";
}

, but relatively easy to backport to .

你的问题是 non existance 的测试在循环的中间。

for (int i = 0; i < subAccount.size(); i++){

    if (TempActNum == subAccount[i].accountNumber) {
        cout << "Account Number: "
             << subAccount[i].accountNumber 
             << "      Balance: " 
             << subAccount[i].accountBalance
             << endl;
        cout << "Last Name: "
             << subAccount[i].lastName 
             << "        First Name: " 
             << subAccount[i].firstName 
             << endl;
        break;
    }

    if (TempActNum != subAccount[i].accountNumber) {
            cout << "This account does not exist." << endl;
    }

}

只有把所有账号都循环完才能判断是否没有找到账号。所以创建一个临时变量来表示它已找到。

bool found = false;
for (int i = 0; i < subAccount.size(); i++){

    if (TempActNum == subAccount[i].accountNumber) {
        cout << "Account Number: "
             << subAccount[i].accountNumber 
             << "      Balance: " 
             << subAccount[i].accountBalance
             << endl;
        cout << "Last Name: "
             << subAccount[i].lastName 
             << "        First Name: " 
             << subAccount[i].firstName 
             << endl;
        found == true; // You found it.
        break;
    }

}
// After you have checked all the accounts (or after breaking out)
// you can print this statement if it was not found.
if (!found) {
        cout << "This account does not exist." << endl;
}

另请注意,我们可以简化您的帐户创建:

vector<Account> subAccount = {
    { 2345, "test1", "test1", 100.00, true},
    { 1234, "test2", "test2", 178.1, true}
};