如何在结构中的数组中搜索
How to search within an array thats within struct?
我在理解内存分配方面遇到了麻烦。
例如,如果我有一个结构,如下所示:
struct AccountInfo{
int number;
int balance;
int past[9];
int minimum_past;
int maximum_past;
};
我将如何访问阵列past[9]
?一个更直接的问题是我如何找到past
的最小值和最大值,然后将这些值分配给minimum_past
和maximum_past
?
我知道要将结构中的成员设置为某些值,我可以做类似AccountInfo -> number = 10;
的事情,但是对于数组,我仍然感到困惑。
我给你做了一个简单的例子。
在头文件中定义你的结构:
struct AccountInfo{
int number;
int balance;
int past[9];
int minimum_past;
int maximum_past;
};
在您的 CPP 文件中:
AccountInfo st_AccInfo;
访问您的结构:
int x = st_AccInfo.number;
for(int i=0; i<sizeof(st_AccInfo.past); i++)
{
// Navigate your Array from index 0 to 8
}
好吧,当您将past
声明为 9 个整数的数组时,就像从 0 开始计数索引一样C++,您可以在不调用未定义行为的情况下使用的更大索引是 8。
话虽如此,您使用的数组元素与其他结构元素完全相同:
AccountInfo accountInfo; // create a struct
AccountInfo* paccountInfo = &accountInfo; // define a pointer to it
accountInfo.last[8] = 12; // direct access
cout << paccountInfo->last[8] << endl; // access through pointer - outputs 12
相关文章:
- cpp二进制搜索问题,计算给定数组中输入元素的出现次数
- 如何在动态数组上使用搜索函数
- 在 C++ 中搜索动态分配的数组中的出现次数
- 当您在此单词中搜索单词时调整字符数组的大小?
- 在对象数组中搜索字符串并返回相应值的函数
- C++线性搜索算法,确定数组中元素的数量
- 递归二进制搜索与字符串数组
- 运行搜索数组
- 比较/搜索数组中多个整数的最佳解决方案
- C 搜索数组以获取一个数字,但如果找不到该数组,则不会触发其他语句
- 更高效的是,每次都读取文件或搜索数组中的数据
- 二叉搜索数组溢出 C++
- 在c++中搜索数组中的素数
- 性能问题C++ - 搜索数组
- C++搜索数组中最大的数字
- 我需要帮助创建一个可以搜索数组的函数
- 我想要一个递归函数来检查使用二进制搜索数组的顺序
- c#到c++的转换:搜索数组
- 搜索数组以查找单词的单独实例
- 搜索数组并比较它们