算法上使用复制构造函数的一元仿函数 [英] Unary functor on algorithm using copy constructor

查看:74
本文介绍了算法上使用复制构造函数的一元仿函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨伙计们!


首先,这是我用来揭露问题的代码:


---- -------------------------------------------------- ------------

#include< functional>

#include< string>

#include < iostream>


使用命名空间std;


struct space_comparer:public unary_function< char,bool {


mutable argument_type __lastChar;


space_comparer()

{

__lastChar =''X'';

cout<< " ctor()调用,lastChar是''" << __lastChar<< "" <<

endl;

}


space_comparer(const space_comparer& __ src)

{

cout<< copy ctor()将lastChar设置为''Y'' << endl;

__lastChar =''Y'';

}


result_type operator()(const argument_type c)const

{

cout<< " [" << __lastChar<< " | " << c<< "]" << endl;

__lastChar = c;

return :: isspace(c);

}


};

main()

{

string cad1;

string cad2;
< br $>
const space_comparer实例;


cad1 =" cpp";


remove_copy_if(cad1.begin(), cad1.end(),back_inserter(cad2),

实例);

remove_copy_if(cad1.begin(),cad1.end(),back_inserter(cad2),

实例);

remove_copy_if(cad1.begin(),cad1.end(),back_inserter(cad2),

实例);


返回0;

}

-------------------- -------------------------------------------

好​​的,所以所有的意思是使用具有

持久性状态的一元函数,以便space_comparer通过使用

之前的值来工作。我的问题是remove_copy_if和remove_copy_if。算法,

我认为所有算法都是使用副本。


我的意思是,假设这个电话:


remove_copy_if(cad1.begin(),cad1.end(),back_inserter(cad2),

space_comparer());


这是通过构建一个新的space_comparer仿函数对象和

使用其实例进行所有算法生命周期。我喜欢做什么

使用一个实际的实例作为谓词,这样它就可以在算法调用中保持一个

状态。


remove_copy_if(cad1.begin(),cad1.end(),back_inserter(cad2),

实例);


我想上面的调用将重用实例对象

仿函数,确实如此。但是它确实对象的临时副本

并且它可以使用它,而原始实例保持不变。上面代码的

输出是:


---------------------- -------------------------------------------

ctor()调用,lastChar是''X''

复制ctor()调用lastChar设置为''Y''

[Y | c]

[c | p]

[p | p]

复制ctor()调用lastChar设置为''Y''

[Y | c]

[c | p]

[p | p]

复制ctor()调用lastChar设置为''Y''

[Y | c]

[c | p]

[p | p]

---------------------------------------- -------------------------


但是,预期的输出必须是这样的:


---------------------------------------- -------------------------

ctor()调用,lastChar是''X''

[X | c]

[c | p]

[p | p]

[p | c]<< ---注意lastChar之前的状态

[c | p]

[p | p]

[p | c]<< ---注意lastChar之前的状态

[c | p]

[p | p]

---------------------------------------- -------------------------


我不知道这是否可能。我已经尝试过使用

" mem_func"和这样的技术,但无济于事......


拜托,我不是那么STL识字,所以请耐心等待。 :-)


干杯,

Hi folks!

First, this is the code I''m using to expose the problem:

------------------------------------------------------------------
#include <functional>
#include <string>
#include <iostream>

using namespace std;

struct space_comparer : public unary_function<char, bool{

mutable argument_type __lastChar;

space_comparer()
{
__lastChar = ''X'';
cout << "ctor() called and lastChar is ''" << __lastChar << "''" <<
endl;
}

space_comparer(const space_comparer &__src)
{
cout << "copy ctor() called setting lastChar to ''Y''" << endl;
__lastChar = ''Y'';
}

result_type operator ()(const argument_type c) const
{
cout << "[" << __lastChar << " | " << c << "]" << endl;
__lastChar = c;
return ::isspace(c);
}

};
main()
{
string cad1;
string cad2;

const space_comparer instance;

cad1 = "cpp";

remove_copy_if(cad1.begin(), cad1.end(), back_inserter(cad2),
instance );
remove_copy_if(cad1.begin(), cad1.end(), back_inserter(cad2),
instance );
remove_copy_if(cad1.begin(), cad1.end(), back_inserter(cad2),
instance );

return 0;
}
---------------------------------------------------------------

OK, so the meaning of all that is to use a unary function with a
persistent "state", so that the "space_comparer" works by using
previous values. My problem is that the "remove_copy_if" algorithm,
and I think all algorithms, use a copy.

