由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 请教C++class
相关主题
java在图像分析领域,就是一个扶不起的阿斗Is it possible to initialize container in initialization list?
讨论程序语言没什么意思,functional programming还是不错的能帮我看看Ruby的这道题吗?
求推荐:fortran好用的debug软件Remove elements from multiple vectors in C++
static vector 怎么 initialize ?static initialization dependency c++
问个C++ 中拷贝vector的问题问个double和long double的问题
一个 default constructor 的问题嵌入式编程问题
【C++】请问这样有没有memory leak?多谢一道 memset in C++的题
C++要是有null object就好了瓶颈在哪儿?
相关话题的讨论汇总
话题: country话题: class话题: vector话题: c++
进入Programming版参与讨论
1 (共1页)
x******a
发帖数: 6336
1
I need a class with some thing like
class foo{
std::string country; //an attribute
std::vector somefactors; //really need for calculation
...
}
The data members currency and somefactors are uniquely determined by the
country with a fooparameters class contains the map from country to the rest.
The reason I don't want to implement as a method like get_factors() is that
in reality I need to run large number of Monte-Carlo simulation
using somefactors and I really need the speed.
currently I initialize the class by order: first initialize country and
then somefactors by calling the fooparameters,
can you help me improve the class/pattern design?
thanks!
D***n
发帖数: 6804
2
真复杂,如果你真想提高速度,请把这些乱七八糟的东西去掉。
int country; /* 整数国家代码 */
double ** somefactors; /* double 指针指向你的somefactor lookup table */
访问的时候
double * factorsByCountry = somefactors[country];
这多简单?
Country Name和Country整数的对应关系,搞一个数组比如countryName,里面成员是国
家字符串的首地址。 用的时候直接countryName[country]访问不就得了?

【在 x******a 的大作中提到】
: I need a class with some thing like
: class foo{
: std::string country; //an attribute
: std::vector somefactors; //really need for calculation
: ...
: }
: The data members currency and somefactors are uniquely determined by the
: country with a fooparameters class contains the map from country to the rest.
: The reason I don't want to implement as a method like get_factors() is that
: in reality I need to run large number of Monte-Carlo simulation

m********t
发帖数: 13072
3
using namespace std;

【在 D***n 的大作中提到】
: 真复杂,如果你真想提高速度,请把这些乱七八糟的东西去掉。
: int country; /* 整数国家代码 */
: double ** somefactors; /* double 指针指向你的somefactor lookup table */
: 访问的时候
: double * factorsByCountry = somefactors[country];
: 这多简单?
: Country Name和Country整数的对应关系,搞一个数组比如countryName,里面成员是国
: 家字符串的首地址。 用的时候直接countryName[country]访问不就得了?

k**********g
发帖数: 989
4

rest.
that
Use C++11
Use a C++11 compiler that is less buggy than the other competitors
Use unordered_map, or "Boost multiindex"
For read-only access to the vector, write the read-only accessor as follows
const std::vector& getFactors() const { return m_someFactors; }
If you need to modify the vector, **as long as you don't change the vector
size and don't write to a location beyond its current size**, you can
actually just return a reference to the vector (without the const)
A C++ STL vector is guaranteed contiguous in memory. Therefore, you can take
the start pointer of a vector (the address of the first element of the
vector), and then use pointer-based access directly. This technique can be
useful if you know how to write C code **correctly**. Alternatively you can
also write SIMD code with this technique.
However, if your code is multi-threaded, you need to be extra careful, as
people always say. So my advice above should not be used in the multi-
threaded case.

【在 x******a 的大作中提到】
: I need a class with some thing like
: class foo{
: std::string country; //an attribute
: std::vector somefactors; //really need for calculation
: ...
: }
: The data members currency and somefactors are uniquely determined by the
: country with a fooparameters class contains the map from country to the rest.
: The reason I don't want to implement as a method like get_factors() is that
: in reality I need to run large number of Monte-Carlo simulation

k**********g
发帖数: 989
5
Also ask your coworkers to see if Eigen or NT2 (Numerical Template Toolbox)
can be used for your project.
x******a
发帖数: 6336
6
Decon: Thanks for the answer, the problem is that there are a lot other
attributes besides the currency.
kinchungwong: thanks for your answer. I used to use pandas DataFrame
multiindex for this. However, the speed is slow. I will try boost multiindex.
j******o
发帖数: 4219
7
如果你唯一的concern是速度的话,把所有country的属性都定义好,然后直接读取某个
country就行了,因为country的的数量和属性是固定的。
1 (共1页)
进入Programming版参与讨论
相关主题
瓶颈在哪儿?问个C++ 中拷贝vector的问题
C++多线程和硬件的关系一个 default constructor 的问题
震惊:java 的矩阵操作比 c++ 快?【C++】请问这样有没有memory leak?多谢
c++ 不自动initialize变量么?C++要是有null object就好了
java在图像分析领域,就是一个扶不起的阿斗Is it possible to initialize container in initialization list?
讨论程序语言没什么意思,functional programming还是不错的能帮我看看Ruby的这道题吗?
求推荐:fortran好用的debug软件Remove elements from multiple vectors in C++
static vector 怎么 initialize ?static initialization dependency c++
相关话题的讨论汇总
话题: country话题: class话题: vector话题: c++