STL stable_sort排序问题 [英] STL stable_sort sorting issue

查看:64
本文介绍了STL stable_sort排序问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello C ++ Guru,

我使用STL stable_sort来使用代码下面的
对矢量字符串数据进行排序。


#include< iostream>

#include< vector>

#include< string>

#include< algorithm>

使用命名空间std;


bool my_comparison(string p1,string p2)

{

//使第一个字符串小写

string :: iterator StringValueIterator = p1.begin();

transform(p1.begin(),p1.end(),StringValueIterator,toupper );


//使第二个字符串小写

StringValueIterator = p2.begin();

transform(p2.begin) (),p2.end(),StringValueIterator,toupper);


返回p1< p2;

}


int main()

{

typedef vector< stringholder;

持有人some_holder;

some_holder.push_back(" aaa");

some_holder.push_back(" bbb");

some_holder.push_back(" AAA");

some_holder.push_back(" BBB");


stable_sort(some_holder.begin( ),some_holder.end(),my_comparison);

holder :: iterator VectIt = some_holder.begin();

for(; VectIt!= some_holder.end() ; ++ VectIt)

{

cout<< * VectIt<< ''\ n'';

}

返回0;

}


我得到以下结果:

aaa

AAA

bbb

BBB


当我期待如下结果时:。


AAA

aaa

BBB

bbb


为什么它优先考虑第一个小案例为什么不上面

来信?

有人可以帮助我修复代码中的错误。

感谢任何帮助或建议。

-

[见 http://www.gotw.ca/resources/clcm.htm 有关的信息]

[comp.lang.c ++。moderated。第一次海报:做到这一点! ]

Hello C++ Guru,
I am using STL stable_sort to sort the vector string data using
below code.

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

bool my_comparison( string p1, string p2)
{
//Make first string lower case
string::iterator StringValueIterator = p1.begin();
transform (p1.begin(),p1.end(), StringValueIterator,toupper);

//Make second string lower case
StringValueIterator = p2.begin();
transform (p2.begin(),p2.end(), StringValueIterator,toupper);

return p1 < p2;
}

int main()
{
typedef vector<stringholder;
holder some_holder;
some_holder.push_back("aaa");
some_holder.push_back("bbb");
some_holder.push_back("AAA");
some_holder.push_back("BBB");

stable_sort(some_holder.begin(),some_holder.end(), my_comparison);
holder::iterator VectIt = some_holder.begin();
for( ; VectIt!=some_holder.end(); ++VectIt)
{
cout <<*VectIt<< ''\n'';
}
return 0;
}

I got below result :
aaa
AAA
bbb
BBB

When I am expecting result like below :.

AAA
aaa
BBB
bbb

Why it''s giving preference to first small case why not upper
letter ?
Can somebody help me in fixing the bug in code.
Any help or suggestion is appreciated.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

推荐答案

5月29日下午12:45,沙滩< sandip.w ... @ gmail.comwrote:
On May 29, 12:45 pm, sandy <sandip.w...@gmail.comwrote:

Hello C ++ Guru,

我使用STL stable_sort在代码下面使用

对矢量字符串数据进行排序。


#include< iostream>

#include< vector>

#include< string>

#include< algorithm>

using namespace std;


bool my_comparison(string p1,string p2)

{

//使第一个字符串小写

string :: iterator StringValueIterator = p1.begin();

transform(p1.begin) (),p1.end(),StringValueIterator,toupper);


//使第二个字符串小写

StringValueIterator = p2.begin();

transform(p2.begin(),p2.end(),StringValueIterator,toupper);


返回p1< p2;


}


int main()

{

typedef vector< ; stringholder;

持有人some_holder;

some_holder.push_back(" aaa");

some_holder.push_back(" bbb");

some_holder.push_back(" AAA");

some_holder.push_back(" BBB");


stable_sort( some_holder.begin(),some_holder.end(),my_comparison);

holder :: iterator VectIt = some_holder.begin();

for(; VectIt!= some_holder .end(); ++ VectIt)

{

cout<< * VectIt<< ''\ n'';

}

返回0;


}


我得到以下结果:

aaa

AAA

bbb

BBB


当我期待如下结果时:。


AAA

aaa

BBB

bbb


为什么它优先考虑第一个小案例为什么不上限

来信?

可以有人帮我修复代码中的错误。

任何帮助或建议都表示赞赏。
Hello C++ Guru,
I am using STL stable_sort to sort the vector string data using
below code.

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

