-
October 10th, 2012, 12:47 PM
#1
PHP array in C++ ?
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";
-
October 10th, 2012, 01:02 PM
#2
Re: PHP array in C++ ?
Code:
std::vector< std::vector< 'type of some data to store' > > something;
maybe?
See http://cplusplus.com/reference/stl/vector/
-
October 10th, 2012, 02:35 PM
#3
Re: PHP array in C++ ?
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.
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");
With of course y and z also.
But I believe you got the point ...
If vectors are the only solution, than I guess I'll go with it ...
Last edited by ArdentAngel; October 12th, 2012 at 12:33 AM.
Reason: Only merged to posts into one, to make it easier to track for everyone ...
-
October 10th, 2012, 02:40 PM
#4
Re: PHP array in C++ ?
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.
Last edited by S_M_A; October 10th, 2012 at 02:42 PM.
-
October 10th, 2012, 03:14 PM
#5
Re: PHP array in C++ ?
I think I missed some things here. What you want is probably more like this
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/string/string/
http://cplusplus.com/reference/stl/map/
Last edited by S_M_A; October 11th, 2012 at 02:28 AM.
-
October 10th, 2012, 06:30 PM
#6
Re: PHP array in C++ ?
Maybe you also need a if-then-else for
distance["object1","object1"];
as "mapping" fails due to the same key used
hi,,,
-
October 10th, 2012, 06:47 PM
#7
Re: PHP array in C++ ?
Originally Posted by ArdentAngel
Oh, and the models array should having this kind of values:
PHP Code:
$models['object1']['x'] = 7831;
$models['object2']['x'] = 1893;
To add to SMA's example:
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)
Regards,
Paul McKenzie
Last edited by Paul McKenzie; October 10th, 2012 at 06:50 PM.
-
October 11th, 2012, 04:20 AM
#8
Re: PHP array in C++ ?
Originally Posted by ArdentAngel
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";
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.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
October 12th, 2012, 12:30 AM
#9
Re: PHP array in C++ ?
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.
-
October 12th, 2012, 12:48 AM
#10
Re: PHP array in C++ ?
Originally Posted by ArdentAngel
Why is in every function "return 0" command?
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.
-
October 12th, 2012, 03:43 AM
#11
Re: PHP array in C++ ?
Originally Posted by ArdentAngel
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 ...)
Don't expect to learn any significant amount of C++ in 3 days. In 3 months you could learn something useful, but it'll take years to become really proficient in C++. Which book are you using to learn?
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
October 12th, 2012, 05:27 AM
#12
Re: PHP array in C++ ?
Originally Posted by ArdentAngel
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 ...)
If you really expected to learn C++ in 3 days, you'll be very disappointed. Look at what was posted by myself, S_M_A, and others. Did you really expect to come up with those solutions with just 3 or so days of learning C++?
Regards,
Paul McKenzie
-
October 12th, 2012, 06:56 AM
#13
Re: PHP array in C++ ?
Last edited by ArdentAngel; October 12th, 2012 at 08:30 AM.
Reason: Expanding it ...
-
October 12th, 2012, 11:51 AM
#14
Re: PHP array in C++ ?
Originally Posted by ArdentAngel
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
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
Last edited by Paul McKenzie; October 12th, 2012 at 11:53 AM.
-
October 14th, 2012, 02:15 PM
#15
Splitting the code to multiple files
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
Code:
#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");
};
predef.h
Code:
#include <map>
#include <string>
struct Point
{
int x;
int y;
int z;
};
std::map< std::string, Point > models;
aa_physics.h
Code:
#pragma once
#include "main.h"
class aa_physics
{
public:
static int distance(const std::string& objName1 , const std::string& objName2);
};
aa_physics.cpp
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));
};
PS.
So, do you know any really good C++ programming books?
Novice to programming in C++.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|