gpt4 book ai didi

c++ - 如何在 vector 元素中找到结构数据?

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:20:16 25 4
gpt4 key购买 nike

我正在从事客户端/服务器项目。我正在努力解决如何在数据库中找到 vector 的信息以匹配请求的客户帐号。

头文件

struct storeData
{
int iAccountNumber;
int iPin;
double dBalance;
string sFirstName;
string sLastName;
string sMiddleInitial;
};
vector <storeData> storeDataArray;
storeData dataObj;

在服务器文件中..

int MyThread::findAccountNumberInStore(int iAccountNumber)
{
int iIndex = -1;

for(int unsigned i = 0; i <= storeDataArray.size(); i++)
{
//i got error message in if statement. i dont know how to fix it.
if(iAccountNumber == storeDataArray.at(dataObj.iAccountNumber))
{
return i;
}

}
return iIndex; //no account is found...
}

另外,如何将结构的数据存储在 vector 中(所有数据都在一个元素中)?

最佳答案

来自 vector.at 的描述:

Returns a reference to the element at position n in the vector.

The function automatically checks whether n is within the bounds of valid elements in the vector, throwing an out_of_range exception if it is not (i.e., if n is greater or equal than its size). This is in contrast with member operator[], that does not check against bounds.

您可以在此处阅读有关此功能的更多信息: vector.at

你也可以在这里阅读关于它的讨论: vector::at vs. vector::operator[]

对于你的实际问题,我建议这样使用它:

if(iAccountNumber == storeDataArray.at(i).iAccountNumber)

关于c++ - 如何在 vector 元素中找到结构数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27349324/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com