Hi, I have a question. How could I create an array that would in PHP look like this:
PHP Code:
$something['argument1']['argument2'] = "some data to store";
Printable View
Hi, I have a question. How could I create an array that would in PHP look like this:
PHP Code:
$something['argument1']['argument2'] = "some data to store";
maybe?Code:std::vector< std::vector< 'type of some data to store' > > something;
See http://cplusplus.com/reference/stl/vector/
But i am only thinking about holding data like coords or something, not some large amounts of sentences. Is there no kind of variable, like char (i tried lots of possibilities with that, but can't really get anywhere ...). What I want to do is a script that calculates distance between 2 points in coordinate system and I want specify which coordinates.
With of course y and z also. ;)PHP Code:
//Models array should only have this kind of values:
$models['object1']['x'] = 7831;
$models['object2']['x'] = 1893;
function distance($objName1, $objName2){
x1 = models[$objName1]['x'];
x2 = models[$objName2]['x'];
//Function that actually does something
}
//And than I simply put in the objects names in the arg field:
distance("object1", "object2");
But I believe you got the point ...
If vectors are the only solution, than I guess I'll go with it ...
Ok so all you need is two objects that can hold the coordinates for a point? If it's just 2 dimensions Windows has POINT. See http://msdn.microsoft.com/en-us/libr...(v=VS.80).aspx
Edit: Ah, but you can easily create your own structure to hold more coordinates.
I think I missed some things here. What you want is probably more like thishttp://cplusplus.com/reference/string/string/Code:
#include <map>
#include <string>
struct Point
{
int x;
int y;
int z;
};
std::map< std::string, Point > models;
int distance( const std::string& objName1, const std::string& objName2 )
{
int x1 = models[objName1].x;
int x2 = models[objName2].x;
//Function that actually does something
return 0;
}
int main()
{
models["object1"].x = 7831;
models["object1"].y = 0;
models["object1"].z = 0;
models["object2"].x = 1893;
models["object2"].y = 0;
models["object2"].z = 0;
int dist = distance( "object1", "object2" );
return 0;
}
http://cplusplus.com/reference/stl/map/
Maybe you also need a if-then-else for
distance["object1","object1"];
as "mapping" fails due to the same key used
To add to SMA's example:
Regards,Code:#include <map>
#include <string>
#include <iostream>
template <typename T>
struct Point
{
T x;
T y;
T z;
Point(T theX = T(), T theY = T(), T theZ = T()) : x(theX), y(theY), z(theZ) {}
};
typedef std::map<std::string, Point<double> > FloatModels;
typedef std::map<std::string, Point<int> > IntModels;
using namespace std;
int main()
{
FloatModels models;
IntModels models2;
models["object1"] = Point<double>(); // defaults to 0.0
models["object2"] = Point<double>(3.0, 2.0, -1.9);
models2["object1"] = Point<int>(); // defaults to 0
models2["object2"] = Point<int>(3, 2, -1);
cout << "Here are the floating point coordinates:\n";
FloatModels::iterator it = models.begin();
while (it != models.end() )
{
cout << it->first << " -> " << "(" << it->second.x << ", " << it->second.y << ", " << it->second.z << ")\n";
++it;
}
cout << "\nHere are the integer point coordinates:\n";
IntModels::iterator it2 = models2.begin();
while (it2 != models2.end() )
{
cout << it2->first << " -> " << "(" << it2->second.x << ", " << it2->second.y << ", " << it2->second.z << ")\n";
++it2;
}
}
Output:
Here are the floating point coordinates:
object1 -> (0, 0, 0)
object2 -> (3, 2, -1.9)
Here are the integer point coordinates:
object1 -> (0, 0, 0)
object2 -> (3, 2, -1)
Paul McKenzie
I assume the underlying datastructure is a hash table. That's available in C++ as an unordered_map. In the newest compilers this will be available in the std namespace (header <unordered_map>), in older compilers it may be available in the std::tr1 namespace.
Thanks, that really helped. Now just to be clear: Why is in every function "return 0" command? (just to learn properly what you guys just pointed me out). I am a novice in C++. I thought it would be ... different ... I was able to get the hang of C# in period of 3 days, so I did not expected C++ to be that much different (specially from reading PHP books, because they always pointed out some other differences ...)
So thanks again to everyone.
It's just because it shall be compilable if somebody copies the code. int distance(...) has to return something but as it is now it is incomplete.
The same goes for int main(). Sometimes you see people use void main and that's not ok according to the standard but accepted by most compilers. I think the reason for it is that in an embedded system there's nobody to return to so void main is quite common. If a watchdog isn't used, returning from main means that the unit has to be power cycled to be restarted.
No, but I did expect to discover by myself how to make the array look the way I asked here at the end. :)
I knew at the very beginning that C++ is advanced language. I never even said that I am a pro at PHP, I am only saying that I did not expect there to be that much of a difference, since with C# I got familiar with (as much as I could for what I had to write in it for a friend who had been learning this in school and none of his classmates knew how to do it. I was his last chance of making it happen (altough I am 3 years younger than he is) ... it wasn't that hard, I admit - I mostly had to get the hang of arrays, which took me 3 hours, since I am a self-learning programmer (if I can call my self that way), so I guess you know how that is ...) quite fast.
But now I see here is a way more than I first expected. :D
Right now for C++ I am using pure google only ... And what I find that way ... :D
So I am open for any book any of you might advise to be a good one.
There is a major difference in C++ and C#, one that may shock you. It is the major difference between C/C++ and most other language that's out there.
In C#, you make a mistake that violates memory in some way, you get a nice error at runtime saying you made a mistake. With C++, you are not guaranteed to get any such error message or warning when you make these mistakes. For example, access an array with an out-of-bounds subscript in C#, then do the same thing in C++. With C++, there is no guarantee that you get an "array subscript error" or similar message at runtime -- the program will run with that bug, and who knows what damage is being done.
The array subscript is just one out of many examples. You have heap management errors, buffer overwrites all caused by a myriad of different reasons, etc.
So you were learning all along "safe" languages (C#, PHP), where you can't make these types of errors without the system telling you that you made these errors. With C++, you're on your own if you make such errors. So you can code all sorts of crap with C++, the code will compile, and may even run, giving you the false sense that everything is OK when it isn't. Run the program on your customer's machine, and everything goes wrong.
To avoid such errors, write code so that it's safe and easily maintainable, etc. requires real experience, not a few days of tinkering around with C++.
Regards,
Paul McKenzie
Thank you all. The code worked perfectly (and I got the hang of it that far). But now I want to split it to multiple header and cpp files. However I keep getting :
error C2653: 'aa_physics' : is not a class or namespace name
error C3861: 'distance': identifier not found
Files are:
main.h
predef.hCode:#include "predef.h"
#include <iostream>
int main()
{
models["object1"].x = 1;
models["object1"].y = 2;
models["object1"].z = 3;
models["object2"].x = 9;
models["object2"].y = 8;
models["object2"].z = 7;
std::cout << aa_physics::distance("object1", "object2");
system("PAUSE");
};
aa_physics.hCode:#include <map>
#include <string>
struct Point
{
int x;
int y;
int z;
};
std::map< std::string, Point > models;
aa_physics.cppCode:#pragma once
#include "main.h"
class aa_physics
{
public:
static int distance(const std::string& objName1 , const std::string& objName2);
};
PS.Code:#include "aa_physics.h"
int aa_physics::distance(const std::string& objName1, const std::string& objName2)
{
int x1 = models[objName1].x;
int x2 = models[objName2].x;
int y1 = models[objName1].y;
int y2 = models[objName1].y;
int z1 = models[objName1].z;
int z2 = models[objName2].z;
return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)+(z1-z2)*(z1-z2));
};
So, do you know any really good C++ programming books?