草庐IT

C++ 排序不适用于比较函数

coder 2024-02-17 原文

<分区>

我正在尝试使用 sort C++ 函数对存储在 vector 中的结构进行排序。我以前做过这个没有任何问题。但是我没有获得所需的输出。

如果我单独尝试比较函数它会起作用,但是一旦我使用排序 C++ 函数,比较函数似乎就不起作用了。

这个想法是对 int 数组进行排序,就好像它们是按升序排列的二进制数一样。我知道有不同的方法可以做到这一点,但我不明白为什么这不起作用。

预期的输出是:

0000000000
0101010101
0110110110

输出是:

0000000000
0110110110
0101010101

我不明白这里发生了什么。这是我的代码:

using namespace std;
int n;
struct solucion{
  int array[150];
};

vector <solucion> soluciones;

bool compare(solucion solucion1, solucion solucion2){
  for(int i=1;i<=n;i++)
    if(solucion2.array[i]>solucion1.array[i])
      return true;
  return false;
}

void print() {
  for(int i=0;i<soluciones.size();i++){
    for(int k=1;k<=n;k++)
      cout << soluciones.at(i).array[k];
    cout << endl;
  }
}

int main(int argc, char *argv[])
{
    solucion solucion1;
    solucion1.array[1]=0;
    solucion1.array[2]=0;
    solucion1.array[3]=0;
    solucion1.array[4]=0;
    solucion1.array[5]=0;
    solucion1.array[6]=0;
    solucion1.array[7]=0;
    solucion1.array[8]=0;
    solucion1.array[9]=0;
    solucion1.array[10]=0;

    solucion solucion2;
    solucion2.array[1]=0;
    solucion2.array[2]=1;
    solucion2.array[3]=0;
    solucion2.array[4]=1;
    solucion2.array[5]=0;
    solucion2.array[6]=1;
    solucion2.array[7]=0;
    solucion2.array[8]=1;
    solucion2.array[9]=0;
    solucion2.array[10]=1;

    solucion solucion3;
    solucion3.array[1]=0;
    solucion3.array[2]=1;
    solucion3.array[3]=1;
    solucion3.array[4]=0;
    solucion3.array[5]=1;
    solucion3.array[6]=1;
    solucion3.array[7]=0;
    solucion3.array[8]=1;
    solucion3.array[9]=1;
    solucion3.array[10]=0;

    soluciones.push_back(solucion1);
    soluciones.push_back(solucion2);
    soluciones.push_back(solucion3);

    n=10;
    sort(soluciones.begin(),soluciones.end(),compare);
    print();
    system("PAUSE");
    return EXIT_SUCCESS;
}

有关C++ 排序不适用于比较函数的更多相关文章

  1. ruby-on-rails - Rails 常用字符串(用于通知和错误信息等) - 2

    大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje

  2. ruby - Ruby 的 Hash 在比较键时使用哪种相等性测试? - 2

    我有一个围绕一些对象的包装类,我想将这些对象用作散列中的键。包装对象和解包装对象应映射到相同的键。一个简单的例子是这样的:classAattr_reader:xdefinitialize(inner)@inner=innerenddefx;@inner.x;enddef==(other)@inner.x==other.xendenda=A.new(o)#oisjustanyobjectthatallowso.xb=A.new(o)h={a=>5}ph[a]#5ph[b]#nil,shouldbe5ph[o]#nil,shouldbe5我试过==、===、eq?并散列所有无济于事。

  3. ruby-on-rails - 如何优雅地重启 thin + nginx? - 2

    我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server

  4. ruby - 在没有 sass 引擎的情况下使用 sass 颜色函数 - 2

    我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re

  5. ruby-on-rails - 在 ruby​​ 中使用 gsub 函数替换单词 - 2

    我正在尝试用ruby​​中的gsub函数替换字符串中的某些单词,但有时效果很好,在某些情况下会出现此错误?这种格式有什么问题吗NoMethodError(undefinedmethod`gsub!'fornil:NilClass):模型.rbclassTest"replacethisID1",WAY=>"replacethisID2andID3",DELTA=>"replacethisID4"}end另一个模型.rbclassCheck 最佳答案 啊,我找到了!gsub!是一个非常奇怪的方法。首先,它替换了字符串,所以它实际上修改了

  6. Ruby Sinatra 配置用于生产和开发 - 2

    我已经在Sinatra上创建了应用程序,它代表了一个简单的API。我想在生产和开发上进行部署。我想在部署时选择,是开发还是生产,一些方法的逻辑应该改变,这取决于部署类型。是否有任何想法,如何完成以及解决此问题的一些示例。例子:我有代码get'/api/test'doreturn"Itisdev"end但是在部署到生产环境之后我想在运行/api/test之后看到ItisPROD如何实现? 最佳答案 根据SinatraDocumentation:EnvironmentscanbesetthroughtheRACK_ENVenvironm

  7. ruby - 在 Ruby 中有条件地定义函数 - 2

    我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin

  8. ruby - inverse_of 是否适用于 has_many? - 2

    当我使用has_one时,它​​工作得很好,但在has_many上却不行。在这里您可以看到object_id不同,因为它运行了另一个SQL来再次获取它。ruby-1.9.2-p290:001>e=Employee.create(name:'rafael',active:false)ruby-1.9.2-p290:002>b=Badge.create(number:1,employee:e)ruby-1.9.2-p290:003>a=Address.create(street:"123MarketSt",city:"SanDiego",employee:e)ruby-1.9.2-p290

  9. ruby - 在 Ruby 中按名称传递函数 - 2

    如何在Ruby中按名称传递函数?(我使用Ruby才几个小时,所以我还在想办法。)nums=[1,2,3,4]#Thisworks,butismoreverbosethanI'dlikenums.eachdo|i|putsiend#InJS,Icouldjustdosomethinglike:#nums.forEach(console.log)#InF#,itwouldbesomethinglike:#List.iternums(printf"%A")#InRuby,IwishIcoulddosomethinglike:nums.eachputs在Ruby中能不能做到类似的简洁?我可以只

  10. ruby - 使用 `+=` 和 `send` 方法 - 2

    如何将send与+=一起使用?a=20;a.send"+=",10undefinedmethod`+='for20:Fixnuma=20;a+=10=>30 最佳答案 恐怕你不能。+=不是方法,而是语法糖。参见http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html它说Incommonwithmanyotherlanguages,Rubyhasasyntacticshortcut:a=a+2maybewrittenasa+=2.你能做的最好的事情是:

随机推荐