我怎么能"名字"数组的元素?

时间:2014-12-20 01:50:46

标签: c++ arrays string

所以我的问题是:

鉴于我的数组

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

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

char *myString = MyArray[string1];

4 个答案:

答案 0 :(得分:1)

不幸的是,你不能做这样的事情;但是,您可以尝试以下操作,以便在不必返回包含数组的标题的情况下知道string1是什么:

enum ArrayNames : int
{
    string1 = 0,
    string2 = 1,
    string3 = 2,
    string4 = 3,
    string5 = 4,
    string6 = 5
};

char *myString = MyArray[ArrayNames.string1];

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

答案 1 :(得分:0)

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

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

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

答案 2 :(得分:0)

问题显然是关于如何访问元素为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"];

答案 3 :(得分:0)

您可以使用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"];

Difference between insert, emplace, and []