C++ 保持K字符串映射为字符串的最佳方法?

C++ 保持K字符串映射为字符串的最佳方法?,c++,dictionary,C++,Dictionary,我必须创建N个元素的映射,即N个元素映射到一个字符串。N的值不是在编译时定义的,而是在运行时定义的。是否有一些标准的方法来做到这一点?目前,我正在尝试存储一个带有字符串的类X的映射,其中X具有字符串数组,但我觉得这似乎不对 您可以使用std::map 请看一个例子: #include <map> #include <set> #include <string> #include <iostream> int main() { typede

我必须创建N个元素的映射,即N个元素映射到一个字符串。N的值不是在编译时定义的,而是在运行时定义的。是否有一些标准的方法来做到这一点?目前,我正在尝试存储一个带有字符串的类X的映射,其中X具有字符串数组,但我觉得这似乎不对

您可以使用
std::map

请看一个例子:

#include <map>
#include <set>
#include <string>
#include <iostream>

int main()
{
    typedef std::set<std::string> key_t;
    std::map<key_t, std::string> m;
    m[{"a", "b", "c"}] = "Hello,";
    m[{"d", "e"}] = "world!";
    m[{"f"}] = "\n";
    std::cout << m[{"a", "b", "c"}] << m[{"d", "e"}] << m[{"f"}];
}
#包括
#包括
#包括
#包括
int main()
{
typedef std::设置键;
std::map m;
m[{“a”,“b”,“c”}]=“你好,”;
m[{“d”,“e”}]=“世界!”;
m[{“f”}]=“\n”;

std::cout您可以使用
std::map

请看一个例子:

#include <map>
#include <set>
#include <string>
#include <iostream>

int main()
{
    typedef std::set<std::string> key_t;
    std::map<key_t, std::string> m;
    m[{"a", "b", "c"}] = "Hello,";
    m[{"d", "e"}] = "world!";
    m[{"f"}] = "\n";
    std::cout << m[{"a", "b", "c"}] << m[{"d", "e"}] << m[{"f"}];
}
#包括
#包括
#包括
#包括
int main()
{
typedef std::设置键;
std::map m;
m[{“a”,“b”,“c”}]=“你好,”;
m[{“d”,“e”}]=“世界!”;
m[{“f”}]=“\n”;

std::cout您可以使用
std::map

请看一个例子:

#include <map>
#include <set>
#include <string>
#include <iostream>

int main()
{
    typedef std::set<std::string> key_t;
    std::map<key_t, std::string> m;
    m[{"a", "b", "c"}] = "Hello,";
    m[{"d", "e"}] = "world!";
    m[{"f"}] = "\n";
    std::cout << m[{"a", "b", "c"}] << m[{"d", "e"}] << m[{"f"}];
}
#包括
#包括
#包括
#包括
int main()
{
typedef std::设置键;
std::map m;
m[{“a”,“b”,“c”}]=“你好,”;
m[{“d”,“e”}]=“世界!”;
m[{“f”}]=“\n”;

std::cout您可以使用
std::map

请看一个例子:

#include <map>
#include <set>
#include <string>
#include <iostream>

int main()
{
    typedef std::set<std::string> key_t;
    std::map<key_t, std::string> m;
    m[{"a", "b", "c"}] = "Hello,";
    m[{"d", "e"}] = "world!";
    m[{"f"}] = "\n";
    std::cout << m[{"a", "b", "c"}] << m[{"d", "e"}] << m[{"f"}];
}
#包括
#包括
#包括
#包括
int main()
{
typedef std::设置键;
std::map m;
m[{“a”,“b”,“c”}]=“你好,”;
m[{“d”,“e”}]=“世界!”;
m[{“f”}]=“\n”;

std::cout编辑:标题中写着“K字符串到字符串”,但在正文中,OP只提到元素。因此我发布了一个针对各种非特定元素的通用方法


以下是您似乎想要实现的目标的草图:

struct some_key
{
    int a = 0;
    double b = 0;
    std::string c;
    std::vector<int> d;
    // ... and so on

    bool operator<(some_key const& rhs) const
    {
        return std::tie(a,b,c,d)<std::tie(rhs.a,rhs.b,rhs.c,rhs.d);
    }
};

int main()
{
    some_key key1;
    some_key key2;
    key2.a=1;

    std::map<some_key, std::string> m;
    m[key1] = "hi";
    m[key2] = "goodbye";

    std::cout<<m[key1]<<std::endl;   //prints "hi"
}
struct some\u key
{
int a=0;
双b=0;
std::字符串c;
std::载体d;
//……等等

bool操作符编辑:标题中写着“K字符串到字符串”,但在正文中,OP只提到元素。因此我发布了一个针对各种非特定元素的通用方法


以下是您似乎想要实现的目标的草图:

struct some_key
{
    int a = 0;
    double b = 0;
    std::string c;
    std::vector<int> d;
    // ... and so on

    bool operator<(some_key const& rhs) const
    {
        return std::tie(a,b,c,d)<std::tie(rhs.a,rhs.b,rhs.c,rhs.d);
    }
};

int main()
{
    some_key key1;
    some_key key2;
    key2.a=1;

    std::map<some_key, std::string> m;
    m[key1] = "hi";
    m[key2] = "goodbye";

    std::cout<<m[key1]<<std::endl;   //prints "hi"
}
struct some\u key
{
int a=0;
双b=0;
std::字符串c;
std::载体d;
//……等等

bool操作符编辑:标题中写着“K字符串到字符串”,但在正文中,OP只提到元素。因此我发布了一个针对各种非特定元素的通用方法


以下是您似乎想要实现的目标的草图:

struct some_key
{
    int a = 0;
    double b = 0;
    std::string c;
    std::vector<int> d;
    // ... and so on

    bool operator<(some_key const& rhs) const
    {
        return std::tie(a,b,c,d)<std::tie(rhs.a,rhs.b,rhs.c,rhs.d);
    }
};

int main()
{
    some_key key1;
    some_key key2;
    key2.a=1;

    std::map<some_key, std::string> m;
    m[key1] = "hi";
    m[key2] = "goodbye";

    std::cout<<m[key1]<<std::endl;   //prints "hi"
}
struct some\u key
{
int a=0;
双b=0;
std::字符串c;
std::载体d;
//……等等

bool操作符编辑:标题中写着“K字符串到字符串”,但在正文中,OP只提到元素。因此我发布了一个针对各种非特定元素的通用方法


以下是您似乎想要实现的目标的草图:

struct some_key
{
    int a = 0;
    double b = 0;
    std::string c;
    std::vector<int> d;
    // ... and so on

    bool operator<(some_key const& rhs) const
    {
        return std::tie(a,b,c,d)<std::tie(rhs.a,rhs.b,rhs.c,rhs.d);
    }
};

int main()
{
    some_key key1;
    some_key key2;
    key2.a=1;

    std::map<some_key, std::string> m;
    m[key1] = "hi";
    m[key2] = "goodbye";

    std::cout<<m[key1]<<std::endl;   //prints "hi"
}
struct some\u key
{
int a=0;
双b=0;
std::字符串c;
std::载体d;
//……等等

bool operator您想要一个
std::map
,其中sometype是映射到字符串的元素的类型。是的,svinja这就是我当前使用类X所做的。这对我来说似乎很笨拙。我很困惑任何其他选项,
N
是否指映射到特定字符串或字符串类型的元素的数量映射到字符串的元素?N指的是字符串的数量。在这种情况下,仅使用
std::map
并插入具有相同值的多个键是否有问题,因为只有键必须是唯一的,而不是值。或者,多个键必须引用值对象的同一实例,而不仅仅是eq单价的?你想要一个
std::map
,其中sometype是映射到字符串的元素的类型。是的,svinja这就是我目前使用类X所做的。这对我来说似乎很笨拙。我很困惑,还有其他选项吗
N
是指映射到特定字符串或字符串类型的元素的数量吗映射到字符串的元素?N指的是字符串的数量。在这种情况下,仅使用
std::map
并插入具有相同值的多个键是否有问题,因为只有键必须是唯一的,而不是值。或者,多个键必须引用值对象的同一实例,而不仅仅是eq单价的?你想要一个
std::map
,其中sometype是映射到字符串的元素的类型。是的,svinja这就是我目前使用类X所做的。这对我来说似乎很笨拙。我很困惑,还有其他选项吗
N
是指映射到特定字符串或字符串类型的元素的数量吗映射到字符串的元素?N指的是字符串的数量。在这种情况下,仅使用
std::map
并插入具有相同值的多个键是否有问题,因为只有键必须是唯一的,而不是值。或者,多个键必须引用值对象的同一实例,而不仅仅是eq单价的?你想要一个
std::map
,其中sometype是映射到字符串的元素的类型。是的,svinja这就是我目前使用类X所做的。这对我来说似乎很笨拙。我很困惑,还有其他选项吗
N
是指映射到特定字符串或字符串类型的元素的数量吗映射到字符串的元素?N指的是字符串的数量。在这种情况下,仅使用
std::map
并插入具有相同值的多个键是否有问题,因为只有键必须是唯一的,而不是值。或者,多个键必须引用值对象的同一实例,而不仅仅是eq单价的?