C/C++ for Python programmer [closed]

断了今生、忘了曾经 提交于 2019-12-02 17:20:58
enobayram

OK, for that particular example:

  • you can get the time services from the standard library here
  • you can use eigen for linear algebra. It's an amazing library, I'm in love with it.
  • check here to learn how to manipulate files

While using C++, you might miss some features from python, but most of them are actually provided by the boost libraries. For instance returning multiple values from a function is very easy with boost.tuple library as in here. You can use boost::shared_ptr if you don't want to bother yourself with memory management. Or if you want to keep using python to play with your c++ classes, you can use boost.python. Boost.parameter helps you define functions with named arguments. There is also Boost.lambda for lambda functions, but if your environment supports it, you can also use C++11 to have language support for lambda functions. Boost is a gold mine, never stop digging. Just assume that it's part of the standard library. I develop C++ in many different platforms, and neither eigen nor boost has let me down yet.

Here's a good FAQ for C++ best practices. This is a very important principle that you have to keep in mind at all times, while working in C++. I extend it a bit, in my mind and think; If you're going to do something dangerous such as: Allocate memory with a raw new, or index a raw C style array, pass around raw pointers, or do static_cast (even worse reinterpret_cast) etc. They should usually happen in a class somehow dedicated to them, and the code to make sure they don't cause trouble lives very close to them, so that you can see at a glance that everything is under control.

Finally, my favourite!!! Do you want to keep using generators in C++? Here's some dark magic.

Alright, lets just start with C for now.

void readH5Data(FILE *file, int ***sample);   // this is for you to implement
void writeH5Data(FILE *file, int ***sample);  // this is for you to implement

int main(int argc, const char *argv[])
{
#define width 3
#define height 3
#define depth 3

    time_t t = time(NULL);

    int ***sample = calloc(width, sizeof(*sample));

    for (int i = 0; i < width; i++)
    {
        sample[i] = calloc(height, sizeof(**sample));
        for (int j = 0; j < height; j++)
        {
            sample[i][j] = calloc(depth, sizeof(***sample));
        }
    }

    for (int i = 0; i < 1000; i++)
    {
        char *filename[64];
        sprintf(filename, "mill2sort-%i-extracted.h5", i);

        // open the file
        FILE *filePtr = fopen(filename, "r");

        if (filePtr == NULL || ferror(filePtr))
        {
            fprintf(stderr, "%s\n", strerror(errno));
            exit(EXIT_FAILURE);
        }
        readH5Data(filePtr, sample);

        fclose(filePtr);
    }

    char filename[] = "mill2sort-extracted-all";

    FILE *writeFile = fopen(filename, "w");

    if (writeFile == NULL || ferror(writeFile))
    {
        fprintf(stderr, "%s\n", strerror(errno));
        exit(EXIT_FAILURE);
    }

    writeH5Data(writeFile, sample);

    fflush(writeFile);
    fclose(writeFile);

    printf("Done in %lli seconds\n", (long long int) (time(NULL) - t));

    for (int i = 0; i < width; i++)
    {
        for (int j = 0; j < width; j++)
        {
             free(sample[i][j]);
        }

        free(sample[i]);
    }

    free(sample);
}

As long as you remember that your array is 3x3x3, you should have no problems overstepping the bounds in your 'writeH5Data' method.

cedbeu

This question is getting quite old, but here is a couple of references that have been useful to me:

A Transition Guide: Python to C++ (pdf)

A Brief Introduction to C++ for Python programmers (incomplete but quite good)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!