首页 \ 问答 \ 向量对通过对元素的差异进行排序(vector pair sorting by the difference of the pair elements)

向量对通过对元素的差异进行排序(vector pair sorting by the difference of the pair elements)

在C ++中是否有任何方法,它会根据对值的差异对我进行一对向量排序。 举个例子,假设我有4对

1 3, 
5 6, 
2 3,
12 5,

所以,对的差异是2 1 1 7,如果我按降序排序排序的向量将是,

12 5,
1 3,
5 6,
2 3,

我希望你明白我的问题是什么。 有什么方法可以用这种方法对元素进行排序吗?

我试过这种方法来根据第一个或第二个元素对元素进行排序。 但这不是我的问题。 我的问题是我需要根据差异进行排序。

bool sortinrev(const pair<int,int> &a, const pair<int,int> &b){
    return(a.first > b.first) ;
}


int main()
{
    vector< pair <int,int> > pq;
    for(int i=1; i<=4; i++){
        int x,y;
        cin >> x >> y;

        pq.push_back(make_pair(x,y));
    }

    sort(pq.begin(), pq.end(), sortinrev);

    for(int i=0; i<4; i++){
        cout << pq[i].first << " " << pq[i].second << endl;
    }


    return 0;
}

Is there any way in C++, That will sort me a pairs of vector based on the difference of the pair values. As an example, Suppose I have 4 pairs

1 3, 
5 6, 
2 3,
12 5,

so, the differences of the pairs are 2 1 1 7, if I sort in descending order The sorted vector would be,

12 5,
1 3,
5 6,
2 3,

I hope you understood what my problem is. Is there any way to sort the elements this way?

I have tried this way to sort elements based on first or second element. But this is not my problem. My problem is I need sort that based on the difference.

bool sortinrev(const pair<int,int> &a, const pair<int,int> &b){
    return(a.first > b.first) ;
}


int main()
{
    vector< pair <int,int> > pq;
    for(int i=1; i<=4; i++){
        int x,y;
        cin >> x >> y;

        pq.push_back(make_pair(x,y));
    }

    sort(pq.begin(), pq.end(), sortinrev);

    for(int i=0; i<4; i++){
        cout << pq[i].first << " " << pq[i].second << endl;
    }


    return 0;
}

原文:https://stackoverflow.com/questions/45677860
更新时间:2021-08-29 18:08

最满意答案

优点:

CppUnit的主要优点是它遵循Setup / Test / Teardown的XUnit模式。 具有XUnit经验的任何人都能理解这种范例。 必须使用宏而不是属性或反射(就像使用.Net或Java XUnit框架一样)是一种解决方法,但不是真的很麻烦。

而CppUnit本身就是可移植的。 虽然TestRunner.DLL GUI完全是在MFC中构建的,因此只是Windows,但命令行版本不使用Microsoft特定代码,并且在许多平台上执行。

它也很稳定。 开发人员多年来几乎没有做任何事情,主要是因为它的核心不需要任何东西。 它做得很好。

缺点:

遗憾的是,缺点是缺乏最终用户支持。 我最大的抱怨是没有好的工具可以将CppUnit集成到IDE中。 我认为最严重的原因是测试需要纪律。 任何使单元测试比它必须更难的东西都将被用作不做的借口。

您可以手动添加格式以输出Visual Studio将正确解释的测试失败行(因此双击它将带您进入测试中失败的ASSERTion。)但是这不附带该工具,您必须学习它在外部像这样的网站上。

您可以编写一个IDE插件,为您生成单元测试框架代码,但我知道没有这样的插件可以公开使用。 同样,这对开发人员来说也很容易。

由于没有集成,因此也没有关于将CppUnit项目合并到代码库中的已发布指南。 这使得入门非常困难。

如果您确实设法使其正常工作,则有几个人会报告幻像CppUnit项目签入/签出问题。

CppUnit不支持任何特定的模拟框架。 由于大多数模拟框架使用反射来使开发模拟无痛,因此C ++总是会受到影响。

备择方案:

那里有许多很多C ++单元测试框架。 Googletest从某些人那里获得了很好的评价。 CppUnitLite受到一些人的赞扬,但我发现它非常令人困惑和无益。 CxxUnit需要一个笨拙的Python扫描和解析步骤 - 对于不太文明的年龄来说,这是一种不那么优雅的武器。 在这个领域有许多商业人士提供他们的商品。

