English 中文(简体)
一种良好的办法,是从沙捞取到C++的阵列价值?
原标题:A good way to output array values from Python and then take them in through C++?
  • 时间:2012-04-09 20:02:35
  •  标签:
  • c++

由于C++的溢出问题,我想用沙尔来掩盖一些价值观。 我有职能 f(a,b),然后产生价值。 我想能够把我所需要的所有价值根据一、二、三、二、三、二、三、二、三、三、三、三、四、五、四、五、四、五、四、五、四、五、五、五、六、五、八、八、九、八、八、八、九、八、八、九、八、九、八、八、八、八、九、八、八、九、八、八、八、九、八、八、八、八、九、八、八、八、八、八、九、八、八、九、八、八、八、九、八、八、八、八、八、八、九、八、八、九、八、八、八、九、九、八、八、九、八、八、八、八、八、八、八、八、八、八、八、九、八、八、九、八、八、八、九、八、八、八、八、八、八、八、九、八、九、八、九、八、九、九、八、八、八、八、

  1. What is a good format to output f(a,b) in?
  2. What s the best way to read this back into C++?
  3. Vector or multidim array?
问题回答

你可以用沙尔语书写一份与C++源星号相符的档案。

h_file.write( { )
for a in range(a_size):
    h_file.write( {  +  , .join(str(f(a, b)) for b in range(b_size)) +  },
 )
h_file.write( } )

你可能希望修改该法典,以在我后来能够显示的法典中增加一些新内容。

你可以用沙尔语书写载有你的数据的C++源代码。 E.g:

def f(a, b):
    # Your function here, e.g:
    return pow(a, b, 65537)
num_a_values = 50
num_b_values = 50
# Write source file
with open( data.cpp ,  wt ) as cpp_file:
    cpp_file.write( /* Automatically generated file, do not hand edit */

 )
    cpp_file.write( #include "data.hpp"
 )
    cpp_file.write( const int f_data[%d][%d] =
 
                       % (num_a_values, num_b_values))
    cpp_file.write( {
 )
    for a in range(num_a_values):
        values = [f(a, b) for b in range(num_b_values)]
        cpp_file.write(   {  +  , .join(map(str, values)) +  },
 )
    cpp_file.write( }
 )
# Write corresponding header file
with open( data.hpp ,  wt ) as hpp_file:
    hpp_file.write( /* Automatically generated file, do not hand edit */

 )
    hpp_file.write( #ifndef DATA_HPP_INCLUDED
 )
    hpp_file.write( #define DATA_HPP_INCLUDED
 )
    hpp_file.write( #define NUM_A_VALUES %d
  % num_a_values)
    hpp_file.write( #define NUM_B_VALUES %d
  % num_b_values)
    hpp_file.write( extern const int f_data[%d][%d];
 
                              % (num_a_values, num_b_values))
    hpp_file.write( #endif
 )

然后,你将产生的源代码作为项目的一部分加以汇编。 然后,你可以直接使用<代码>f_data[>阵列。

这对中小尺度数据表,例如信标,确实是好的。 对于较大的数据表(数百万项条目),一些汇编者将失败,你可能发现汇编/链接缓慢得难以接受。

如果你的数据更加复杂,你可以采用同样的方法确定结构。

[Based on Mark Ransom s answer, but with some style differences and more explanation].

如果数据微不足道,那么我就读到数据档案的记忆中的数据。 我将安排事情,以便我能够直接使用数据文档,而不必在开始时读到所有数据。

这样做的原因是,如果你只想使用某些价值,你就不想读一下数据。 通过利用记忆测绘,贵处将自动阅读你所需要的文件部分。 而如果你在援助团中处于低档状态,则贵处可以重新利用分配给该档案的记忆,而不必浪费时间将它写到点火上。

如果你的职能是单一数量的,你可能只想一.。 你们很可能需要2D阵列,例如:

#define DATA_SIZE (50 * 25)
typedef const int (*data_table_type)[50];
int fd = open("my_data_file.dat", O_RDONLY);
data_table_type data_table = (data_table_type)mmap(0, DATA_SIZE,
                                  PROT_READ, MAP_SHARED, fd, 0);
printf("f(5, 11) = %d
", data_table[5][11]);

For more info on memorymapped file, see Wikipedia , or the

如果你需要更复杂的数据结构,你可将C/C++结构和阵列列入档案。 但是,你可以穿透点或实际上没有任何东西的C++级。

一旦你决定如何阅读数据,下一个问题是如何生成数据。 rel=“nofollow”>struct. Pack() 这样做非常有用——这将使你能够转换。 粉碎的数值是经过适当调整的粉碎,然后可以写到档案中。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签