I'm trying to iterate through a struct, but wanted to be able to do something along the lines of:

 string searchy = "jimmy";
 string suffix = "firstname";

 for (person dude : pnum){
                if (dude.&suffix == searchy){
                        // Do things
                }
        }

For those familiar with Perl/PHP, I'm trying to accomplish the equivalent of $$variable. Google is not my friend today. :/

Recommended Answers

All 11 Replies

The double dollar sign is used to set address a variable variable name, right? This is not possible in C++ and in this context I don't think it's what you want. You want the address a struct member called "jimmy" here? You'd probably rather want a "name" struct member and compare the value of it with "jimmy".

What I'm shooting for is to be able to take a part of a struct and use it as essentially the "search" field. In the code above, I referenced the firstname. I was trying to get the if statement to basically do a check for dude.firstname == "jimmy" or whatever the string would end up being.

struct person {
        int id;
        string firstname;
        string lastname;
        string address;
        string phone;
};

So if I'm understanding you correctly, you can't do a reference variable in that fashion? Also, I'm reading into this name struct member thing in the meantime. Would that still be the most efficient way? Or did I ask the wrong question?

Oops, in my post I meant you're trying to reference the "firstname" field instead of "jimmy". It doesn't change much for the way I interpreted it though; it's not possible to select a struct member by variable. If you really insists on something like this maybe changing the structs to map's is something to consider. (Or add a map to the struct that contains all the strings)

Well you could store all your duds :) into a container, meaning a container of type struct person. And, you could iterate over that container, checking for each particular struct, if its name/other characteristics match your search. I don't know if it's exactly what you were thinking of...

#include <vector>
#include <string>
using namespace std;
struct person{
    string fname, lname;
    int age;
};

int main(){
    vector<person> list;
    // do stuff with your persons;

    string searchByThis;
    for (vector<person>::iterator it=list.begin();it!=list.end();it++){
        if ((*it).fname == searchByThis){
            //do stuff with your person...
        }
    }
    return 0;
}

He doesn't only want the value to match against to be stored into a variable, but also the struct field that is matched against. So in your example he'd want something like this:

#include <vector>
#include <string>
using namespace std;
struct person{
    string fname, lname;
    int age;
};

int main(){
    vector<person> list;
    // do stuff with your persons;

    string searchValue = "JohnDoe",
           searchField = "fname";

    for (vector<person>::iterator it=list.begin();it!=list.end();it++){
        // Do "(*it).fname == "JohnDoe"" because searchValue = JohnDoe and searchField is fname.
        {
            //do stuff with your person...
        }
    }
    return 0;
}

You can't overload the dot operator either, otherwise you could try to make something that comes kind-of-close with some internal additions.

The map thing I mentioned:

#include <string>
#include <map>
#include <iostream>

using namespace std;

class Person
{
    public:
        Person(const int id, const string firstName, const string lastName, const string address, const string phone) : _id(id)
        {
            _data["firstName"] = firstName;
            _data["lastName" ] = lastName;
            _data["address"  ] = address;
            _data["phone"    ] = phone;
        }
        string operator() (const string field);

    private:
        int _id;
        map<string, string> _data;
};

string Person::operator() (const string field)
{
    map<string, string>::const_iterator it;

    it = _data.find(field);

    if (it != _data.end())
    {
        return it->second;
    }
    else
    {
        // Exception?
        return "";
    }
}

int main()
{
    Person example(1, "Bob", "Saget", "House 01", "1234");

    if (example("firstName") == "Bob")
    {
        cout << "Bob saget detected.\n";
    }
    else
    {
        cerr << "Bob saget not found.\n";
    }

    return 0;
}

But yeeeaahh.. Shouldn't want this :< Will also become more complex if you want it to contain random datatypes.

Thanks for the replies guys. I'm definitely learning a lot, so that's awesome. But if that last example isn't something I should want, then how would you go about it if it were your program?

You didn't supply enough information about your application for me to answer that. I'd change my design in a way where I have no need to be able to address a struct member based on a variable name. (e.g. a design in which functions will know what a struct looks like) In essence it would become somewhat like Lucaci Andrew posted. Are you trying to setup everything super generic that you want to address things based on a variable? If that's what you want I'd probably reconsider the use of a struct and design something more capable of doing what you want. (which could ofcourse contain structs)

I think the problem is that I likely don't know enough about the language to even know if what I'm doing is most efficient, it's just how I would have done it in perl/php.

All I'm trying to accomplish is the ability to search for data based on a struct for now. I assumed a struct was the appropriate way to group data logically together, if there's another/better way I'd appreciate it if you could enlighten me.

Yeah a struct is right for that. Searching data "based on a struct" is troublesome if you don't want the function to do any assumptions on what the struct looks like though. What you could do is define functions for any type of search "type". For example a "SearchByFirstName", "SearchById", and so on. For some, like first name, you'll likely want to return a vector as the search can yield multiple results but an ID is unique so it will return at most 1 result.

That actually makes me feel a significantly better then if it's better to create a search function per field, I just assumed that was the less preferred method to a $$var like function. I'll mark as solved. Thanks both to you and Lucaci for help and input.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.