-1

So my question is this:

Given my array

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

How can I name string1 so that when I try to access it in the array, I use code like below?

char *myString = MyArray[string1];

4 Answers4

1

Unfortunately, you cannot do such a thing; however, you can try the following so that you know what string1 is without having to go back to the header containing your array:

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

char *myString = MyArray[ArrayNames.string1];

Hopefully this helps anyone who wants to save some time while working with arrays out :)

  • 2
    There's no need to specify the underlying type of an `enum` unless you plan to store it somewhere or you need to forward declare it (and it's C++0x only). In addition the scope operator is `::` and not `.` (which is unnecessary unless declaring an enum class, `string1` is enough. Finally `enum` entries should be capitalized. – Jack Dec 20 '14 at 01:57
0

if you want to access strings by name, just make some variables.

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

you're looking for a solution to a problem that does not exist.

Richard Hodges
  • 68,278
  • 7
  • 90
  • 142
  • gentle disapproval - this way, stringX can only be a const std::string, with no (unique) type for method/function signatures. If both apples and oranges are same type (i.e. const std::string), the author only has unique method-names to choose, and the compiler can not inform when apple passed to s method that only accepts orange varieties. – 2785528 Dec 20 '14 at 03:57
  • The question is only asking about looking up a string by name. – Richard Hodges Dec 20 '14 at 11:00
0

As the question clearly is about how to access an index of an array whose elements are const char*,
I believe the below example is an easy and straight forward approach using an enum.

#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;
}

You may want to use another container, such as std::map which lets you associate a key with your element:

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

cout << myMap["string1"];
Andreas DM
  • 10,685
  • 6
  • 35
  • 62
  • Gentle disapproval - this 'style' creates pathological dependency between order of enums and order of MyArray[] elements. Consider when many enum - string pairs. (I've seen > 500) This coordination should not be a manual effort. – 2785528 Dec 20 '14 at 03:51
0

You can accomplish this by using c++'s std::map container. A map requires two data types. The first is the key and the second is the value the key is associated with. The key is what you'll use to get the associated value with.

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 []

Community
  • 1
  • 1
SelfTaught
  • 482
  • 6
  • 16
  • It's always best to include some descriptive text such as "You can accomplish this with an associative contains like [`std::map`](http://en.cppreference.com/w/cpp/container/map)" along with some background about the specific container(s). Also consider using [`std::make_pair("string1", "string1')`](http://en.cppreference.com/w/cpp/utility/pair/make_pair) instead of `std::pair<>` and including examples for [`map_of_strings.emplace("string1", "string1')`](http://en.cppreference.com/w/cpp/container/map/emplace), and brace initialization `T map_of_strings = { {"string1", "string1"} }`. – Captain Obvlious Dec 20 '14 at 03:54