简体   繁体   English

搜索向量中的对象

[英]Search for objects within a vector

I am trying to find a way, within my code to search for an object that is in the Vectors that I have used. 我试图在我的代码中找到一种方法来搜索我使用的Vector中的对象。 I am already pushing back the information into the different vectors and I know about .size to display the info. 我已经将信息推回​​到不同的向量中,并且我知道.size来显示信息。 I want the user to be able to enter the bank account number then if that is correct display the contents of the other vectors. 我希望用户能够输入银行帐号,如果正确,则显示其他向量的内容。

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

using namespace std;

//declaring four vectors
std::vector<string>names;
std::vector<string>address;
std::vector<int>age;
std::vector<double>accountnumber;
//forward declaration of functions
void namesInput ();
void addressInput ();
void ageInput ();
void accountnumberInput ();


int main()
{
    //variable for switch statement
    int choice;
    system("title HSBC Online Banking");
    system("color B6");
    cout << "Please select from the following options" << endl;
    cout << "----------------------------------------" << endl;
    cout << "1. Enter profile " << endl;
    cout << "2. Search for client " << endl;
    cout << "3. Exit" << endl;
    cin >> choice;
    switch(choice)
    {
    case 1:
        //calling functions for first switch case
        namesInput ();
        addressInput ();
        ageInput ();
        accountnumberInput ();
        system("cls");
        break;
    case 2:
        break;
    case 3:
        system("exit");
        break;

    }


    return 0;
}

void namesInput ()
{
    system("cls");
    for (int i = 1; i <= 3; i++)
    {
       string temp;//variable to give to vector
       cout<<"Enter " << i << " first, middle and last names : ";
       cin>>temp;
       names.push_back(temp);//push back into vector
    }

}

void addressInput ()
{
    system ("cls");
    for (int i = 1; i <= 3; i++)
    {
       string temp;//variable to give to vector
       cout<<"Enter " << i << " House Number, Street, Postcode : ";
       cin>>temp;
       address.push_back(temp);//push back into vector
    }


}

void ageInput ()
{
    system ("cls");
    for (int i = 1; i <= 3; i++)
    {
       int temp;//variable to give to vector
       cout<<"Enter " << i << " Day, Month, Year : ";
       cin>>temp;
       age.push_back(temp);//push back into vector
    }


}

void accountnumberInput ()
{
    system ("cls");
    for (int i = 1; i <= 1; i++)
    {
       int temp;//variable to give to vector
       cout<<"Enter " << i << " Account Number ";
       cin>>temp;
       accountnumber.push_back(temp);//push back into vector
    }
    main ();
}

void findClient ()
{
    cout << "To find a client please enter their account number" << endl;



}
 std::vector<double>accountnumber; 

Using a double for account numbers does not seem a good choice to me: you may want to use a std::string instead. 在我看来,对帐号使用精度不是一个好选择:您可能想改用std::string

For example: note that "exact" comparison between doubles is not possible (so searching for account numbers using doubles as keys is problematic); 例如:请注意,不可能在双精度词之间进行“精确”比较(因此使用双精度数作为关键字来搜索帐号是有问题的); instead you can do that with strings. 相反,您可以使用字符串来实现。

So, assuming you use a vector<string> for account numbers, you can use std::find() to find a given account position in the vector, eg: 因此,假设您使用vector<string>作为帐号,则可以使用std::find()在矢量中找到给定的账户头寸,例如:

auto it = find(accountNumbers.begin(), accountNumbers.end(),
               accountNumberToFind);

if (it != accountNumbers.end())
{ 
    ... Found, process it ...
}

Once you have the iterator to the found account, you can get the vector 0-based index from it using a simple pointer/iterator arithmetic like (it - accountNumbers.begin()) . 一旦有了找到的帐户的迭代器,就可以使用简单的指针/迭代器算法(it - accountNumbers.begin())(it - accountNumbers.begin())从中获得基于矢量0的索引。

Then, given this index, you can access the corresponding items in the other vectors. 然后,在给定该索引的情况下,您可以访问其他向量中的相应项目。

However, from an object-oriented design perspective, I'd prefer defining a class for accounts, with several fields like account number, name, address, etc., and have a single vector<Account> (instead of several vectors of single account attributes). 但是,从面向对象的设计角度来看,我更喜欢为帐户定义一个类,该类具有多个字段,例如帐户编号,名称,地址等,并且具有单个vector<Account> (而不是单个帐户的多个vector)属性)。

There are some parts of your code that I'd suggest to revisit, however, according to you code, you can just do the following: 我建议您重新访问代码中的某些部分,但是根据您的代码,您可以执行以下操作:

     void lookForAccount(){

     system ("cls");
     for (int i = 1; i <= 1; i++)
     {
        int temp;//variable to give to vector
        cout<<"Enter " << i << " Account Number you want to look for";
        cin>>temp;
        for(int j = 0;j<accountnumber.size();j++){
            if(accountnumber[j]==temp){
               cout<<"account number: "<<accountnumber[j]<<", name: "<<names[j]<<"etc..."<<endl;
            }
        }
     }
     main ();
     }

Instead of this dummy method I'd anyway suggest to use iterators and std::find. 我仍然建议使用迭代器和std :: find代替该虚拟方法。

However, as pointed out in comments, this code is bug prone due to several reasons: handling account number by double is not the best choiche. 但是,正如注释中指出的那样,由于以下几个原因,此代码容易出错:最好不要选择双倍处理帐号。 Furthermore, I'd suggest to redesign your code to handle all the account information using a C++ objects. 此外,我建议您重新设计代码,以使用C ++对象处理所有帐户信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM