固定向量集合的高效查找
Efficient lookup on fixed collection of vectors
我有固定数量 (5) 的结构向量(结构在运行时插入,数量可以变化)。
我有一个 enum class
用作查找的键。例如
enum class CountryCode {
kUS,
// ...other 4
};
const std::vector<SomeStruct>& get_by_country_code(CountryCode cc);
我可以将 std::unordered_map<CountryCode, std::vector<SomeStruct>, CustomHash>
与自定义哈希一起使用,但我认为对于固定大小的集合来说不值得,因为在运行时之前就知道排序。
存储这 5 个向量并按 enum
值进行查找的最有效方法是什么?
我也在考虑 std::tuple
,但在我的情况下,价值观不是异质的。
What would be the most efficient way to store these 5 vectors and do a lookup by the enum value?
数组。 enum 的默认值为 0,1,... 非常适合作为数组的索引。
唯一的小障碍是枚举 类 必须显式转换为基础整数。
does array of vectors considered a good practice?
没有什么特别不好的做法。
如果向量的大小同时已知并且在创建后它们不发生变化,那么有一个更有效的解决方案:使用包含的单个向量所有按国家代码划分的对象。然后对子向量使用跨度数组。
#include <iostream>
#include <vector>
#include <cstdint>
#include <array>
using namespace std;
const int k_MAX_COUNTRY_CODE = 5;
enum class CountryCode: uint8_t {
COUNTRY_CODE_0 = 0,
COUNTRY_CODE_1 = 1,
COUNTRY_CODE_2 = 2,
COUNTRY_CODE_3 = 3,
COUNTRY_CODE_4 = 4,
};
void printVector(vector<int>& f_vec){
for(auto& el : f_vec){
cout << el << " ";
}
cout << "\n";
}
int main() {
CountryCode l_countryCode;
array<vector<int>, k_MAX_COUNTRY_CODE> l_lookUP;
vector<int> l_lookUP(k_MAX_COUNTRY_CODE);
for(int i = 0; i < k_MAX_COUNTRY_CODE; i++){
vector<int> tempVec((i+1)*2 , i + 534);
l_lookUP[i] = tempVec;
}
printVector(l_lookUP[static_cast<int>(CountryCode::COUNTRY_CODE_4)]);
printVector(l_lookUP[static_cast<int>(CountryCode::COUNTRY_CODE_3)]);
printVector(l_lookUP[static_cast<int>(CountryCode::COUNTRY_CODE_1)]);
printVector(l_lookUP[static_cast<int>(CountryCode::COUNTRY_CODE_2)]);
return 0;
}
就像@eerorika 建议的那样,我也会使用一个数组来快速查找。我希望这会有所帮助。
我有固定数量 (5) 的结构向量(结构在运行时插入,数量可以变化)。
我有一个 enum class
用作查找的键。例如
enum class CountryCode {
kUS,
// ...other 4
};
const std::vector<SomeStruct>& get_by_country_code(CountryCode cc);
我可以将 std::unordered_map<CountryCode, std::vector<SomeStruct>, CustomHash>
与自定义哈希一起使用,但我认为对于固定大小的集合来说不值得,因为在运行时之前就知道排序。
存储这 5 个向量并按 enum
值进行查找的最有效方法是什么?
我也在考虑 std::tuple
,但在我的情况下,价值观不是异质的。
What would be the most efficient way to store these 5 vectors and do a lookup by the enum value?
数组。 enum 的默认值为 0,1,... 非常适合作为数组的索引。
唯一的小障碍是枚举 类 必须显式转换为基础整数。
does array of vectors considered a good practice?
没有什么特别不好的做法。
如果向量的大小同时已知并且在创建后它们不发生变化,那么有一个更有效的解决方案:使用包含的单个向量所有按国家代码划分的对象。然后对子向量使用跨度数组。
#include <iostream>
#include <vector>
#include <cstdint>
#include <array>
using namespace std;
const int k_MAX_COUNTRY_CODE = 5;
enum class CountryCode: uint8_t {
COUNTRY_CODE_0 = 0,
COUNTRY_CODE_1 = 1,
COUNTRY_CODE_2 = 2,
COUNTRY_CODE_3 = 3,
COUNTRY_CODE_4 = 4,
};
void printVector(vector<int>& f_vec){
for(auto& el : f_vec){
cout << el << " ";
}
cout << "\n";
}
int main() {
CountryCode l_countryCode;
array<vector<int>, k_MAX_COUNTRY_CODE> l_lookUP;
vector<int> l_lookUP(k_MAX_COUNTRY_CODE);
for(int i = 0; i < k_MAX_COUNTRY_CODE; i++){
vector<int> tempVec((i+1)*2 , i + 534);
l_lookUP[i] = tempVec;
}
printVector(l_lookUP[static_cast<int>(CountryCode::COUNTRY_CODE_4)]);
printVector(l_lookUP[static_cast<int>(CountryCode::COUNTRY_CODE_3)]);
printVector(l_lookUP[static_cast<int>(CountryCode::COUNTRY_CODE_1)]);
printVector(l_lookUP[static_cast<int>(CountryCode::COUNTRY_CODE_2)]);
return 0;
}
就像@eerorika 建议的那样,我也会使用一个数组来快速查找。我希望这会有所帮助。