简体   繁体   English

如何在地图中将STL未排序键值作为对使用

[英]How to use STL unsorted key-value as pair in map

I need to use STL C++ map to store key value pairs. 我需要使用STL C ++映射来存储键值对。 I need to store more than one data information in stl map. 我需要在stl映射中存储多个数据信息。 eg 例如

Need to store DataType,Data and its behavior as(in param/outparam) all in string format. 需要以字符串格式将DataType,Data及其行为存储为(在param / outparam中)。 But map always use key value pair 但是地图总是使用键值对

so if I store it like 所以如果我像这样存储

std::map<map<"int","50",>"behavior">. 

But always it sorts the the data on basis of keys which I dont want. 但是总会根据我不想要的键对数据进行排序。 If I use like .. 如果我用像..

pair<string, pair<string,string> >;
pair<string, pair<string,string>>("int",("100","in"));

This prompts compile time error! 这提示编译时错误!

error C2664: 'std::pair<_Ty1,_Ty2>::pair(const std::pair<_Ty1,_Ty2> &)' : cannot convert parameter 1 from 'const char *' to 'const std::pair<_Ty1,_Ty2> &' 错误C2664:'std :: pair <_Ty1,_Ty2> :: pair(const std :: pair <_Ty1,_Ty2>&)':无法将参数1从'const char *'转换为'const std :: pair <_Ty1 ,_Ty2>&'

What should be the exact solution of the above problem? 上述问题的确切解决方法是什么?

Regards 问候

If you don't want ordering, don't use ordered containers like map or set. 如果您不想订购,请不要使用诸如map或set之类的有序容器。 You could achieve what you're looking for using a class and a vector. 您可以使用类和向量来实现所需的内容。 The sorted nature of std::map is what makes it efficient to lookup by key. std :: map的排序性质是使按键查找有效的原因。 If you want/need unsorted and more hash like behavior for lookups, look at Boost unordered containers . 如果您想要/需要未排序的和更多类似哈希的行为来查找,请查看Boost无序容器 Note that this doesn't guarantee the order in is going to be the order out. 请注意,这并不保证订购的将是订购的。 I'm assuming you want to preserve the order of the types your putting in the container, the example below will do that. 我假设您想保留放入容器中的类型的顺序,下面的示例将做到这一点。

#include <iostream>
#include <vector>

using namespace std;

class DataBehavior
{
public:
    DataBehavior(const string &type, 
                 const string &data,
                 const string &behavior)
        :type(type), data(data), behavior(behavior)
    {
    }

    const string &getType() const     { return type; }
    const string &getData() const     { return data; }
    const string &getBehavior() const { return behavior; }

private:
    string type;
    string data;
    string behavior;
};

typedef vector<DataBehavior> Ctr;

int main (int argc, char *argv[])
{
    Ctr ctr;
    ctr.push_back(DataBehavior("int",    "50", "behavior"));
    ctr.push_back(DataBehavior("int",    "80", "behavior"));
    ctr.push_back(DataBehavior("string", "25", "behavior2"));

    for (Ctr::const_iterator it = ctr.begin(); it != ctr.end(); ++it)
    {
        cout << "Type: " << it->getType() << "  " 
             << "Data: " << it->getData() << "  "
             << "Behavior: " << it->getBehavior() << "\n";
    }
    return 1;
}

You're declaring the variables wrong in the 您在变量中声明变量错误

pair<string, pair<string,string> >;
pair<string, pair<string,string>>("int",("100","in"));

You'd need to do 你需要做

pair<string, pair<string,string>>("int",pair<string,string>("100","in"));

which is fairly verbose. 这很冗长。 You could use the make_pair function to significantly shorten that: 您可以使用make_pair函数大大缩短该时间:

make_pair( "int", make_pair( "100", "in" ) );

Also, as the others have said, if you don't want sorted order, use unordered_map from TR1 or Boost. 另外,正如其他人所说,如果您不想排序,请使用TR1或Boost中的unordered_map If you're on MS you can use hash_map instead. 如果您使用的是MS,则可以改用hash_map

Need to store DataType,Data and its behavior as(in param/outparam) all in string format. 需要以字符串格式将DataType,Data及其行为存储为(在param / outparam中)。 But map always use key value pair 但是地图总是使用键值对

To me it seems you need to use different data structure, eg 在我看来,您需要使用不同的数据结构,例如

typedef std::tuple<std::string,               // DataType
                   std::string,               // Data
                   std::string> element_type; // behaviour

// use can replace tuple with sth like: 
//struct element_type {
//    std::string m_DataType;
//    std::string m_Data;
//    std::string m_Behaviour;
//    ... ctors, operators ...
//};
// or even with std::array<std::string, 3>

std::set<element_type> elements_set;

// or

typedef int32_t element_id;

std::map<element_id, element_type> elements_map;

And you've got rather a design problem than a programming one. 而且您遇到的是设计问题,而不是编程问题。

Why do you use a map in the first place? 为什么首先使用地图? What do you want to use as lookup key -- the datatype? 您想使用什么作为查找键-数据类型? The value? 价值? The "param/outparam" characteristic? “ param / outparam”特征是什么?

Whatever you want to use as a key should go first, and the rest could be a struct / pointer / string (* urg *) / ... . 无论您要用作键的什么都应该放在首位,其余的可以是struct / pointer / string(* urg *)/...。

So I suggest you give some more thought to your needs and design a datastructure accordingly. 因此,我建议您多考虑一下自己的需求,并据此设计数据结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM