1 Replies - 238 Views - Last Post: 29 July 2015 - 11:37 AM

#1 EchoGecko  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 22
  • Joined: 19-July 15

Can you use string literal as identifier a new variable?

Posted 29 July 2015 - 11:31 AM

My program reads MIDI files and tells you how many tracks are in the MIDI file. I'm trying to write the musical contents of different tracks to their own corresponding vector<int>'s. Because the number of tracks in a MIDI file can vary, I need a way to create new vector<int> objects for however many tracks there are.

So, I imagined maybe I'd have a function that does this for me. It could use the contents of a string to name the vector (e.g "Track_0"), and basically, the function would create a new vector<int> called "Track_0". I can ++ a counter every time the function is called, and the counter will increment the contents of the string object to change the name of the next vector to be created when the function is called again for subsequent tracks. It would then have to return the name of the new vector which would be somehow used to update which vector was being used by main(). I just have no idea how, and I feel I'm getting tunnel vision.

I suppose I'm looking for a way to have string literals read by the compiler as C++ code?

Is This A Good Question/Topic? 0
  • +

Replies To: Can you use string literal as identifier a new variable?

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 6298
  • View blog
  • Posts: 13,462
  • Joined: 16-October 07

Re: Can you use string literal as identifier a new variable?

Posted 29 July 2015 - 11:37 AM

No. But take a serious look at std::map.

Rather than worrying about variable names, you want to worry about how you need to reference data. If your reference is a string key "Track_0", throw it in a map and use that key each time it's needed.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1