CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    Join Date
    Oct 2012
    Location
    Kromberk, Slovenia
    Posts
    8

    Red face 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"

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    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/
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  3. #3
    Join Date
    Oct 2012
    Location
    Kromberk, Slovenia
    Posts
    8

    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 ...

  4. #4
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    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.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  5. #5
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    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.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  6. #6
    Join Date
    Aug 2008
    Posts
    112

    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,,,

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: PHP array in C++ ?

    Quote Originally Posted by ArdentAngel View Post
    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.

  8. #8
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: PHP array in C++ ?

    Quote Originally Posted by ArdentAngel View Post
    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

  9. #9
    Join Date
    Oct 2012
    Location
    Kromberk, Slovenia
    Posts
    8

    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.

  10. #10
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: PHP array in C++ ?

    Quote Originally Posted by ArdentAngel View Post
    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.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  11. #11
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: PHP array in C++ ?

    Quote Originally Posted by ArdentAngel View Post
    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

  12. #12
    Join Date
    Apr 1999
    Posts
    27,449

    Re: PHP array in C++ ?

    Quote Originally Posted by ArdentAngel View Post
    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

  13. #13
    Join Date
    Oct 2012
    Location
    Kromberk, Slovenia
    Posts
    8

    Talking Re: PHP array in C++ ?

    Quote Originally Posted by D_Drmmr View Post
    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?
    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.

    Right now for C++ I am using pure google only ... And what I find that way ...
    So I am open for any book any of you might advise to be a good one.
    Last edited by ArdentAngel; October 12th, 2012 at 08:30 AM. Reason: Expanding it ...

  14. #14
    Join Date
    Apr 1999
    Posts
    27,449

    Re: PHP array in C++ ?

    Quote Originally Posted by ArdentAngel View Post
    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.

  15. #15
    Join Date
    Oct 2012
    Location
    Kromberk, Slovenia
    Posts
    8

    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++.

Page 1 of 2 12 LastLast

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
  •  





Click Here to Expand Forum to Full Width

Featured