搜索struct vector并在C ++中输出元素

时间:2018-03-27 17:07:51

标签: c++ clion

好的,所以我的结构看起来像这样:

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;

所以现在我需要能够在类型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),则会打印“此帐户不存在”。以及所有帐户详细信息。那么什么是更好的搜索和打印方式?或者对我的问题有什么解决方法?谢谢你的时间。

2 个答案:

答案 0 :(得分:1)

您的问题是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}
};

答案 1 :(得分:0)

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";
}

,但相对容易向后移植到