STL conatainer具有比较功能,标准是什么?

编程入门 行业动态 更新时间:2024-10-20 16:36:15
本文介绍了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.

tj ***** @ gmail 写道: 我有一些代码可以很好地构建.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 写道: 我有一些代码在.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具有比较功能,标准是什么?

本文发布于:2023-10-09 06:08:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1474909.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:功能   标准   STL   conatainer

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!