If I have a vector of structs

struct widgets
{
string widgetName;
int age;
int color;
};

and widgetName had multiple words in lets say

widget one type
45
blue
widget two
34
yellow
widget eight
46
green
widget ten type
33
pink

and I wanted to search the vector where this information is stored.

vector<widgets> widgetTypes;

Now what I want to do is search through the vector and only find the widgets with the word "type" in it then I want to compare the widgets that have "type" in it and see which one has the highest age. Then I want to output that widget i.e. the widget with the highest age is "widget one type" with an age of "45"?

I know I should use a find and probably npos but kind of unsure if I am right and how to set up the code.

Recommended Answers

All 16 Replies

If I have a vector of structs

struct widgets
{
string widgetName;
int age;
int color;
};

and widgetName had multiple words in lets say

widget one type
45
blue
widget two
34
yellow
widget eight
46
green
widget ten type
33
pink

and I wanted to search the vector where this information is stored.

vector<widgets> widgetTypes;

Now what I want to do is search through the vector and only find the widgets with the word "type" in it then I want to compare the widgets that have "type" in it and see which one has the highest age. Then I want to output that widget i.e. the widget with the highest age is "widget one type" with an age of "45"?

I know I should use a find and probably npos but kind of unsure if I am right and how to set up the code.

Sounds good.. use the find method.
if (str.find("type", 0) != string::npos) //...then you found a match.

Now once you put that into your loop, there's the simple matter of recording the highest value as you iterate. Make two variables to track as you go through the vector:

int highest_age;
int highest_age_location;

then if vec.age > highest_age, highest_age = vec.age, highest_age_location = i.

Still a little confused. Just to clarify, if I had a vector called

vector<widgets> widgetTypes;

then based on your code you listed I would put

if (wines.find("widgeType", 0) != string::npos) 

int highest_age;
int highest_age_location;

if (vec[i].age > highest_age); 
highest_age = vec[i].age; 
highest_age_location = i;

Is that correct because if it is than there is something wrong as I get an error when compiling.

Still a little confused. Just to clarify, if I had a vector called

vector<widgets> widgetTypes;

then based on your code you listed I would put

if (wines.find("widgeType", 0) != string::npos) 

int highest_age;
int highest_age_location;

if (vec[i].age > highest_age); 
highest_age = vec[i].age; 
highest_age_location = i;

Is that correct because if it is than there is something wrong as I get an error when compiling.

Well the error is coming from this line:
if (wines.find("widgeType", 0) != string::npos)

What is wines? Find only works on strings. So you need to get it out of your vector of structs somehow.

for (int i = 0; i < widgetTypes.size(); ++i){
  if (widgetTypes[i].find("type", 0) != string::npos){
    if (widgetTypes[i].age > highest_age){
      highest_age = widgetTpes[i].age;
      highest_age_loc = i;
    }
  }
}

I left an error in that code for you to find. Remember that find only works on strings.

I have the following code now

for (int i = 0; i < widgetTypes.size(); ++i){
		if (widgetTypes[i].find("type", 0) != string::npos){    //error mentioned below is here
			if (widgetTypes[i].age > highest_age){
				highest_age = widgetTypes[i].age;
				highest_age_location = i;
			}
		}
	}

and I get the following error

struct widgets has no member named find

Yeah try to determine why that is, then make the appropriate correction.

greyWolf, working on trying to figure out why that is that is why I asked.

So what should the find() method be operating on? So far, GreyWolf has written the only code here, other than your definition of your struct. Where's -your- code?

greyWolf, this is the code that I came up with that seems to work. I see what you meant be setting it to a string now. If there is nothing that you see that needs to be changed to improve this code I will mark this thread as solved. Thanks for the help.

string widgetFinder;
	
	for (int i = 0; i < widgetTypes.size(); ++i){
                widgetFinder = widgetTypes[i].age;
		if (widgetFinder.find("type", 0) != string::npos){    //error mentioned below is here
			if (widgetTypes[i].age > highest_age){
				highest_age = widgetTypes[i].age;
				highest_age_location = i;
			}
		}
	}

I should have removed the comments saying "error mentioned here" as there is no longer an error