bool my_comparison( string p1, string p2)
{
//Make first string lower case
string::iterator StringValueIterator = p1.begin();
transform (p1.begin(),p1.end(), StringValueIterator,toupper);

//Make second string lower case
StringValueIterator = p2.begin();
transform (p2.begin(),p2.end(), StringValueIterator,toupper);

return p1 < p2;

}

int main()
{
typedef vector<stringholder;
holder some_holder;
some_holder.push_back("aaa");
some_holder.push_back("bbb");
some_holder.push_back("AAA");
some_holder.push_back("BBB");

stable_sort(some_holder.begin(),some_holder.end(), my_comparison);
holder::iterator VectIt = some_holder.begin();
for( ; VectIt!=some_holder.end(); ++VectIt)
{
cout <<*VectIt<< ''\n'';
}
return 0;

}

I got below result :
aaa
AAA
bbb
BBB

When I am expecting result like below :.

AAA
aaa
BBB
bbb

Why it''s giving preference to first small case why not upper
letter ?
Can somebody help me in fixing the bug in code.
Any help or suggestion is appreciated.



您正在进行不区分大小写的搜索。因此,aaa和AAA

基本上相等。因此,通常您将无法确定将这两个项目放入容器中的顺序

。但是,我们对此有一个警告。因为你使用了stable_sort(),所以相等的元素将保持原始容器中相同的相对顺序

(参见 www.sgi.com/tech/stl/stable_sort.html

的确切定义)。所以输出是预期的。

另外两个注释:

===================== =========

这个比较函数:

bool my_comparison(string p1,string p2)


在调用此函数之前,每个参数的副本都会生成(每个参数调用时间为
)。通过

const引用传递参数可能更有效。


bool my_comparison(字符串& const p1,字符串& const p2)


如果更改定义,则不允许直接修改参数,因此您需要相应地更改

算法。


希望这有帮助

Martin。

-

