如何创建结构体数组并实现搜索表?附数据与代码片段
嘿,我来帮你搞定这个需求!先看看你当前代码里的几个小问题,然后一步步实现数据存储和搜索功能~
第一步:修正结构体定义
你当前的结构体里用了int *date、double *low这类指针,还有string currency[100]的写法其实都不太对——每个数据条目里的这些都是单个值,不是数组/动态指针;而且C++里直接用string类型就可以存储字符串,不需要固定大小的数组。
正确的结构体应该是这样的(假设你用C++实现,毕竟用到了string):
#include <string> #include <fstream> #include <iostream> #include <vector> #include <unordered_map> // 进阶索引用 using namespace std; // 方便演示,实际项目可根据情况调整 // 单个行情数据条目结构体 struct MarketData { int date; // 日期直接存整数,比如20130610 string currency; // 币种名称,如Diamond string exchange; // 交易所名称,如CoinMate double low; // 最低价 double high; // 最高价 int daily_cap; // 日成交量(根据你的数据字段推测) };
另外推荐用vector<MarketData>代替固定大小的数组,它能自动扩容,不用提前预估数据量上限。
第二步:从文件读取数据到结构体集合
你之前用了C风格的FILE*,C++里更推荐用ifstream来读取文件,更安全便捷。示例代码:
int main() { // 打开目标数据文件 ifstream inFile("test_data.txt"); if (!inFile.is_open()) { cerr << "Error: 无法打开数据文件!" << endl; return 1; } vector<MarketData> dataList; // 存储所有读取到的行情数据 MarketData tempItem; // 按照数据格式逐行读取:日期 币种 交易所 最低价 最高价 成交量 while (inFile >> tempItem.date >> tempItem.currency >> tempItem.exchange >> tempItem.low >> tempItem.high >> tempItem.daily_cap) { dataList.push_back(tempItem); // 将读取到的条目加入集合 } inFile.close(); // 可选:打印读取结果,验证是否正确 cout << "成功读取" << dataList.size() << "条数据:" << endl; for (const auto& item : dataList) { cout << item.date << " | " << item.currency << " | " << item.exchange << " | " << item.low << " | " << item.high << " | " << item.daily_cap << endl; } // 调用搜索函数(后面会实现) searchByCurrency(dataList, "OKCash"); searchByDate(dataList, 20130610); return 0; }
如果是纯C语言实现,把string换成char[64]这类固定长度的字符数组,用fscanf读取,结构体数组可以用动态分配(malloc)的方式,逻辑类似。
第三步:实现搜索功能
根据需求,我们可以实现不同维度的搜索,下面举几个常用的例子:
1. 按币种名称搜索所有相关条目
void searchByCurrency(const vector<MarketData>& dataList, const string& targetCurrency) { cout << "\n=== 搜索币种:" << targetCurrency << " ===" << endl; bool isFound = false; for (const auto& item : dataList) { if (item.currency == targetCurrency) { cout << "日期:" << item.date << " | 交易所:" << item.exchange << " | 最低价:" << item.low << " | 最高价:" << item.high << " | 成交量:" << item.daily_cap << endl; isFound = true; } } if (!isFound) { cout << "未找到该币种的相关数据!" << endl; } }
2. 按日期搜索所有相关条目
void searchByDate(const vector<MarketData>& dataList, int targetDate) { cout << "\n=== 搜索日期:" << targetDate << " ===" << endl; bool isFound = false; for (const auto& item : dataList) { if (item.date == targetDate) { cout << "币种:" << item.currency << " | 交易所:" << item.exchange << " | 最低价:" << item.low << " | 最高价:" << item.high << " | 成交量:" << item.daily_cap << endl; isFound = true; } } if (!isFound) { cout << "未找到该日期的相关数据!" << endl; } }
进阶:多条件组合搜索(比如日期+交易所)
void searchByDateAndExchange(const vector<MarketData>& dataList, int targetDate, const string& targetExchange) { cout << "\n=== 搜索日期:" << targetDate << " 且 交易所:" << targetExchange << " ===" << endl; bool isFound = false; for (const auto& item : dataList) { if (item.date == targetDate && item.exchange == targetExchange) { cout << "币种:" << item.currency << " | 最低价:" << item.low << " | 最高价:" << item.high << " | 成交量:" << item.daily_cap << endl; isFound = true; } } if (!isFound) { cout << "未找到符合条件的相关数据!" << endl; } }
额外优化建议
如果你的数据量很大,频繁搜索的话可以建立索引提升效率,比如用unordered_map按币种建立映射:
// 建立币种索引 unordered_map<string, vector<MarketData>> currencyIndex; for (const auto& item : dataList) { currencyIndex[item.currency].push_back(item); } // 搜索时直接通过索引查找,速度更快 auto it = currencyIndex.find("OKCash"); if (it != currencyIndex.end()) { cout << "\n通过索引搜索OKCash的结果:" << endl; for (const auto& item : it->second) { cout << item.date << " | " << item.exchange << endl; } }
内容的提问来源于stack exchange,提问作者Sam
相关产品推荐
相关产品推荐

