3

I am trying to create a multidimensional array in c++ where there's a string and an int involved. I tried int test[1][2] = {{"a", 1}, {"b", 2}, {"c", 3}}; but g++ gave me the following:

example.cpp: In function ‘int getServer(std::string)’:
error: too many initializers for ‘int [1][2]’
error: invalid conversion from ‘const char*’ to ‘int’ [-fpermissive]

I tried to use char test[1][2] as well for the initializer, but this didn't work.

Edit: This will become a rather large multidimensional array and it's needed so that I can get values and store based on a loop control variable which will vary in its length.

2
  • Are you mapping one to another or storing pairs? Commented Aug 27, 2013 at 15:36
  • basically trying to store them in pairs to access each value doing something like test[0][0] Commented Aug 27, 2013 at 15:38

3 Answers 3

5

Use std::pair:

std::array<std::pair<std::string, int>, 3> test{{"a", 1}, {"b", 2}, {"c", 3}};

std::pair works with C++03, but the initialization and array type I used are C++11. You can still use a normal array and a bunch of std::make_pair calls.

Now you can access each inner element with an index and first or second:

test[0].first //"a"
test[2].second //3
Sign up to request clarification or add additional context in comments.

4 Comments

well, I need to be able to use the multidimensional array to get the length (by that, I mean the number of pairs), and also be able to use it in cases like while (i <= 10) { test[0][i]; i++; }
@cellsheet, You can get the number of pairs with test.size(). As for your other example, pairs only have two "elements", so i <= 10 doesn't make sense. And with only two, I can't imagine why you would specifically make it harder to use [i] than just .first, followed by .second.
there's a specific formula I have to port in which it heavily rely's on the length of the array and stores/access values depending on the loop control variable. while (length < test.size()) { variable += test[variable2][1]; variable++; } is an example of part of this formula.
@cellsheet, The only thing you need to change to make it work is the constant [1] to a constant .second.
5

This is not allowed in c++. An array can only have one type, so specifying an array with two types like that does not actually make sense.

My idea to do this would be to define a struct:

struct pair {
    std::string s;
    int i;
}

And then define a one dimensional array of type pair. Then access your elements like array[0].s

1 Comment

To store a collection of multiple types, I would use a tuple instead of an array.
2

This generally should not be done in C++. If you are associating pairs (chars and ints, for example), and want them in an array, you should use a simple struct to associate the two, like

struct MyPair {
   char letter;
   int number;
};

Then use it like this:

MyPair pairAr[2];
MyPair myPair;
myPair.letter = 'a';
myPair.number = 1;
pairAr[0] = myPair;

This is generally more a C style. You also may want to consider making a class which contains the information you want, and using STL collections, like the Vector class.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.