STL conatainer具有比较功能,标准是什么? [英] STL conatainer with a comparison function, what is the standard?

查看:64
本文介绍了STL conatainer具有比较功能,标准是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码在.Net 2001(?)... VC ++ 7.0上构建得很好。但是在MinGW和MS VC ++ 6.0中使用

gcc 3.42则没有。我能理解VC ++

无法正常工作,但还不是gcc标准吗?这是代码,从我搜索周围的东西可以看出,第三项中的函数在某些编译器中是好的,但不是其他编译器。任何人都可以推荐一个免费的兼容的

编译器,是Open Watcom吗?


vector< CIniFile :: Record>内容; //用于保存已排序的内容

vector< CIniFile :: Record> sections = GetSections(FileName); //获取一个

的章节列表

if(!sections.empty())//有什么需要处理的吗?

{

if(降序)// Descending或Ascending?

std :: sort(sections.begin(),sections.end(),

DescendingSectionSort());

else //对章节进行排序

std :: sort(sections.begin(),sections.end(),

AcendingSectionSort());

.....

}

I have some code that builds fine on .Net 2001(?).. VC++ 7.0. But with
gcc 3.42 in MinGW and MS VC++ 6.0 it does not. I can understand VC++
not working, but isn''t gcc standard yet? Here is the code, from what I
can tell from searching around, the function in the third term is ok in
some compilers, but not others. Can anyone recommend a free compliant
compiler, is Open Watcom?

vector<CIniFile::Record> content; // Used to hold the sorted content
vector<CIniFile::Record> sections = GetSections(FileName); // Get a
list of Sections
if(!sections.empty()) // Is there anything to process?
{
if(Descending) // Descending or Ascending?
std::sort(sections.begin(), sections.end(),
DescendingSectionSort());
else // Sort the Sections
std::sort(sections.begin(), sections.end(),
AcendingSectionSort());
.....
}

推荐答案

没有看到DescendingSectionSort的声明和

AcendingSectionSort ......不知道可能出现什么问题。

Without seeing the declarations for DescendingSectionSort and
AcendingSectionSort... No idea what may be wrong.


< a href =mailto:tj ***** @ gmail.com> tj ***** @ gmail.com 写道:
我有一些代码可以很好地构建.Net 2001(?).. VC ++ 7.0。但是在MinGW和MS VC ++ 6.0中使用
gcc 3.42则没有。我能理解VC ++不工作,但是不是gcc标准吗?这是代码,从我搜索到的内容可以看出,第三项中的函数在某些编译器中是可以的,但在其他编译器中则没有。任何人都可以推荐一个免费的兼容
编译器,是Open Watcom吗?

vector< CIniFile :: Record>内容; //用于保存已排序的内容
向量< CIniFile :: Record> sections = GetSections(FileName); //获取章节列表
if(!sections.empty())//有什么需要处理的吗?
{
if(降序)//降序或升序?
std :: sort(sections.begin(),sections.end(),
DescendingSectionSort());
//对章节进行排序
std :: sort( sections.begin(),sections.end(),
AcendingSectionSort());
....
}
I have some code that builds fine on .Net 2001(?).. VC++ 7.0. But with
gcc 3.42 in MinGW and MS VC++ 6.0 it does not. I can understand VC++
not working, but isn''t gcc standard yet? Here is the code, from what I
can tell from searching around, the function in the third term is ok in
some compilers, but not others. Can anyone recommend a free compliant
compiler, is Open Watcom?

vector<CIniFile::Record> content; // Used to hold the sorted content
vector<CIniFile::Record> sections = GetSections(FileName); // Get a
list of Sections
if(!sections.empty()) // Is there anything to process?
{
if(Descending) // Descending or Ascending?
std::sort(sections.begin(), sections.end(),
DescendingSectionSort());
else // Sort the Sections
std::sort(sections.begin(), sections.end(),
AcendingSectionSort());
....
}




嗯,''gcc''(你实际上使用''g ++'',而不是''gcc''来编译C ++编程)

符合标准。第3个arg到''sort''必须是一个函数

本身需要2个参数。你的''排序''陈述是不正确的。

这里是从''排序''的文件剪辑:


< quote>

排序


模板< class RanIt>

void sort(RanIt first,RanIt last);


模板< class RanIt,类Pred>

void sort(RanIt first,RanIt last,Pred pr);


第一个模板函数在[first,last]范围内重新排序由

迭代器指定的序列,以形成一个由运算符<>命令的序列

。因此,元素按升序排序。


函数计算排序谓词X< Y最多

ceil((最后 - 第一个)* log(最后 - 第一个))次。


第二个模板函数的行为相同,除了它

用pr(X,Y)替换运算符<(X,Y)

< / quote>


你正在使用''第二个模板''功能;那么,你的

DescendingSectionSort()和AscendingSectionSort()必须

