Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Iterating through struct 1

Status
Not open for further replies.

ronnyjljr

IS-IT--Management
Nov 23, 2003
249
US
How would one go about iterating through a struct so that the actual variable name can be retrieved?

For Instance, say the struct has variable name;
MyStruct.Name = "Blah"

How would I be able to retrieve "Name" or any other variable name in a for loop?

Thanks,
Ron

/**************
Me: When will the project be finished?
Them: What project?
Me: The one you have been working on for the last 3 years!
Them: Ohh, we can't finish it?
Me: Why?
Them: We don't know how to program...
 
Your question doesn't make sense.You dont iterate through a structs.

You iterate through collections. Like a collections of ints, strings, structs, classes or whatnot.

/**************
Them: When will the project be finished?
Me: When you've made up your mind about what you really want.

/Per

"It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure."
 
So it is not possible to get the variables from a struct somehow?

-R

/**************
Me: When will the project be finished?
Them: What project?
Me: The one you have been working on for the last 3 years!
Them: Ohh, we can't finish it?
Me: Why?
Them: We don't know how to program...
 
No, it's not possible that way. Structure field names have no significance once the program is compiled. Just like variable names, they get compiled into memory locations and offsets, which have no "names" as such.

If you're trying to write a function to do something like list out all the values of all the fields of a structure, maybe a structure isn't even the right data type for you to use. If it's really important to iterate through a list, why not just make an actual list instead of a structure?

Your list could be as simple as an array of values of the same data type, or more complex, like a map of variants (a variant is an automation term for a variable whose type can be changed and determined on the fly rather than just at compile-time).

There is a trade-off, though. The easier you make it to iterate through a list-like object, the harder it will be to initialize and use the object. A structure is nice because its memory layout is defined at compile-time, so initializing and accessing its members is really fast.
 
>So it is not possible to get the variables from a struct somehow?

Not like traversing the members of a struct (as mentioned).

How about you tell us what you really want to do. I'm sure there's a way to accomplish that, though perhaps not the way you initially thought...


/Per

"It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure."
 
This is what I'd like to do.

Create a for loop
iterate through some list of variables
print out the variable name and variable value into a file

And then read it back in, which might be a little harder.

Thanks,
Ron

/**************
Me: When will the project be finished?
Them: What project?
Me: The one you have been working on for the last 3 years!
Them: Ohh, we can't finish it?
Me: Why?
Them: We don't know how to program...
 
Ok, it seems you want to associate a value (of some type) with a name (a string). There is a collection type that is perfect for this : The map.

Code:
#include <map>
#include <string>

// Assuming the value to associate is an int, 
// but you can change it to any (copyable) type really
typedef std::map<std::string, int> MyMap;
...
{
  MyMap map;
  map["Foo"] = 42;
  map["Bar"] = 67;
  map["Boink"]= 12345;


  // Iterate through them all
  for (MyMap::const_iterator it=map.begin();it!=map.end();++it)
  {
    std::cout << "Name=" << (*it).first << " Value=" << (*it).second << std::endl;
  }

  // Look for a certain value
  MyMap::iterator itFind =map.find("Fnort");
  if (itFind!=map.end())
  {
     // Found it
     std::cout << "Found Name=" << (*itFind).first << " Value=" << (*itFind).second << std::endl;

     // Replace the found element's value
  	 (*itFind).second = 999;
     std::cout << "Replaced Name=" << (*itFind).first << " Value=" << (*itFind).second << std::endl;
  } 
  else
  {
    // Fnort not found
  }

  // Replace a value is as easy as assigning
  map["Foo"] = 49;
}

If you really want them together in a struct you actually have a collection of structs.
Code:
#include <list>
#include <string>

struct MyStruct
{
	std::string mName;
	int mValue;
};

// Using a list, notice the similarities with defining and traversing a std::map
// That's the cool thing about all collections in STL.
typedef std::list<MyStruct> MyList;
...
{

  MyList myList;
  MyStruct s;
  s.mName = "Foo";  s.mValue=42;  myList.push_back(s);
  s.mName = "Bar";  s.mValue=67;  myList.push_back(s);

  // Iterate through them all
  for (MyList::const_iterator it=myList.begin();it!=myList.end();++it)
  {
    std::cout << "Name=" << (*it).mName.c_str() << " Value=" << (*it).mValue << std::endl;
  }

}

/Per

&quot;It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure.&quot;
 
Superb!!!

Thanks!

/**************
Me: When will the project be finished?
Them: What project?
Me: The one you have been working on for the last 3 years!
Them: Ohh, we can't finish it?
Me: Why?
Them: We don't know how to program...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top