[见 http://www.gotw.ca/resources/clcm.htm 了解有关的信息]

[comp.lang.c ++。版主。第一次海报:做到这一点! ]

You are doing a case insensitive search. Thus "aaa" and "AAA" are
essentially equal. Thus normally you would not be able to determine
the order that these two items would be placed in the container. BUT
we have a caveat to that. Because you used stable_sort() any elements
that are equal will remain in the same relative order that they were
in the original container (see www.sgi.com/tech/stl/stable_sort.html
for the exact definition). So the output is as expected.
A couple of additional notes:
==============================
This comparison function:
bool my_comparison( string p1, string p2)

A copy of each parameter is made before this function is called (every
time it is called). It may be more efficient to pass the parameters by
const reference.

bool my_comparison(string& const p1,string& const p2)

If you change the definition you will not be allowed to directly
modify the parameters and thus you will be required to change your
algorithm accordingly.

Hope this helps
Martin.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


结果是一致的,因为当您将大写(aaa)与

AAA进行比较时,它将返回false(比较少运营商。


我找到的解决方案(不太好)可能是这样的:


在转换前保存副本:

字符串P1 = p1;

字符串P2 = p2;


然后返回:


返回(p1 == p2)? (P1< P2):( p1< p2);


-haro

sandy写道:
The result is consitant, because when you compare UPPERCASE (aaa) with
AAA, it will return false (comparing with less operator).

The solution (not so nice) I found, could be this:

Save the copies before transform:
string P1 = p1;
string P2 = p2;

and then return this:

return (p1 == p2) ? (P1 < P2) : (p1 < p2);

-haro
sandy wrote:

Hello C ++ Guru,

我使用STL stable_sort来使用

对代码进行排序。


#include< iostream>

#include< vector>

#include< string>

#include< algorithm>

使用命名空间std;


bool my_comparison(string p1,string p2)

{

//使第一个字符串小写

string :: iterator StringValueIterator = p1.begin();

transform(p1.begin(),p1.end(),StringValueIterator ,toupper);


//使第二个字符串小写

StringValueIterator = p2.begin();

transform(p2 .begin(),p2.end(),StringValueIterator,toupper);


返回p1< p2;

}


int main()

{

typedef vector< stringholder;

持有人some_holder;

some_holder.push_back(" aaa");

some_holder.push_back(" bbb");

some_holder.push_back(" AAA");

some_holder.push_back(" BBB");


stable_sort(some_holder.begin( ),some_holder.end(),my_comparison);

holder :: iterator VectIt = some_holder.begin();

for(; VectIt!= some_holder.end() ; ++ VectIt)

{

cout<< * VectIt<< ''\ n'';

}

返回0;

}


我得到以下结果:

aaa

AAA

bbb

BBB


当我期待如下结果时:。


AAA

aaa

BBB

bbb


为什么它优先考虑第一个小案例为什么不上面

来信?

有人可以帮助我修复代码中的错误。

感谢任何帮助或建议。

Hello C++ Guru,
I am using STL stable_sort to sort the vector string data using
below code.

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

bool my_comparison( string p1, string p2)
{
//Make first string lower case
string::iterator StringValueIterator = p1.begin();
transform (p1.begin(),p1.end(), StringValueIterator,toupper);

//Make second string lower case
StringValueIterator = p2.begin();
transform (p2.begin(),p2.end(), StringValueIterator,toupper);

return p1 < p2;
}

int main()
{
typedef vector<stringholder;
holder some_holder;
some_holder.push_back("aaa");
some_holder.push_back("bbb");
some_holder.push_back("AAA");
some_holder.push_back("BBB");

stable_sort(some_holder.begin(),some_holder.end(), my_comparison);
holder::iterator VectIt = some_holder.begin();
for( ; VectIt!=some_holder.end(); ++VectIt)
{
cout <<*VectIt<< ''\n'';
}
return 0;
}

I got below result :
aaa
AAA
bbb
BBB

When I am expecting result like below :.

AAA
aaa
BBB
bbb

Why it''s giving preference to first small case why not upper
letter ?
Can somebody help me in fixing the bug in code.
Any help or suggestion is appreciated.



-

[见 http://www.gotw.ca/resources/clcm.htm 有关的信息]

[comp.lang.c ++。审核。第一次海报:做到这一点! ]

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


文章< 11 ********************** @ k79g2000hse。 googlegroups .com>,

sandy< sa ********* @ gmail.comwrote:
In article <11**********************@k79g2000hse.googlegroups .com>,
sandy <sa*********@gmail.comwrote:

Hello C ++ Guru ,

我使用STL stable_sort使用

对代码进行排序。


#include< iostream>

#include< vector>

#include< string>

#include< algorithm>

using namespace std;


bool my_comparison(string p1,string p2)

{

//制作第一个字符串小写

string :: iterator StringValueIterator = p1.begin();

transform(p1.begin(),p1.end(),StringValueIterator,toupper);


//使第二个字符串小写

StringValueIterator = p2.begin();

transform(p2.begin(),p2.end (),StringValueIterator,toupper);


返回p1< p2;

}


int main()

{

typedef vector< stringholder;

持有人some_holder;

some_holder.push_back(" aaa");

some_holder.push_back(" bbb");

some_holder.push_back(" AAA");

some_holder.push_back(" BBB");


stable_sort(some_holder.begin( ),some_holder.end(),my_comparison);

holder :: iterator VectIt = some_holder.begin();

for(; VectIt!= some_holder.end() ; ++ VectIt)

{

cout<< * VectIt<< ''\ n'';

}

返回0;

}


我得到以下结果:

aaa

AAA

bbb

BBB


当我期待如下结果时:。


AAA

aaa

BBB

bbb


为什么它优先考虑第一个小盒子为什么不上面

字母?
Hello C++ Guru,
I am using STL stable_sort to sort the vector string data using
below code.

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

bool my_comparison( string p1, string p2)
{
//Make first string lower case
string::iterator StringValueIterator = p1.begin();
transform (p1.begin(),p1.end(), StringValueIterator,toupper);

//Make second string lower case
StringValueIterator = p2.begin();
transform (p2.begin(),p2.end(), StringValueIterator,toupper);

return p1 < p2;
}

int main()
{
typedef vector<stringholder;
holder some_holder;
some_holder.push_back("aaa");
some_holder.push_back("bbb");
some_holder.push_back("AAA");
some_holder.push_back("BBB");

stable_sort(some_holder.begin(),some_holder.end(), my_comparison);
holder::iterator VectIt = some_holder.begin();
for( ; VectIt!=some_holder.end(); ++VectIt)
{
cout <<*VectIt<< ''\n'';
}
return 0;
}

I got below result :
aaa
AAA
bbb
BBB

When I am expecting result like below :.

AAA
aaa
BBB
bbb

Why it''s giving preference to first small case why not upper
letter ?



因为它们首先放在你的容器里。 " stable_sort"尝试

尽可能保持物品的现有顺序。由于您的代码

表示aaa和AAA是平等的,它不会交换它们。


-

[见 http://www.gotw.ca/resources/clcm.htm 有关的信息]

[comp .lang.c ++。主持。第一次海报:做到这一点! ]

Because they are put in your container first. "stable_sort" tries to
maintain the existing order of the items as best it can. Since your code
says that "aaa" and "AAA" are equal, it doesn''t swap them.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


这篇关于STL stable_sort排序问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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