| Callum5042 (125) | |
|
I have a vector that holds a bunch of numbers for objects. But say you got the numbers "2, 3, 4, 6", and say when I used the function to create them, I created them under string "Create". How would I be able to put this string in to find the amount of number related to the string. Also, it is not a set number. How would I go with doing this? | |
|
|
|
| strongdrink (444) | |||
What about a stringstream?
| |||
|
Last edited on
|
|||
| Need4Sleep (532) | |||
The most simple method would be to use a for loop to run through each element of a string, since a string is just an array of chars.
OR you if you want efficiency use quicksort followed by a binarysearch to find your number. Hope this helps | |||
|
|
|||
| Callum5042 (125) | |
|
I'm not searching through the character of the string though, I'm at college but I'll post the code of how it is created atm so you understand what I mean a bit more. But let say: int 1, 3, 4, 7, 9 are created under the string "First" int 2, 5, 6, 8, 10 are created under the string "Second" Then let say I can use "First" to move all the numbers under it. I was thinking of maybe a multimap, but not too sure if it would work. | |
|
|
|
| JLBorges (1336) | |||
|
> I was thinking of maybe a multimap, but not too sure if it would work It would work. Though you might find that it is more convenient to use a std::map< std::string, std::vector<int> >
| |||
|
|
|||
| Callum5042 (125) | |||
Hmm not sure if it working yet, but I'm using thisstd::map<std::string, std::vector<Object>> ObjectName;Then this when I want to create a new object
But how would I go with displaying all of the Objects within the map? | |||
|
|
|||
| JLBorges (1336) | |||
> ObjectAmount++;A std::vector<> keeps track of the number of elements in it; ObjectAmount is redundant.http://www.cplusplus.com/reference/stl/vector/size/ > how would I go with displaying all of the Objects within the map?
| |||
|
|
|||
| Callum5042 (125) | ||||||
That works fine
But I see what I'm doing wrong, I need it to create a new map everytime I need a different name.
Now only main will be created, but I want to be able to create others when I call that function. And ObjectAmount, is for setting the amount. If you see what I mean. | ||||||
|
|
||||||
| JLBorges (1336) | |
|
> If you see what I mean. No, I'm afraid I don't. | |
|
|
|
| Callum5042 (125) | |||||||
|
I'll try to explain, I'm writing a .lib file for a simple game engine. To create a new object I use this code engine->CreateObject("Main", "path", 128, 128);With main being the name of the object, path being the file path for the picture and 128 being x and y. So say when we press 'x' we do this
And if we press 'c' we do this
The CreateObject function
At the moment, I can only create one of them, but I create more then one under the same name. So I could only do: engine->CreateObject("bullet", "path", 256, 256); but not engine->CreateObject("missile", "path", 128, 128);So my question is, how would I be able to call more than just one of them? Hopefully this makes it a bit clearer. | |||||||
|
Last edited on
|
|||||||
| JLBorges (1336) | |||
Get rid of that ObjectAmount; it is completely useless.This is an error: ObjectName[name][ObjectAmount].Init(path, ObjectAmount, x, y);ObjectAmount holds the total number of objects under all names; it is an invalid index into the vector under a particular name. Instead, do something like this:
| |||
|
|
|||
| Callum5042 (125) | |||
|
Thanks, it working good now :D. You're quite the programmer :), just one more thing. This creates the bullet engine->CreateObject("Bullet","Path",engine->object["Main"].back().x,engine->object["Main"].back().y);But say I want to make them all move at -y; Now I can do this using a loop
Is there a easier way to do this?, something like. engine->object["Bullet"][ALL].y -= 2;Thanks | |||
|
|
|||
| JLBorges (1336) | |
|
> Is there a easier way to do this? Not really. A range based for loop would make the code cleaner: for( auto& obj : engine->object["Bullet"] ) obj.y -= 2 ; | |
|
|
|
| Callum5042 (125) | |
| Alright, cheers. | |
|
|
|