如何"name"数组的元素?

How can I "name" the elements of an array?

本文关键字:元素 数组 name 如何      更新时间:2023-10-16

所以我的问题是这样的:

给定我的数组

char *MyArray[] = {"string1","string2","string3","string4","string5","string6"};

如何命名 string1,以便当我尝试在数组中访问它时,我使用如下代码?

char *myString = MyArray[string1];

不幸的是,你不能做这样的事情;但是,你可以尝试以下方法,以便知道 string1 是什么,而不必返回到包含数组的标头:

enum ArrayNames : int
{
    string1 = 0,
    string2 = 1,
    string3 = 2,
    string4 = 3,
    string5 = 4,
    string6 = 5
};
char *myString = MyArray[ArrayNames.string1];

希望这可以帮助任何想要在使用数组时节省一些时间的人:)

如果你想按名称访问字符串,只需创建一些变量。

const string std::string 
string1 = "string1",
string2 = "string2",
string3 = "string3",
string4 = "string4",
string5 = "string5";

您正在寻找不存在的问题的解决方案。

由于问题显然是关于如何访问元素const char*数组的索引,
我相信下面的示例是使用枚举的简单直接的方法。

#include <iostream>
using namespace std;
int main()
{
    enum StringCodes {
        STRING1,
        STRING2,
        STRING3,
        STRING4,
        STRING5,
        STRING6
    };
    const char *MyArray[] = 
                {"string1","string2","string3","string4","string5","string6"};
    const char *string1 = MyArray[STRING1];
    cout << string1;
    return 0;
}

您可能希望使用另一个容器,例如 std::map,它允许您将键与元素相关联:

map<string,const char*> myMap;
myMap["string1"] = "string1";
myMap["string2"] = "string2";
cout << myMap["string1"];

你可以通过使用 c++ 的 std::map 容器来实现这一点。地图需要两种数据类型。第一个是键,第二个是与键关联的值。键是用于获取关联值的内容。

std::map<std::string, std::string> map_of_strings;
/**
 * Put your strings twice because the first is the key which
 * enables you to get the value using the string. The second
 * is the value. You can insert your key / values using any
 * one of the following methods:
 *
 * Method 1:
 *  - Using std::pair which is the longer less pleasant
 *    looking way of adding to a map.
 */
 map_of_strings.insert(std::pair<std::string, std::string>("string1", "string1"));
/**
 * Method 2:
 *  - Using std::make_pair so you don't have to provide
 *    the two data types. Make pair will do it for you.
 */
 map_of_strings.insert(std::make_pair("string2", "string2"));
/**
 * Method 3:
 *  - Using emplace instead of insert. Emplace provides
 *    the same end result that insert does which is adding
 *    new pairs of data. However, emplace utilizies rvalues
 *    instead of creating a copy the given key / value. For
 *    more detailed information about the difference between
 *    insert and emplace, take a look at the stack article i've
 *    provided a link to below this code.
 */
 map_of_strings.emplace(std::pair<std::string, std::string>("string3", "string3"));
/**
 * Method 4:
 *  - Basically the same as method 3
 */
 map_of_strings.emplace(std::make_pair("string4", "string4"));
/**
 * Method 5:
 *  - Using square brackets like you would with an array
 */
 map_of_strings["string5"] = "string5";
/**
 * Method 6:
 *  - Using curly braces to declare single or multiple key / value pairs.
 */
 map_of_strings = {{"string6", "string6"}, {"string7", "string7"}};
 // Get string1 from the map - Tada!
 std::string str = map_of_strings["string1"];

插入、置位和 [] 之间的区别