Sign in to follow this  
esperknight

Vectors combined with structures

Recommended Posts

esperknight    124
I hope I'm not asking a stupid question but i'm trying to use vectors combined with contructors. A basic example from what I'm doing is: struct click_points { int x; int y; int max_x; int max_y; string default_image; string on_over; string function; }; vector<click_points> v_click_points; click_points temp_click_points; v_click_points.push_back(temp_click_points); cout << "vector = " << v_click_points[0].default_image; my code basically puts everything into the temp_click_points variable then pushes into the vector but no matter which element i access in the vector, it seems to return the y one. Am i pushing this incorrectly? I've been trying different ways to push it onto the vector by specifying which variable im accessing in the struct on either one but nothing is working. Any ideas or solutions are appreciated! Thanks! EsperKnight

Share this post


Link to post
Share on other sites
Fruny    1658
I see no reason why it should always "return the y one", even though I am a bit uncertain as what you mean by that. Can you post the actual code you are using, the results you get, and the results you expected?

Share this post


Link to post
Share on other sites
marshdabeachy    100
I don't see the problem either, but this...

Quote:
i'm trying to use vectors combined with contructors


... is a typo. You meant "structures" instead "constructors," right?

Share this post


Link to post
Share on other sites
esperknight    124
Wow, structures is right, i even missed that rereading my code but i'll post what I'm doing here.


struct click_points
{
int x;
int y;
int max_x;
int max_y;
string default_image;
string on_over;
string function;
};




//This reads the pushed back data code and builds the menu
int Menu_Build_Display(vector<string> menu_data)
{
string temp;
int start, end;
start = 0;
end = 0;

int i = 0;

vector<click_points> v_click_points;
click_points temp_click_points;

start = menu_data[i].find("<FUNCT=");
end = menu_data[i].find(" ");

while (i < menu_data.size())
{
start = menu_data[i].find("<FUNCT=");
end = menu_data[i].find(" ");

if (start == -1)
{
//cout << "ERROR ERROR WILL ROBINSON! YOU ARE MISSING <FUNCT= TO START" << endl
// << "YOUR CODE! BEEP BEEP!" << endl;
}
else
{
start = start + 7;
end = end - start;
temp_click_points.function = menu_data[i].substr(start, end);

//v_click_points.push_back(temp_click_points);

start = menu_data[i].find("img=");
if (start > -1)
{
if ( (start = menu_data[i].find("./", start)) > -1)
{
start = menu_data[i].find("./", start) + 2;
}

end = menu_data[i].find(" ", start);
end = end - start;

temp_click_points.default_image = menu_data[i].substr(start, end);
}

start = menu_data[i].find("onover=");
if (start > -1)
{
if ( (start = menu_data[i].find("./", start)) > -1)
{
start = menu_data[i].find("./", start) + 2;
}
end = menu_data[i].find(" ", start);

end = end - start;
temp_click_points.on_over = menu_data[i].substr(start, end);
}

start = menu_data[i].find("x=");
if (start > -1)
{
start = start + 2;

end = menu_data[i].find(" ", start);
end = end - start;
temp = (menu_data[i].substr(start, end));
temp_click_points.x = atoi(temp.c_str());
}

start = menu_data[i].find("y=");
if (start > -1)
{
start = start + 2;

end = menu_data[i].find(" ", start);
if (end == -1)
{
end = menu_data[i].find(">", start);
}
end = end - start;
temp = (menu_data[i].substr(start, end));

temp_click_points.y = atoi(temp.c_str());

}


v_click_points.push_back(temp_click_points);

cout << "vector = " << v_click_points[0].y;
}
i++;
}

return 0;
}




An example of the script it reads in is (there are quite few more that go through this) :
<FUNCT=#ToC img=./Graphics/Interface/start.bmp onover=./Graphics/Interface/startGlow.bmp x=70 y=295>

it does read it in fine, but trying to push back the temporary structure does not produce anything in the vector. I tried just now outputting it. I can output the y value using cout << "vector = " << v_click_points[1].y; will return 295 but using cout << "vector = " << v_click_points[0].y; will return 0 (which should actually return 295) cout << "vector = " << v_click_points[0].default_image; returns nothing. Hope this helps explain it a bit better. Thanks!

EK

Share this post


Link to post
Share on other sites
Fruny    1658
Quote:
Original post by esperknight
I can output the y value using

cout << "vector = " << v_click_points[1].y; will return 295

but using

cout << "vector = " << v_click_points[0].y; will return 0

(which should actually return 295)

cout << "vector = " << v_click_points[0].default_image; returns nothing.

Hope this helps explain it a bit better. Thanks!

EK


Yes, it looks to me like you are inserting a bad "click point" prior to the one you really want. Double check v_click_points.size(). Print the contents of temp_click_point prior to inserting it in the vector.

Additionally, passing menu_data by value is inefficient: try int Menu_Build_Display(const vector<string>& menu_data) instead.

You may also use a string reference instead of all those menu_data[i]:

while (i < menu_data.size())
{
const string& data = menu_data[i]

start = data.find("<FUNCT=");
end = data.find(" ");

...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this