C ++ Map的问题

时间:2011-04-04 04:14:15

标签: c++ algorithm sorting data-structures map

我遇到了Map的一些问题。我正在开发一个应用程序,我正在设计一个数据库,我面临一个问题,我需要在主内存中存储表模式。地图中的元素会自动排序(按照键),我需要按顺序排序。我希望元素按照用户输入的方式插入Map中。是否有可以使用的替代数据结构?而且,在不知道这个事实的情况下,我开发了整个应用程序。只有在测试期间,我才能弄清楚这件事(我的坏!)。因此,如果我改为完全不同的数据结构,那么代码中有几个地方需要修改。如果有一种简单的方法可以消除这个问题,请告诉我,或者至少我可以使用类似的数据结构,以便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;
}

请协助。谢谢。

2 个答案:

答案 0 :(得分:3)

这取决于您打算如何使用数据。如果您偶尔只需按密钥访问数据,但通常只需要按照插入顺序访问数据,您只需将数据存储在std::vector

std::vector<std::pair<Key, Value> > data;

只要您始终使用push_back添加元素并且从不对元素重新排序(例如使用std::sort),元素将按插入顺序保留。您可以使用std::find对序列执行线性搜索,以查找具有给定键的元素。

如果您需要经常按键访问元素但仍需要维护插入顺序,则可以使用std::mapstd::vector来包含数据:

std::map<Key, Value> data;
std::vector<Key> key_insertion_order;

每当您将元素插入data时,请将密钥添加到key_insertion_order序列的末尾。每当您从data中删除元素时,请从序列中删除该键。

答案 1 :(得分:1)

也许你可以欺骗地图,你总是添加最大的元素,因此地图将它们放在最后。但请注意,这不是一个安全的代码。如果您从单个点和单个线程进行插入,请使用它。这是示例代码

#include "stdafx.h"
#include <map>
#include <string>

class unordered
    : public std::binary_function<std::string,std::string,bool>
{
public:
    bool operator()(const std::string& lhs,const std::string& rhs)
    {
        if (lhs == str(NULL))
            return false;
        else
            return true;
    }

public:
    static const char* str(char* str_p)
    {
        static char* val_p = NULL;
        if (str_p != NULL)
            val_p = str_p;
        return val_p;           
    }

};

int _tmain(int argc, _TCHAR* argv[])
{
    std::map<std::string,int,unordered> s_map;

    unordered::str("d");
    s_map.insert(std::make_pair("d",4));
    unordered::str("a");
    s_map.insert(std::make_pair("a",1));
    unordered::str("b");
    s_map.insert(std::make_pair("b",2));

    return 0;
}