2010-06-28 39 views
0

关于下面的C++代码,Borland公司警告8092

LengthAlphabeticalSort lengthAlphabeticalSort; 
outputList.sort(lengthAlphabeticalSort); // causes borland 8092 error, guaranteed stable_sort. 

class LengthAlphabeticalSort 
{ 
    public: 
     bool operator() (std::string str1, std::string str2) 
     { 
      if(str1.length() < str2.length()) 
       return true; 
      else 
       return false; 
     } 
}; 

与Borland编译编译时,我得到一个警告不是 迭代器:在功能上剧名需要随机迭代器:: CommonWords(const的剧名 &)const的 涡轮增量链接5.69版权所有(C)1997-2005 Borland公司

谁能告诉我如何解决这个问题?它与VS2010和GNU干净地编译

+1

是什么类型'outputList'? – jalf 2010-06-28 13:41:20

+0

typedef std :: list StringList; 它是std :: list aCuria 2010-06-28 13:58:26

+0

您是否可以确认您标记的行肯定是警告消息中的行(worder.cpp,第138行)? – 2010-06-28 14:11:52

回答

1

那么,std::list上的sort成员函数并没有像你的那样采用二元函子,所以从查看你的发布代码,我会说你的编译器是错误的。

但是,错误消息您发布的困惑我:

random iterator required in function Worder::CommonWords(const Worder &) const 

为什么说一个随机迭代器在CommonWords要求?是CommonWordssort就是所谓的功能?

+0

是的,这种排序是在CommonWords中调用的。 – aCuria 2010-06-29 03:44:08

+0

我想它是一个编译器错误,可惜没人有更确定的答案。 – aCuria 2010-07-05 15:29:42

1

就个人而言,我觉得名字LengthAlphabeticalSort混淆,因为那种纯粹是长度,而不是由一些字母。此外,你应该参照给const将字符串传递,并且如果其他是多余的:

struct ByLength 
{ 
    bool operator()(const std::string& a, const std::string& b) 
    { 
     return a.length() < b.length(); 
    } 
}; 

最初的名单是保证按字母顺序排列。

这是否意味着你排序两次,第一次按字母顺序,然后按长度?我认为这是更好的做法是用适当的谓词只有一次排序:

struct FirstByLengthThenAlphabetical 
{ 
    bool operator()(const std::string& a, const std::string& b) 
    { 
     if (a.length() < b.length()) return true; 
     if (b.length() < a.length()) return false; 
     return a < b; 
    } 
}; 
+0

初始列表保证按字母顺序排列。因此,因为list.sort()保证是一个稳定的排序,所以按长度排序会给出一个按长度排序的排序,如果长度相同,按照字母顺序排列,因此命名约定。 通过引用和多余的if-else是我忽略的东西。 +1 – aCuria 2010-06-29 03:42:29

+0

@aCuria:我更新了我的帖子。 – fredoverflow 2010-06-29 07:42:05

+0

theres为什么它已经按字母顺序排列。我在两个集合上调用set_intersection并将结果输出到列表中。因此,名单总是按字母顺序排列。 – aCuria 2010-06-29 12:45:07

0

最近,我碰到这个问题来了,而旧版本的Borland下做一些汇编,以及(这个问题可以固定在较新版本)。我能够进行试验,并发现它似乎Borland公司通过函数名称进行查找,看是否你可能会调用一个STL算法。因此,任何与stl算法完全相同的名称都会检查是否将正确的迭代器类型传递给函数。

这很麻烦,因为排序是std::sort(RandomAccessIterator first, RandomAccessIterator last)算法,但排序也是std::list::sort(Compare comp)方法。不幸的是,这给我的印象是一个非常令人震惊的编译器错误,这是完全依赖于函数/方法的名称。

我不知道std :: list或其他容器的方法与被命名为与算法相同的方法是什么,但最好的解决方案是通过使用命令行参数来忽略该特定问题的警告。行选项:-w-8092