我推荐http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C.2B.2B获取非常丰富的备选方案列表。

虽然我还没有机会使用它,但我知道Visual Studio 2012已经整合了Microsoft自己的测试套件工具,并使其可用于本机C ++代码。 以前,它只能通过.Net集成工作,这对大多数C ++代码来说都是无用的。 我真的很期待拥有一整套工具,包括他们的代码覆盖集成,它突出了您测试的代码,显示您的测试覆盖范围的差距。

一句话:如果我今天重新开始,我会选择一个与XUnit兼容的不同测试框架,一个与IDE完全集成的测试框架。 当我们选择CppUnit时,它是Visual Studio 6.0中城里最好的游戏,但现在我们只是和它一起生活。


Advantages:

The primary advantage of CppUnit is that it follows the XUnit patterns of Setup / Test / Teardown. The paradigm is understood by anyone with XUnit experience. Having to use macros instead of attributes or reflection (as you do with .Net or Java XUnit frameworks) is a bit of a workaround, but not really burdensome.

And CppUnit itself is portable. Although the TestRunner.DLL GUI is built entirely in MFC and is therefore Windows only, the command line version uses no Microsoft specific code, and executes on many platforms.

It's also very stable. The developer has done almost nothing with it for many years, mostly because its core needs nothing. It does its one thing very well.

Disadvantages:

The disadvantages are unfortunately that end user support is lacking. My biggest complaint is that there are no good tools that integrate CppUnit into an IDE. The reason I consider this most severe is that testing requires discipline. Anything that makes unit testing harder than it has to be will be used as an excuse to not do it.

You can manually add a format to output a test failure line that Visual Studio will properly interpret (so double clicking on it will take you to the failing ASSERTion in the test.) But this doesn't ship with the tool, you have to learn it externally on sites like this one.

You can write an IDE plugin that will generate unit test skeleton code for you, but I am aware of no such plugins being made publicly available. Again, this is something that needs to be easy for developers.

Because there is no integration, there is also no published guidance on incorporating a CppUnit project into your codebase. That makes it very hard to get started.

If you do manage to get it working, several people report problems with phantom CppUnit project check-in / check-out problems.

CppUnit doesn't support any particular mocking framework. As most mock frameworks use reflection to make developing mocks painless, C++ is always going to suffer a bit there.

Alternatives:

There are many, many C++ unit testing frameworks out there. Googletest gets good reviews from some people. CppUnitLite is praised by some, but I found it very confusing and unhelpful. CxxUnit requires a clumsy Python scanning and parsing step - a less elegant weapon for a less civilized age. And there are many commercial players in this space, offering their wares.

I'd recommend http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C.2B.2B for a very rich list of alternatives.

While I have not yet had the chance to use it, I understand that Visual Studio 2012 has incorporated Microsoft's own test suite tooling and made it available for native C++ code. Previously, it only worked through .Net integration, which was useless for most C++ code. I'm really looking forward to having a full suite of tools, including their code coverage integration, where it highlights your tested code, showing you gaps in your test coverage.

Bottom line: if I were starting over today, I would choose a different XUnit-compatible test framework, one that was fully integrated with the IDE. Back when we chose CppUnit, it was the best game in town for Visual Studio 6.0, but now we're just living with it.

相关问答