I mean, suppose this call:

remove_copy_if(cad1.begin(), cad1.end(), back_inserter(cad2),
space_comparer() );

This works by constructing a new "space_comparer" functor object and
using its instance for all the algorithm lifetime. What I like to do
is use an actual instance as the predicate, so that it can mantain a
state across algorithm calls.

remove_copy_if(cad1.begin(), cad1.end(), back_inserter(cad2),
instance );

I supposed the above call would reuse the "instance" of the object
functor, which indeed does. But it does a temporary copy of the object
and it works with it, leaving the original instance untouched. The
output of the above code is:

-----------------------------------------------------------------
ctor() called and lastChar is ''X''
copy ctor() called setting lastChar to ''Y''
[Y | c]
[c | p]
[p | p]
copy ctor() called setting lastChar to ''Y''
[Y | c]
[c | p]
[p | p]
copy ctor() called setting lastChar to ''Y''
[Y | c]
[c | p]
[p | p]
-----------------------------------------------------------------

But, the expected output needs to be like this:

-----------------------------------------------------------------
ctor() called and lastChar is ''X''
[X | c]
[c | p]
[p | p]
[p | c] <<--- Note previous state of "lastChar"
[c | p]
[p | p]
[p | c] <<--- Note previous state of "lastChar"
[c | p]
[p | p]
-----------------------------------------------------------------

I don''t know if this is at all possible. I already tried with
"mem_func" and such techniques, but to no avail...

Please, I''m not so STL literate, so bear with me. :-)

Cheers,

推荐答案



" Dijkstra" < pe ***** @ gmail.comwrote in message

news:6b ************************* ********* @ k30g2000 hse.googlegroups.com ...

"Dijkstra" <pe*****@gmail.comwrote in message
news:6b**********************************@k30g2000 hse.googlegroups.com...

嗨伙计们!


首先,这是我用来揭露问题的代码:


---------------------- --------------------------------------------

#include< functional>

#include< string>

#include< iostream>


using namespace std;


struct space_comparer:public unary_function< char,bool {


mutable argument_type __lastChar;
Hi folks!

First, this is the code I''m using to expose the problem:

------------------------------------------------------------------
#include <functional>
#include <string>
#include <iostream>

using namespace std;

struct space_comparer : public unary_function<char, bool{

mutable argument_type __lastChar;



如果你需要一个,只有一个__lastChar(顺便说一句,选择不好的名字)

那么你应该让它静止。我不认为mutable会让它变得静止。


static argument_type lastChar_;


变量名称前面的下划线通常是后备的对于

编译器,这里有不同的规则,哪些是哪些,哪些不是,最好不要使用前面的下划线为你自己的变量。

If you need one, and only one __lastChar (a bad choice of names, by the way)
then you should make it static. I don''t think that mutable makes it static.

static argument_type lastChar_;

Underscores in the front of variable names are generally reerved for the
compiler,t here are different rules as to which are and which aren''t, it is
best to just never use preceeding underscore for you own variables.


space_comparer()

{

__lastChar =''X'';

cout<< " ctor()调用,lastChar是''" << __lastChar<< "" <<

endl;

}


space_comparer(const space_comparer& __ src)

{

cout<< copy ctor()将lastChar设置为''Y'' << endl;

__lastChar =''Y'';

}


result_type operator()(const argument_type c)const

{

cout<< " [" << __lastChar<< " | " << c<< "]" << endl;

__lastChar = c;

return :: isspace(c);

}


};


main()

{

string cad1;

string cad2;


const space_comparer实例;


cad1 =" cpp";


remove_copy_if(cad1。 begin(),cad1.end(),back_inserter(cad2),

实例);

remove_copy_if(cad1.begin(),cad1.end(),back_inserter( cad2),

实例);

remove_copy_if(cad1.begin(),cad1.end(),back_inserter(cad2),

实例);


返回0;

}

---------------- -----------------------------------------------


好​​的,所以所有这一切的意思是使用具有

持久性状态的一元函数,以便space_comparer等。通过使用

之前的值来工作。我的问题是remove_copy_if和remove_copy_if。算法,

我认为所有算法都是使用副本。


我的意思是,假设这个电话:


remove_copy_if(cad1.begin(),cad1.end(),back_inserter(cad2),

space_comparer());


这是通过构建一个新的space_comparer仿函数对象和

使用其实例进行所有算法生命周期。我喜欢做什么

使用一个实际的实例作为谓词,这样它就可以在算法调用中保持一个

状态。


remove_copy_if(cad1.begin(),cad1.end(),back_inserter(cad2),

实例);


我想上面的调用将重用实例对象

仿函数,确实如此。但是它确实对象的临时副本

并且它可以使用它,而原始实例保持不变。上面代码的

输出是:


---------------------- -------------------------------------------

ctor()调用,lastChar是''X''

复制ctor()调用lastChar设置为''Y''

[Y | c]

[c | p]

[p | p]

复制ctor()调用lastChar设置为''Y''

[Y | c]

[c | p]

[p | p]

复制ctor()调用lastChar设置为''Y''

[Y | c]

[c | p]

[p | p]

---------------------------------------- -------------------------


但是,预期的输出必须是这样的:


---------------------------------------- -------------------------

ctor()调用,lastChar是''X''

[X | c]

[c | p]

[p | p]

[p | c]<< ---注意lastChar之前的状态

[c | p]

[p | p]

[p | c]<< ---注意lastChar之前的状态

[c | p]

[p | p]

---------------------------------------- -------------------------


我不知道这是否可能。我已经尝试过使用

" mem_func"和这样的技术,但无济于事......


拜托,我不是那么STL识字,所以请耐心等待。 :-)