每个需要2个类型为CIniFile :: Record(或者ref为')。


某处应该是这样的:


bool DescendingSectionSort(const CIniFile :: Record& X,

const CIniFile: :Record& Y)

{

//比较这里的代码

}


同样的AscendingSectionSort()的代码类型在这里...


然后你的排序语句如下所示:


std :: sort( sections.begin(),sections.end(),

DescendingSectionSort);


这里有一个完整的简单例子:

#include< iostream>

#include< vector>


struct Stuff

{

int a;

double b;


Stuff(int ia,double fb):a(ia),b(fb) {}

};


bool DescStuff(const Stuff& s1,const Stuff& s2)

{

返回s1.a> s2.a;

}


int main()

{

std :: vector< ;东西>东西;


stuff.push_back(东西(1,1.0));

stuff.push_back(Stuff(2,2.0));

stuff.push_back(Stuff(3,3.0));


//在''a'上按降序排序''stuff''

std :: sort(stuff.begin(),stuff.end(),DescStuff);


std :: vector< Stuff> :: iterator it;


//打印排序的''东西''

for(it = stuff.begin(); it!= stuff.end(); it ++)

std :: cout<< (* it).a<< std :: endl;


返回0;

}


问候,

Larry



Hmm, ''gcc'' (you actually use ''g++'', not ''gcc'', to compile C++ progs)
is standards compliant. The 3rd arg to ''sort'' must be a function
that itself takes 2 args. Your ''sort'' statements are incorrect.
Here''s a snip from the docs for ''sort'':

<quote>
sort

template<class RanIt>
void sort(RanIt first, RanIt last);

template<class RanIt, class Pred>
void sort(RanIt first, RanIt last, Pred pr);

The first template function reorders the sequence designated by
iterators in the range [first, last) to form a sequence ordered
by operator<. Thus, the elements are sorted in ascending order.

The function evaluates the ordering predicate X < Y at most
ceil((last - first) * log(last - first)) times.

The second template function behaves the same, except that it
replaces operator<(X, Y) with pr(X, Y)
</quote>

You are using the ''second template'' function; so, your
DescendingSectionSort() and AscendingSectionSort() must
each take 2 args of type CIniFile::Record (or ref''s to same).

Somewhere should be something like this:

bool DescendingSectionSort(const CIniFile::Record& X,
const CIniFile::Record& Y)
{
// compare code here
}

The same type of code for AscendingSectionSort() goes here...

Then your sort statements would look like this:

std::sort(sections.begin(), sections.end(),
DescendingSectionSort);

Here''s a complete simple example:

#include <iostream>
#include <vector>

struct Stuff
{
int a;
double b;

Stuff(int ia, double fb) : a(ia), b(fb) {}
};

bool DescStuff(const Stuff& s1, const Stuff& s2)
{
return s1.a > s2.a;
}

int main()
{
std::vector<Stuff> stuff;

stuff.push_back(Stuff(1, 1.0));
stuff.push_back(Stuff(2, 2.0));
stuff.push_back(Stuff(3, 3.0));

// sort ''stuff'' into descending order on ''a''
std::sort(stuff.begin(), stuff.end(), DescStuff);

std::vector<Stuff>::iterator it;

// print the sorted ''stuff''
for (it = stuff.begin(); it != stuff.end(); it++)
std::cout << (*it).a << std::endl;

return 0;
}

Regards,
Larry


tj*****@gmail.com 写道:
我有一些代码在.Net 2001(?).. VC ++ 7.0上构建良好。但是在MinGW和MS VC ++ 6.0中使用
gcc 3.42则没有。我能理解VC ++不工作,但是不是gcc标准吗?这是代码,从我搜索到的内容可以看出,第三项中的函数在某些编译器中是可以的,但在其他编译器中则没有。任何人都可以推荐一个免费的兼容
编译器,是Open Watcom吗?

vector< CIniFile :: Record>内容; //用于保存已排序的内容
向量< CIniFile :: Record> sections = GetSections(FileName); //获取章节列表
if(!sections.empty())//有什么需要处理的吗?
{
if(降序)//降序或升序?
std :: sort(sections.begin(),sections.end(),
DescendingSectionSort());
//对章节进行排序
std :: sort( sections.begin(),sections.end(),
AcendingSectionSort());
....
}
I have some code that builds fine on .Net 2001(?).. VC++ 7.0. But with
gcc 3.42 in MinGW and MS VC++ 6.0 it does not. I can understand VC++
not working, but isn''t gcc standard yet? Here is the code, from what I
can tell from searching around, the function in the third term is ok in
some compilers, but not others. Can anyone recommend a free compliant
compiler, is Open Watcom?

vector<CIniFile::Record> content; // Used to hold the sorted content
vector<CIniFile::Record> sections = GetSections(FileName); // Get a
list of Sections
if(!sections.empty()) // Is there anything to process?
{
if(Descending) // Descending or Ascending?
std::sort(sections.begin(), sections.end(),
DescendingSectionSort());
else // Sort the Sections
std::sort(sections.begin(), sections.end(),
AcendingSectionSort());
....
}




正如我在之前的帖子中所说,你的sort

语句的语法是不正确的。


你有std ::的方式排序"使用

" DescendingSectionSort()"和AscendingSectionSort(),

应该产生编译时错误。应该没有

是任何()以下DescendingSectionSort和

AscendingSectionSort在std :: sort中声明。


代码应为:


if(降序)// Descending或Ascending?

std :: sort(sections.begin(),sections.end(),

DescendingSectionSort);

else //对章节进行排序

std :: sort(sections.begin(),sections.end(),

AcendingSectionSort);


所以,它是.Net 2001(? ).. VC ++ 7.0"这是错误的(非标准),

和VC ++ 6.0和GCC是正确的(标准)。


问候,

拉里



As I said in my earlier post, the syntax of your "sort"
statements is incorrect.

The way you have the "std::sort" statements written, with
"DescendingSectionSort()" and "AscendingSectionSort()",
should produce a compile-time error. There should not
be any "()" following "DescendingSectionSort" and
"AscendingSectionSort" in the "std::sort" statements.

The code should be:

if(Descending) // Descending or Ascending?
std::sort(sections.begin(), sections.end(),
DescendingSectionSort );
else // Sort the Sections
std::sort(sections.begin(), sections.end(),
AcendingSectionSort );

So, it is ".Net 2001(?).. VC++ 7.0" that is wrong (non-Standard),
and VC++ 6.0 and GCC which are correct (Standard).

Regards,
Larry


这篇关于STL conatainer具有比较功能,标准是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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