我正面临着Map的一些问题。我正在开发一个应用程序,我正在设计一个数据库,并且我正面临着一个问题,我需要将表架构存储在主内存中。地图中的元素会自动排序(按键),我需要按照原样排序。我希望将元素作为用户输入它们的方式插入到Map中。有没有可以使用的替代数据结构?而且,在不知道这个事实的情况下,我开发了整个应用程序。只有在测试过程中我才能弄清楚这件事(我的坏处!)。所以,如果我更改为完全不同的数据结构,那么代码中有几处需要修改。请让我知道是否有一种简单的方法可以消除这个问题,或者至少可以使用类似的数据结构,以便Map上的操作与新数据结构类似。C++地图的问题
这是我写来实现这一代码:
class Attribute {
public:
string attributeName;
string type; //char, int, etc
int size; //4 for int and corresponding size for char
};
class Table {
public:
string tableName;
map<string, Attribute> attribute;
string primaryKey;
int recordSize;
int totalSize;
int records;
};
Attribute CatalogMemoryHandler::createAttribute(string attributeName, string type, int size) {
Attribute attribute;
attribute.attributeName = attributeName;
attribute.type = type;
attribute.size = size;
return attribute;
}
Table CatalogMemoryHandler::createTable(string tableName, string primaryKey, int recordsSize, int totalSize, int records) {
Table tableObj;
tableObj.tableName = tableName;
tableObj.primaryKey = primaryKey;
tableObj.recordSize = recordsSize;
tableObj.totalSize = totalSize;
tableObj.records = records;
return tableObj;
}
bool CatalogMemoryHandler::addNewTable(string tableName,
string primaryKey,
int recordSize,
int totalSize,
int records,
vector<string> listOfAttributeNames,
vector<string> listOfAttributeTypes,
vector<int> listofAttributeSizes
) {
Table newTable = createTable(tableName, primaryKey, recordSize, totalSize, records);
for(int i = 0; i < (int) listOfAttributeNames.size(); i++) {
Attribute attribute = createAttribute(listOfAttributeNames[i], listOfAttributeTypes[i], listofAttributeSizes[i]);
newTable.attribute.insert(make_pair(listOfAttributeNames[i], attribute));
}
cout << "\n";
table[tableName] = newTable;
return true;
}
请协助。谢谢。
如果元素被频繁添加/然后取出它会很慢,除去从向量的关键,特别是如果它是某个地方开始,因为你需要将所有的下列键填写孔。 – stefanB 2011-04-04 04:27:59
因此,为了不对几个地方进行修改,我需要使用一个向量?另外,如何HashMap?我相信它是无序的。我需要经常按键访问元素,所以我需要一张地图或类似的东西。感谢您的回复。 – Shankar 2011-04-04 04:28:10
@stefanB:这不是一个理想的解决方案,但它是微创,这是OP的要求之一。 – 2011-04-04 04:32:06