干杯,
space_comparer()
{
__lastChar = ''X'';
cout << "ctor() called and lastChar is ''" << __lastChar << "''" <<
endl;
}

space_comparer(const space_comparer &__src)
{
cout << "copy ctor() called setting lastChar to ''Y''" << endl;
__lastChar = ''Y'';
}

result_type operator ()(const argument_type c) const
{
cout << "[" << __lastChar << " | " << c << "]" << endl;
__lastChar = c;
return ::isspace(c);
}

};
main()
{
string cad1;
string cad2;

const space_comparer instance;

cad1 = "cpp";

remove_copy_if(cad1.begin(), cad1.end(), back_inserter(cad2),
instance );
remove_copy_if(cad1.begin(), cad1.end(), back_inserter(cad2),
instance );
remove_copy_if(cad1.begin(), cad1.end(), back_inserter(cad2),
instance );

return 0;
}
---------------------------------------------------------------

OK, so the meaning of all that is to use a unary function with a
persistent "state", so that the "space_comparer" works by using
previous values. My problem is that the "remove_copy_if" algorithm,
and I think all algorithms, use a copy.

I mean, suppose this call:

remove_copy_if(cad1.begin(), cad1.end(), back_inserter(cad2),
space_comparer() );

This works by constructing a new "space_comparer" functor object and
using its instance for all the algorithm lifetime. What I like to do
is use an actual instance as the predicate, so that it can mantain a
state across algorithm calls.

remove_copy_if(cad1.begin(), cad1.end(), back_inserter(cad2),
instance );

I supposed the above call would reuse the "instance" of the object
functor, which indeed does. But it does a temporary copy of the object
and it works with it, leaving the original instance untouched. The
output of the above code is:

-----------------------------------------------------------------
ctor() called and lastChar is ''X''
copy ctor() called setting lastChar to ''Y''
[Y | c]
[c | p]
[p | p]
copy ctor() called setting lastChar to ''Y''
[Y | c]
[c | p]
[p | p]
copy ctor() called setting lastChar to ''Y''
[Y | c]
[c | p]
[p | p]
-----------------------------------------------------------------

But, the expected output needs to be like this:

-----------------------------------------------------------------
ctor() called and lastChar is ''X''
[X | c]
[c | p]
[p | p]
[p | c] <<--- Note previous state of "lastChar"
[c | p]
[p | p]
[p | c] <<--- Note previous state of "lastChar"
[c | p]
[p | p]
-----------------------------------------------------------------

I don''t know if this is at all possible. I already tried with
"mem_func" and such techniques, but to no avail...

Please, I''m not so STL literate, so bear with me. :-)

Cheers,



Jim Langston写道:
Jim Langston wrote:

" Dijkstra" < pe ***** @ gmail.comwrote in message

news:6b ************************* ********* @ k30g2000 hse.googlegroups.com ...
"Dijkstra" <pe*****@gmail.comwrote in message
news:6b**********************************@k30g2000 hse.googlegroups.com...

>嗨大家好!

首先,这是我用来揭露问题的代码:

---------------------------- --------------------------------------
#include< functional>
#include< string>
#include< iostream>
使用命名空间std;

struct space_comparer:public unary_function< char,bool {

mutable argument_type __lastChar;
>Hi folks!

First, this is the code I''m using to expose the problem:

------------------------------------------------------------------
#include <functional>
#include <string>
#include <iostream>

using namespace std;

struct space_comparer : public unary_function<char, bool{

mutable argument_type __lastChar;



如果你需要一个,只有一个__lastChar(顺便说一句,选择不好的名字)

那么你应该让它静止。我不认为mutable会使它变得静止。


If you need one, and only one __lastChar (a bad choice of names, by the way)
then you should make it static. I don''t think that mutable makes it static.



在这种特殊情况下,代码很糟糕。根据ISO / IEC 14882:2003,包含双下划线的所有

标识符都保留给

实现(当使用标准库时 - 它是)。

In this particular case, the code is bad. Per ISO/IEC 14882:2003, all
identifiers containing a double underscore are reserved to the
implementation (when the standard library is used -- and it is).


7月8日,10:14 * pm,Jim Langston < tazmas ... @ rocketmail.comwrote:
On Jul 8, 10:14*pm, "Jim Langston" <tazmas...@rocketmail.comwrote:

" Dijkstra" < pepi ... @ gmail.com写信息


新闻:6b *********************** *********** @ k30g2000 hse.googlegroups.com ...
"Dijkstra" <pepi...@gmail.comwrote in message

news:6b**********************************@k30g2000 hse.googlegroups.com...

嗨大家好!
Hi folks!


首先,这是我用来揭露问题的代码:
First, this is the code I''m using to expose the problem:


-------------------------------------------- ----------------------

#include< functional>

#include< string>

#include< iostream>
------------------------------------------------------------------
#include <functional>
#include <string>
#include <iostream>


using namespace std;
using namespace std;


struct space_comparer:public unary_function< char,bool {
struct space_comparer : public unary_function<char, bool{


mutable argument_type * __ lastChar;
mutable argument_type *__lastChar;



如果你需要一个,只有一个__lastChar(顺便说一句,选择不好的名字)

那么你应该让它静止。 *我不认为mutable会让它变得静止。


static argument_type lastChar_;


变量名称前面的下划线通常是对于

编译器的支持,这里有不同的规则,哪些是哪些,哪些不是,最好是从不使用前面的下划线为你自己的变量。


If you need one, and only one __lastChar (a bad choice of names, by the way)
then you should make it static. *I don''t think that mutable makes it static.

static argument_type lastChar_;

Underscores in the front of variable names are generally reerved for the
compiler,t here are different rules as to which are and which aren''t, it is
best to just never use preceeding underscore for you own variables.


space_comparer()

{

__lastChar =''X'';

cout<< " ctor()调用,lastChar是''" << __lastChar<< "" <<

endl;

}
space_comparer()
{
__lastChar = ''X'';
cout << "ctor() called and lastChar is ''" << __lastChar << "''" <<
endl;
}


space_comparer(const space_comparer& __ src )

{

cout<< copy ctor()将lastChar设置为''Y'' << endl;

__lastChar =''Y'';

}
space_comparer(const space_comparer &__src)
{
cout << "copy ctor() called setting lastChar to ''Y''" << endl;
__lastChar = ''Y'';
}


result_type operator( )(const argument_type c)const

{

cout<< " [" << __lastChar<< " | " << c<< "]" << endl;

__lastChar = c;

return :: isspace(c);

}
result_type operator ()(const argument_type c) const
{
cout << "[" << __lastChar << " | " << c << "]" << endl;
__lastChar = c;
return ::isspace(c);
}


};
};


main()

{

string cad1;

string cad2;
main()
{
string cad1;
string cad2;


const space_comparer instance;
const space_comparer instance;


cad1 =" cpp" ;;
cad1 = "cpp";


remove_copy_if(cad1.begin(),cad1.end(),back_inserter(cad2),

instance);

remove_copy_if(cad1.begin(),cad1.end(),back_inserter(cad2),

实例);

remove_copy_if(cad1。 begin(),cad1.end(),back_inserter(cad2),

instance);
remove_copy_if(cad1.begin(), cad1.end(), back_inserter(cad2),
instance );
remove_copy_if(cad1.begin(), cad1.end(), back_inserter(cad2),
instance );
remove_copy_if(cad1.begin(), cad1.end(), back_inserter(cad2),
instance );


返回0;

}

---------- -------------------------------------------------- ---
return 0;
}
---------------------------------------------------------------


好​​的,所以所有的意思是使用带有

持久性的一元函数" space_comparer,space_comparer,space_comparer,通过使用

之前的值来工作。我的问题是remove_copy_if和remove_copy_if。算法,

我认为所有算法都使用副本。
OK, so the meaning of all that is to use a unary function with a
persistent "state", so that the "space_comparer" works by using
previous values. My problem is that the "remove_copy_if" algorithm,
and I think all algorithms, use a copy.


我的意思是,假设这个调用:
I mean, suppose this call:


remove_copy_if(cad1 .begin(),cad1.end(),back_inserter(cad2),

space_comparer());
remove_copy_if(cad1.begin(), cad1.end(), back_inserter(cad2),
space_comparer() );


这是通过构造一个新的space_comparer来实现的。仿函数对象和

使用其实例进行所有算法生命周期。我喜欢做什么

使用一个实际的实例作为谓词,这样它就可以在算法调用中保持一个

状态。
This works by constructing a new "space_comparer" functor object and
using its instance for all the algorithm lifetime. What I like to do
is use an actual instance as the predicate, so that it can mantain a
state across algorithm calls.


remove_copy_if(cad1.begin(),cad1.end(),back_inserter(cad2),

instance);
remove_copy_if(cad1.begin(), cad1.end(), back_inserter(cad2),
instance );


我认为上述调用将重用实例对象

仿函数,确实如此。但是它确实对象的临时副本

并且它可以使用它,而原始实例保持不变。上面代码的

输出是:
I supposed the above call would reuse the "instance" of the object
functor, which indeed does. But it does a temporary copy of the object
and it works with it, leaving the original instance untouched. The
output of the above code is:


-------------- -------------------------------------------------- -

ctor()调用和lastChar是''X''

复制ctor()调用lastChar设置为''Y''

[Y | c]

[c | p]

[p | p]

复制ctor()调用lastChar设置为''Y''

[Y | c]

[c | p]

[p | p]

复制ctor()调用lastChar设置为''Y''

[Y | c]

[c | p]

[p | p]

---------------------------------------- -------------------------
-----------------------------------------------------------------
ctor() called and lastChar is ''X''
copy ctor() called setting lastChar to ''Y''
[Y | c]
[c | p]
[p | p]
copy ctor() called setting lastChar to ''Y''
[Y | c]
[c | p]
[p | p]
copy ctor() called setting lastChar to ''Y''
[Y | c]
[c | p]
[p | p]
-----------------------------------------------------------------


但是,预期的输出需要像这样:
But, the expected output needs to be like this:


------------------------ -----------------------------------------

ctor ()调用和lastChar是''X''

[X | c]

[c | p]

[p | p]

[p | c] *<< ---注意lastChar之前的状态

[c | p]

[p | p]

[p | c] *<< ---注意lastChar之前的状态

[c | p]

[p | p]

---------------------------------------- -------------------------
-----------------------------------------------------------------
ctor() called and lastChar is ''X''
[X | c]
[c | p]
[p | p]
[p | c] * <<--- Note previous state of "lastChar"
[c | p]
[p | p]
[p | c] * <<--- Note previous state of "lastChar"
[c | p]
[p | p]
-----------------------------------------------------------------


我不是知道这是否可能。我已经尝试过使用

" mem_func"和这样的技术,但无济于事...
I don''t know if this is at all possible. I already tried with
"mem_func" and such techniques, but to no avail...


拜托,我不是那么STL识字,所以忍受我。 :-)
Please, I''m not so STL literate, so bear with me. :-)


干杯,
Cheers,




As据我所知。关键字mutuable意味着即使类/结构为常量,你也可以改变变量的值。


例如 -

const space_comparer s;

s .__ lastChar =''V''; //完全有效


现在说到这一点。正如Jim所说,您可以使用

静态成员来获得预期结果。谓词通常不是通过引用传递的
。这就是为什么每个人都要调用复制构造函数

时间。

As far as I know. The keyword "mutuable" implies that you may change
the value of the variable even though the class/struct is a const.

Eg -
const space_comparer s;
s.__lastChar = ''V''; //totally valid

Now getting to the point. As already stated by Jim you can use a
static member to get the expected results. Predicates are usually not
passed by reference. Thats why the copy constructor is called each
time.


这篇关于算法上使用复制构造函数的一元仿函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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