从标准容器访问类 [英] Accessing classes from standard containers

查看:42
本文介绍了从标准容器访问类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我有一些我正在研究的代码,问题不在于它不是b
工作这是太慢了。我有一个类在std :: map中的std :: map中保存我的自制类

。这是声明。


std :: map< char,std :: map< char,CWordFrequency> > baseMap;


现在我可以指定一个指向内部地图的指针,但我不能将

指针分配给我的类,而不会将第二个地图复制到内存中。我不确定但是我认为我需要使用的运算符是。*或者 - > *。


有人可以指出我正确的方向吗?


这是缓慢但有效的代码片段。


sequenceFrequencyMap = baseMap [base];

tempFrequency = sequenceFrequencyMap [sequence];


if( wordPosition> 0&&(wordPosition + 1)<(word.length() -

1)){tempFrequency.incrementCountWithin();}

else if(wordPosition == 0)

{tempFrequency.incrementCountBeginning();}

else if((wordPosition + 1)== word.length() - 1)< br $>
{tempFrequency.incrementCountEnd();}

sequenceFrequencyMap [sequence] = tempFrequency;

baseMap [base] = sequenceFrequencyMap;

Hello,

I have some code that I''m working on, the problem isn''t that it doesn''t
work it''s that it''s too slow. I have a class that holds my homemade class
within an std::map, within an std::map. here is the declaration.

std::map<char, std::map<char, CWordFrequency> > baseMap;

Now I can assign a pointer to the inner map, but I cannot assign a
pointer to my class without actually copying the second map into memory. I''m
not sure but I think the operators I need to use are ".*" or "->*".

Can somebody point me in the right direction?

Here is the slow but working code fragment.

sequenceFrequencyMap = baseMap[base];
tempFrequency = sequenceFrequencyMap[sequence];

if (wordPosition > 0 && (wordPosition + 1) < (word.length() -
1)) {tempFrequency.incrementCountWithin();}
else if (wordPosition == 0)
{tempFrequency.incrementCountBeginning();}
else if ((wordPosition + 1) == word.length() - 1)
{tempFrequency.incrementCountEnd();}

sequenceFrequencyMap[sequence] = tempFrequency;
baseMap[base] = sequenceFrequencyMap;

推荐答案

这里引用你的朋友:


CWordFrequency& wf = baseMap [base] [sequence] ;

if(...)

{wf.incrementCountWithin(); }

//等

References are your friends here:

CWordFrequency &wf = baseMap[base][sequence];
if( ... )
{ wf.incrementCountWithin(); }
// etc.


Juicer_X写道:
Juicer_X wrote:
你好,
<我有一些我正在研究的代码,问题不在于它b $ b不起作用它是'太慢了'。


首先,这个小代码片段有点难以破译

,因为它没有明确定义所有实例的类型/>
变量。在将来,显示正在使用的变量的

声明可能会有所帮助,这样你的问题就更清楚了。

更清楚。

从您发布的上下文中,我只能假设有
声明类似于以下代码中某些地方的声明:

您共享的块:


std :: map< char,CWordFrequency> sequenceFrequencyMap;

CWordFrequency tempfrequency;

char base;

char序列;


我的答案将会依靠这些假设...


现在有很多事情可以做些加速代码。

首先,声明:

sequenceFrequencyMap = baseMap [base];
tempFrequency = sequenceFrequencyMap [sequence];


创建std :: map< char,CWordFrequency>的临时实例然后分别使用
CWordFrequency进行操作。

这些是非常昂贵且看似不必要的操作在这个

的案例中。 />

简单地获取对CWordFrequency

对象的引用(通过map :: operator []),然后调用incrementXXXX()
使用此引用的方法。这样做,只需摆脱上面的行

,并用'/ b $ b $''替换你使用''tempFrequency''的每个地方'baseMap [base] [sequence]' '


由于您现在将直接使用嵌入式对象,因此您无需将b-b的副本重新插入地图中,因此

下面的代码也是不必要的。 sequenceFrequencyMap [sequence] = tempFrequency;
baseMap [base] = sequenceFrequencyMap;
Hello,

I have some code that I''m working on, the problem isn''t that it doesn''t work it''s that it''s too slow.
First off, this small code fragment is a bit difficult to decipher
because it doesn''t clearly define the types of all of the instance
variables. In the future, it might be helpful for you to show the
declarations of the variables being used, so that your question is a
bit more clear.
From the context of your posting, I can only assume that there are declarations similar to the ones below someplace in the code prior to
the block you shared:

std::map<char, CWordFrequency> sequenceFrequencyMap;
CWordFrequency tempfrequency;
char base;
char sequence;

My answer will rely on these assumptions...

Now there are quite a few things one could do to speed up the code.
First off, the statements:
sequenceFrequencyMap = baseMap[base];
tempFrequency = sequenceFrequencyMap[sequence];
create temporary instances of std::map<char, CWordFrequency> and
CWordFrequency, respectively, that you then perform operations on.
These are extremely costly and seemingly unecessary operations in this
case.

It would be wiser to simply obtain a reference to the CWordFrequency
object (via map::operator[]), and then call your incrementXXXX()
methods using this reference. Do do this, simply get rid of the lines
above, and replace each place you use ''tempFrequency'' with
''baseMap[base][sequence]''

Since you will now be working with your embedded object directly, you
will have no need to re-insert a copy of it into your map, therefore
the code below will also be unnecessary. sequenceFrequencyMap[sequence] = tempFrequency;
baseMap[base] = sequenceFrequencyMap;




同样,上面的代码创建了大型对象的不必要副本,

因此慢慢下来。进行这些更改后,您的代码应该显着更快,并且看起来像下面的代码片段:


std :: map< char,std :: map< char,CWordFrequency> > baseMap;

if(wordPosition> 0&&

(wordPosition + 1)<(2 - 1)){

baseMap [base] [sequence] .incrementCountWithin();

}

else if(wordPosition == 0){

baseMap [base] [sequence] .incrementCountBeginning();

}

else if((wordPosition + 1)== 2 - 1){

baseMap [ base] [sequence] .incrementCountEnd();

}


还有一件事需要注意......

由于允许标准容器随意复制

他们的条目,很可能使用原始或托管指针

到你的CWordFrequency对象你的地图也会增加

的速度。话虽如此,这是一个不太明显的优化,不知道CWordFrequency对象是什么。在任何情况下,我通常建议它除了最原始的类型之外的大多数东西都是


我希望这有帮助!


Michael Loritsch



Again, the code above was creating unnecessary copies of large objects,
and thus slowly you down. After making these changes, your code should
be significantly faster, and look something like the following snippet:

std::map<char, std::map<char, CWordFrequency> > baseMap;
if (wordPosition > 0 &&
(wordPosition + 1) < (2 - 1)) {
baseMap[base][sequence].incrementCountWithin();
}
else if (wordPosition == 0) {
baseMap[base][sequence].incrementCountBeginning();
}
else if ((wordPosition + 1) == 2 - 1) {
baseMap[base][sequence].incrementCountEnd();
}

There is one more thing that should be noted...

Since the standard containers are allowed to arbitrarily make copies of
their entries, it is likely that using either raw or managed pointers
to your CWordFrequency objects within your maps would also increase
speed. That being said, it is a less obvious optimization, not knowing
what a CWordFrequency object is. In any case, I normally recommend it
for most anything but the most primitive types.
I hope this helps!

Michael Loritsch




Juicer_X写道:

Juicer_X wrote:
你好,

我有一些我正在研究的代码,问题不在于它b $ b不起作用,它太慢了。我有一个类在std :: map中的std :: map中保存我自制的
类。这是声明。

std :: map< char,std :: map< char,CWordFrequency> > baseMap;

现在我可以指定一个指向内部地图的指针,但是我无法将
指向一个指向我的类的指针,而不会将第二个地图复制到
内存中。我不确定,但我认为我需要使用的操作符是。*。或 - > *。


编号首先,地址操作符是&,其次,你需要做的唯一的事情就是命名对象。 />
这是缓慢但有效的代码片段。

sequenceFrequencyMap = baseMap [base];
tempFrequency = sequenceFrequencyMap [sequence];


这不是指针!你正在复制整个内部地图,并且

然后你从你的副本中复制你的课程!

if(wordPosition> 0&&(wordPosition) + 1)<
(word.length() - 1)){tempFrequency.incrementCountWithin();}
else if(wordPosition == 0)
{tempFrequency.incrementCountBeginning(); }
else if((wordPosition + 1)== word.length() - 1)
{tempFrequency.incrementCountEnd();}

sequenceFrequencyMap [sequence] = tempFrequency;
baseMap [base] = sequenceFrequencyMap;


简单优化:

((wordPosition + 1)== word.length() - 1)==

( wordPosition + 2 == word.length())

编译器可能无法做到这一点,因为不太可能看到

不会发生溢出。


/ real /解决方案是使用引用(而不是指针):


std :: map< char,CWordFrequency>& sequenceFrequencyMap = baseMap [base];

CWordFrequency& tempFrequency = sequenceFrequencyMap [sequence];


这意味着你不会复制对象,而是将它们修改到位。

因此,副本-back步骤也可以跳过,即不再需要
sequenceFrequencyMap [sequence] = tempFrequency;
baseMap [base] = sequenceFrequencyMap;
Hello,

I have some code that I''m working on, the problem isn''t that it doesn''t work it''s that it''s too slow. I have a class that holds my homemade class within an std::map, within an std::map. here is the declaration.

std::map<char, std::map<char, CWordFrequency> > baseMap;

Now I can assign a pointer to the inner map, but I cannot assign a pointer to my class without actually copying the second map into memory. I''m not sure but I think the operators I need to use are ".*" or "->*".
No. First, the address-of operator is &, and secondly, the only thing
you need to do is name the object.
Here is the slow but working code fragment.

sequenceFrequencyMap = baseMap[base];
tempFrequency = sequenceFrequencyMap[sequence];
That''s not a pointer! You''re copying the entire inner map out, and
then you copy your class out of that copy!
if (wordPosition > 0 && (wordPosition + 1) < (word.length() - 1)) {tempFrequency.incrementCountWithin();}
else if (wordPosition == 0)
{tempFrequency.incrementCountBeginning();}
else if ((wordPosition + 1) == word.length() - 1)
{tempFrequency.incrementCountEnd();}

sequenceFrequencyMap[sequence] = tempFrequency;
baseMap[base] = sequenceFrequencyMap;
Simple optimalization:
((wordPosition + 1) == word.length() - 1) ==
(wordPosition + 2 == word.length() )
Compiler probably can''t do that, because it is unlikely to see
that no overflow can happen.

The /real/ solution is to use references (not pointers):

std::map<char, CWordFrequency>& sequenceFrequencyMap = baseMap[base];
CWordFrequency& tempFrequency = sequenceFrequencyMap[sequence];

This means you won''t copy the objects, but modify them in place.
As a result, the copy-back step can also be skipped, i.e. no more
sequenceFrequencyMap[sequence] = tempFrequency;
baseMap[base] = sequenceFrequencyMap;



问候,

Michiel Salters


Regards,
Michiel Salters


这篇关于从标准容器访问类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