【发布时间】:2011-07-28 21:59:59
【问题描述】:
我在使用地图时遇到了一些问题。我正在开发一个应用程序,我正在设计一个数据库,我面临一个问题,我需要将表模式存储在主内存中。地图中的元素会自动排序(根据键),我需要按原样排序。我希望元素以用户输入它们的方式插入到地图中。有没有我可以使用的替代数据结构?另外,在不知道这个事实的情况下,我开发了整个应用程序。只有在测试期间我才能弄清楚这件事(我的错!)。所以,如果我改成完全不同的数据结构,那么代码中有几个地方需要修改。请让我知道是否有一种简单的方法可以消除此问题,或者至少我可以使用类似的数据结构,以便 Map 上的操作类似于新数据结构的操作。
这是我为实现此目的而编写的代码:
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;
}
请帮忙。谢谢。
【问题讨论】:
标签: c++ algorithm sorting data-structures map