| | |
Access elements of 'struct' using a variable.
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2005
Posts: 43
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> using namespace std; struct record { string C0; string C1; string C2; }; int main() { record param; param.C0 = "heading1"; param.C1 = "heading2"; param.C2 = "heading3"; string key = "CO"; string data = param.C0; cout << "DATA: " << data << endl; return 1; }
Is it possible to replace 'C0' with the variable 'key'?
ie:
string data = param.??key??; // where key is interpolated to 'C0'.
Like
Or maybe
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> using namespace std; struct record { string C[3]; }; int main() { record param; param.C[0] = "heading1"; param.C[1] = "heading2"; param.C[2] = "heading3"; int key = 0; string data = param.C[key]; cout << "DATA: " << data << endl; return 0; // zero is success }
Or maybe
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> #include <map> using namespace std; int main() { map<string,string> param; param["C0"] = "heading1"; param["C1"] = "heading2"; param["C2"] = "heading3"; string key = "C0"; string data = param[key]; cout << "DATA: " << data << endl; return 0; }
•
•
Join Date: Dec 2005
Posts: 43
Reputation:
Solved Threads: 0
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
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
![]() |
Similar Threads
- Differences Between Java and C/C++ (C++)
- How to access elements of text file like in matrix (Perl)
- Win32 API Struct (Perl)
- Structure of Arrays in C++ using NEW operator (C++)
- Arrays (C++)
- accessing private data members (C++)
Other Threads in the C++ Forum
- Previous Thread: general question about c++
- Next Thread: string stream
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