更多
  • 这主要取决于观众和目的。 在建模语言方面,BPMN和UML活动图涵盖了几乎相同的具有不同符号的概念空间。 这个符号很快就会变得宗教信仰。 我个人比AD BP更喜欢AD标记 - 但这是非常私人的事情。 一般来说,BPMN倾向于从业务流程建模/业务分析背景中获得支持。 UML AD倾向于来自软件角度的人士。 工具支持倾向于反映这一点:高端流程建模工具(casewise,aris等)更有可能支持BPMN; 软件建模工具(MagicDraw,Sparx等)赞成UML。 然而,这里越来越多的交叉。 在这两种情况下,我 ...
  • 在iOS项目中使用新的自动参考计数(ARC)内存管理风格有哪些优点和缺点? ARC程序的执行与写好的MRC几乎相同。 也就是说,行为差异通常是不可检测的,因为操作顺序和性能都非常接近。 如果您已经知道如何使用手动引用计数(MRC)来实现OS X或iOS应用程序,则ARC不会真正添加功能 - 它只允许您从源中删除引用计数操作。 如果您不想学习MRC,那么您可能需要先尝试ARC。 很多人都在努力,或者试图忽略MRC的常见做法(例如:我已经为静态分析器引入了一些对象)。 如果您想避免这些问题,ARC将允许您推迟您 ...
  • 优点: CppUnit的主要优点是它遵循Setup / Test / Teardown的XUnit模式。 具有XUnit经验的任何人都能理解这种范例。 必须使用宏而不是属性或反射(就像使用.Net或Java XUnit框架一样)是一种解决方法,但不是真的很麻烦。 而CppUnit本身就是可移植的。 虽然TestRunner.DLL GUI完全是在MFC中构建的,因此只是Windows,但命令行版本不使用Microsoft特定代码,并且在许多平台上执行。 它也很稳定。 开发人员多年来几乎没有做任何事情,主要是 ...
  • 区别在于Flags(按位枚举)可以同时包含多个状态。 如果你有评级系统,你可以使用标志 [Flags] public enum Rating { Normal = 0, // 00000000 Great = 1, // 00000001 Super = 2, // 00000010 Mega = 4, // 00000100 Legendary = 8 // 00001000 } 标志检查位。 注意这个事实。 ...
  • 我发现StyleCop对于在开发团队中获得一致性非常有用。 它不会改进代码的功能价值,但它会提高可读性并消除您在协作环境中获得的许多细微差别。 至于有很多不适用的规则,我发现最好的办法就是关闭那些你不想在机器级别遵循的规则。 然后,您可以随时与其他人共享此配置文件,以便让整个团队按照相同的一组规则工作。 至于ReSharper,规则中有一些交叉点,但它的主要目的是编码生产力,而不是符合一组格式规则。 另一件事是ReSharper是一个IDE唯一的工具,而StyleCop可以在Visual Studio以外的 ...
  • 我认为这是一个没有任何进展的分支,根据缺乏承诺来判断。 它从一些冠冕堂皇的目标开始。 sourceforge上的主页混淆不清,大部分操作都在启动板上 。 我最近使用cppunit足以添加VS2008项目。 I think it's a fork which went nowhere, judging by the lack of committing. It started with some high-sounding goals. The home page on sourceforge is conf ...
  • 做Web GUI的主要“难题”是你将需要建立某种类型的Web服务器。 一个gui应用程序可以只是一个可执行文件。 你也可能(也许)需要一些GCI编码来从用户那里获得信息回到你的程序。 一般来说,它可能会成为更多的工作。 与此平衡的是,任何用户使用他们选择的操作系统和浏览器联网到您的网络服务器应该能够操作您的程序。 The main "gotcha" to doing a web GUI is that you are going to need to set up a web server of some ...
  • 创建OS调用的存根或模拟,并像平常一样在Windows上运行测试。 在这种情况下,使用静态分析工具并强调可移植性问题很有用,因为例如关于数据大小和字节顺序的假设可能导致在一个平台上工作的代码在另一个平台上失败。 如果您的目标和测试平台共享通用数据大小和字节顺序,那么您在这方面处于更好的位置。 Create stubs or mocks of the OS calls and run the tests on Windows as you would normally. It is useful in thi ...
  • 正如我们最终发现的那样,XML必须通过一些XSL转换为不同的格式,这个格式在网站上为插件提供: https://github.com/SonarOpenCommunity/sonar-cxx/blob/master/sonar-cxx-plugin/src/main/resources/xsl/cppunit-1.x-to-junit-1.0.xsl As we eventually found, the XML had to be transformed via some XSL to a differe ...
  • 优点: 您可以扩展您没有源的任何类,甚至是那些类。 例如,查看Apple添加到NSString类的UI扩展,以进行渲染,获取指标等。 由于您可以访问所有实例变量,因此类别为您提供了一种使用逻辑分组在编译单元之间构建代码的好方法,而不是像Java那样采用“它必须都在一个物理位置”的方法。 缺点: 您无法安全地覆盖已由类本身或其他类别定义的方法。 AFAIK,这些语言不保证如果您尝试以下方式实际调用哪个实现: @interface Foo { } - (void) method; @end @interfac ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。