I’m wondering if there’s a way to use a string to access a specific item in a matrix of int[X].
I have a program which uses enums as iterators to reference a large amount of data. To select an item in the matrix, the user will enter a string, which is also an enum, which also must serve as an iterator for something in the matrix. Here is a toybox example:
====================================================================
#include <iostream>
#include <string>
using namespace std;
The idea is the user executes the program by typing “./RUN First” to print out the first element in the MyNumbers array, “./RUN Second” to access the second, and so on. I can’t use static numbers for the iterator. (i.e., “./RUN 1”) I must reference by enum/string.
When run, the output of this problem is thus:
====================================================================
user@debian$ ./RUN Second
Matrix[ atoi(Second) ]: 1
user@debian$
====================================================================
are not present in the final executable. You can prove this to yourself by compiling the code as it is and running md5sum on it. Then correct the spelling of 'Forth' to 'Fourth' , compile it again and rerun md5sum. You will get the same result, showing the executables are identical.
So to solve your problem you need to do one of these options, depending upon your experience/needs:
1) Have a std::vector of std::string with the relevant values. Then loop through these to find the correct string and output the corresponding value of the Matrix array
2) Have a std::vector of std:: pair, with one value of the pair being the strings and the second value being the values of Matrix. Again, search for the string and output the relevant Matrix value
3) Create a std::map with the keys being the strings and values being the Matrix values.
C++ doesn't do that, it is strongly typed. You can't just throw variables around and hope it will work.
What Peter said.
Also, be careful with enums and mainspace scope. Your are using the "LAST_VALUE" trick, but at the same time, without scope, you are creating a global "LAST_VALUE" token, and ruining it for everyone else.
You can, for example, scope your enum in a namespace:
This way, you have to write "NyNumbers::LAST_VALUE". You can also use a struct instead of a namespace. There subtle differences. If you have access to C++11, you can also use enum classes.
That said, I'd question the entire enum approach. It seems that all you really want is to store an index, and be able to build it from text:
Code:
#include <iostream>
#include <string>
#include <unordered_map>
#include <cstdlib>
//The string 2 enum
std::unordered_map<std::string, size_t> String2NyNumbers =
{
{"First", 0},
{"Second", 1},
{"Third", 2},
{"Forth", 3}
};
int main(int argc, char* argv[])
{
int Matrix[] = { 1, 3, 7, 12 };
auto it = String2NyNumbers.find(argv[1]);
if(it == String2NyNumbers.end())
{return EXIT_FAILURE;}
size_t index = static_cast<size_t>(it->second);
std::cout << "The " << argv[1] << " index at " << index << " is " << Matrix[index] << std::endl;
return 0;
}
Is your question related to IO?
Read this C++ FAQ LITE article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
Bookmarks