Access elements of 'struct' using a variable.

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2005
Posts: 43
Reputation: Nedals is an unknown quantity at this point 
Solved Threads: 0
Nedals Nedals is offline Offline
Light Poster

Access elements of 'struct' using a variable.

 
0
  #1
Jun 16th, 2006
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. struct record {
  5. string C0;
  6. string C1;
  7. string C2;
  8. };
  9. int main()
  10. {
  11. record param;
  12. param.C0 = "heading1";
  13. param.C1 = "heading2";
  14. param.C2 = "heading3";
  15. string key = "CO";
  16. string data = param.C0;
  17. cout << "DATA: " << data << endl;
  18. return 1;
  19. }

Is it possible to replace 'C0' with the variable 'key'?
ie:
string data = param.??key??; // where key is interpolated to 'C0'.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,052
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: Access elements of 'struct' using a variable.

 
2
  #2
Jun 17th, 2006
No.
All my posts may be redistributed under the GNU Free Documentation License.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Access elements of 'struct' using a variable.

 
0
  #3
Jun 17th, 2006
Like
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. struct record {
  5. string C[3];
  6. };
  7. int main()
  8. {
  9. record param;
  10. param.C[0] = "heading1";
  11. param.C[1] = "heading2";
  12. param.C[2] = "heading3";
  13. int key = 0;
  14. string data = param.C[key];
  15. cout << "DATA: " << data << endl;
  16. return 0; // zero is success
  17. }

Or maybe
  1. #include <iostream>
  2. #include <string>
  3. #include <map>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. map<string,string> param;
  9. param["C0"] = "heading1";
  10. param["C1"] = "heading2";
  11. param["C2"] = "heading3";
  12. string key = "C0";
  13. string data = param[key];
  14. cout << "DATA: " << data << endl;
  15. return 0;
  16. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 43
Reputation: Nedals is an unknown quantity at this point 
Solved Threads: 0
Nedals Nedals is offline Offline
Light Poster

Re: Access elements of 'struct' using a variable.

 
0
  #4
Jun 17th, 2006
Thanks...

Your first sample would not work for me because I canot array the actual 'key' names (bad example on my part).

Your second example, using <map>, will work. I had tried it earlier and could not get it to work in VC++. I got some 117 warnings..

This was fixed by adding...
#pragma warning(disable:4786)

see: http://www.daniweb.com/techtalkforum...%3Bmap%26gt%3B
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC