C++ 搜索向量中的对象
我试图在我的代码中找到一种方法来搜索我使用的向量中的对象。我已经将信息推回到不同的向量中,我知道显示信息的大小。我希望用户能够输入银行账号,如果正确,则显示其他向量的内容C++ 搜索向量中的对象,c++,C++,我试图在我的代码中找到一种方法来搜索我使用的向量中的对象。我已经将信息推回到不同的向量中,我知道显示信息的大小。我希望用户能够输入银行账号,如果正确,则显示其他向量的内容 #include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; //declaring four vectors std::vector<
#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;
}
一旦找到了找到的帐户的迭代器,就可以使用简单的指针/迭代器算法(如-accountNumbers.begin)从中获取基于向量0的索引
然后,给定此索引,您可以访问其他向量中的相应项
#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;
}
但是,从面向对象的设计角度来看,我更喜欢为accounts定义一个类,包含几个字段,如account number、name、address等,并且具有一个向量,而不是单个account属性的多个向量。但是,根据您的代码,我建议您重新查看代码的某些部分,您只需执行以下操作: 无效查找帐户{ 系统cls;
对于int i=1;i std::find可能会有帮助。您应该使用银行账户向量。否则,将很难处理名称、地址等各个向量之间的关系。顺便说一句,如果您按精确值搜索,您可能会对accountnumber的双精度有更大的问题。@harper那么我可以输入所有不同的信息吗从其他向量转换为一个向量?我以前曾想过,但不确定这是否可行。@您可以使用结构的向量,其中结构包含以前在单独向量中存在的所有项。我对结构类型名称的建议是account。
auto it = find(accountNumbers.begin(), accountNumbers.end(),
accountNumberToFind);
if (it != accountNumbers.end())
{
... Found, process it ...
}