greyWolf, this is the code that I came up with that seems to work. I see what you meant be setting it to a string now. If there is nothing that you see that needs to be changed to improve this code I will mark this thread as solved. Thanks for the help.

string widgetFinder;
	
	for (int i = 0; i < widgetTypes.size(); ++i){
                widgetFinder = widgetTypes[i].age;
		if (widgetFinder.find("type", 0) != string::npos){    //error mentioned below is here
			if (widgetTypes[i].age > highest_age){
				highest_age = widgetTypes[i].age;
				highest_age_location = i;
			}
		}
	}

Looks good, but... you intend to search the age variable for the word "type"? Are you sure you don't want to search the widgetName variable?

Since I need the widgetName and age would I use a double if statement?

You already have a "double if statement":

...
    if (widgetFinder.find("type", 0) != string::npos){
        if (widgetTypes[i].age > highest_age){
            ...

You just need to make sure you assign the correct member of widgetTypes (widgetName, not age) to your widgetFinder string.

The problem I am having is the current code

string widgetFinder;
 
	for (int i = 0; i < widgetTypes.size(); ++i){
                widgetFinder = widgetTypes[i].age;
		if (widgetFinder.find("type", 0) != string::npos){    //error mentioned below is here
			if (widgetTypes[i].age > highest_age){
				highest_age = widgetTypes[i].age;
				highest_age_location = i;
			}
		}
	}

is working correctly in identifying only widgets with the word "type" in the widgetName element. But because there are a total of 3 elements tied to the widgetName which are widgetName, age, and color. What I need the loop to do is

1. find in widgetName a name with the word "type" in it
2. store that widgets info for comparison and output
3. compare the first widgetName with "type" in it's age to the next widgetName with "type" in it
4. whichever is greater save that information and continue to the entire struct has been checked.

I am so close and yet so far. I am guessing it is a simple comparison code that needs to be inserted but I am lost on where or how (more of how) to insert it.

At the risk of being blunt, your code (at least as posted) cannot possibly be correctly identifying widgets with the work "type" in the widgetName element, because you're checking the age element (converted to a string).

Other than that, because you're checking each element with the correct name (or will be, once you fix that bug), and then keeping track of the maximum age value, and the vector-index of the corresponding element, you -are- in effect "storing that widget's info for comparison". The element is already stored in the vector, and the index allows you to get back to it at any point. If you wish, you can more explicitly copy the Element of interest into a separate variable, and perform your comparisons against that, including when to replace it with a new Element, but remember to keep track of the case where you haven't yet found any valid element, and at the end of you loop, check whether any elements with "type" in the widgetName were found.

Which reminds me, if you keep the code more or less as-is, be sure to initialize both highest_age and highest_age_index to sensible values (maybe -1 for both). Especially for the index (currently default-initialized to zero), if you go through all the widgets in your vector and don't find any with "type" in the widgetName, at the end of the loop, you'll incorrectly believe that element zero had the maximum age (of zero), even though it was never considered as a possibility (and in addition to not having "type" in its widgetName, probably also does not have an age of zero).

The following code has solved the problem. Thanks everyone for there help

for (int i = 0; i < widgetTypes.size(); ++i){
		widgetFinder = widgetTypes[i].widgetName;
		if (widgetFinder.find("type", 0) != string::npos){
			widgetAge = widgetTypes[i].age;
			if (widgetAge > highest_age){
				highest_age = widgetAge;
				highest_age_location = widgetTypes[i].widgetName;
			}
		}
	}

Thanks again everyone.

assume you initialize the vector as follows:

vector<widgets> widgetTypes;
vector<int> agevector; // vector to hold ages alone


struct widgets wid;
wid.widgetname = widget one type
wid.age=45
wid.color=blue


widgetTypes.push_back(wid2);

struct widgets wid2;
wid2.widgetname = widget two
wid2.age=34
wid2.color=yellow

widgetTypes.push_back(wid2);


struct widgets wid3;
wid3.widgetname = widget eight
wid3.age=46
wid3.color=green



// compare the widget name has type and if it has fill the vector

if (widwidgetname.find("type") != -1)
{
    agevector.push_back(wid.age);
}



widgetTypes.push_back(wid3);

Hence after this you will have a vector with ages alone whose widgetNames contains type.
Thus iterate this agevector to find the hightest age.

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.