Indirect string identification

Jan 25, 2016 at 6:30am
I want to use indirect string id in C++.
As an example, I have a number of AnsiStrings, say :
A1="knife"; B2="Poison"; C5="Rope"; etc
and a string with the name of one of these, say ss="B2";
What I need is a function ff so that
cout<<ff(ss)
will print "Poison"

The value of ss can be anything, so a solution such as
if(ss=="A1")return A1; if(ss=="B2")return B2;...
is not possible.

Any idea how to do this ?
Jan 25, 2016 at 6:40am
Jan 25, 2016 at 7:03am
Hi and thanks for the quick reply.

I just had a (new) look at the definition of map containers, but I still do not see
how to use this for my problem.

Could you please provide a model for my ff function, say using my examples ?
Jan 25, 2016 at 7:59am
Hi,

The first item in a map is the key or lookup value - "B2" in your case. The second part is the value to be stored - "Poison"

Have a look in the example here:

http://www.cplusplus.com/reference/map/map/find/
Jan 25, 2016 at 8:20am
Thanks again,
This can indeed work if I know before all strings in the program and their content, but this is not the case.
The content of some of the strings is dynamic, i.e. B2 could be "Poison" at some step in the program, and "Strychnine" later, etc.

The example I gave was a simplification, what I want really to do is to write a converter for CocoBasic music strings (see :
http://www.colorcomputerarchive.com/coco/Documents/Manuals/Hardware/Color%20Computer%203%20Exended%20Basic%20%28Tandy%29.pdf.
pages 95 etsq.)
The syntax of this includes notes "A-G", octaves, etc, but it also includes "Xab$" codes, meaning play the music (sub)string ab$; And the ab$ string can be defined and/or modified later !
Jan 25, 2016 at 8:26am
The content of some of the strings is dynamic, i.e. B2 could be "Poison" at some step in the program, and "Strychnine" later, etc.


That's ok, nothing says you can't modify the value later, just like any other variable.
Topic archived. No new replies allowed.