对列表中的对象进行排序和组合值

时间:2015-05-05 12:50:49

标签: c++ sorting stl

我有一个DECLARE @vEndTime DateTime Declare @vStartTime DateTime DECLARE @sql4 nvarchar(4000) DECLARE @parameters nvarchar(4000) SET @parameters = '@vStartTime boards.DateEnd, @vEndTime boards.DateEnd' set @vEndTime = '09/24/2011 12:00:00:00' set @vStartTime = (SELECT MAX(DateEnd) FROM [UICTransfer_Archive].[dbo].[StageLotLineage]) WHILE @vStartTime < '09/24/2011 12:00:00:00' BEGIN SET @vEndTime = DATEADD (minute , 60 , @vStartTime ) select @sql4 = N' INSERT INTO [UICTransfer_Archive].[dbo].[StageLotLineage](LineName, BoardBarcode, MachineProductName, DateStart, DateEnd, MachineName, PlacementTime, RefID, CircuitNumber, FeederSlot, FeederTrack, Component, LotNumber, ReelID) SELECT boards.LineName ,SUBSTRING(boards.BoardBarcode, 1, 5) AS BoardBarcode -- ,board.BoardBarcode ,boards.MachineProductName ,convert(varchar,boards.DateStart,120) ,convert(varchar,boards.DateEnd,120) ,placements.MachineName ,convert(varchar,placements.PlacementTime,120) ,placements.RefID ,placements.CircuitNumber ,placements.FeederSlot ,placements.FeederTrack ,locations.Component ,locations.LotNumber ,locations.ReelID FROM [UICTransfer_Archive].[dbo].[TrnsBoardComplete] as boards LEFT JOIN [UICTransfer_Archive].[dbo].[TrnsPlacements] as placements ON placements.BoardCompleteID = boards.BoardCompleteID LEFT JOIN [UICTransfer_Archive].[dbo].[TrnsLocations] as locations ON (locations.FeederSlot=placements.FeederSlot and locations.FeederTrack = placements.FeederTrack and locations.FeederStack = placements.FeederStack and locations.FeederPallet = placements.FeederPallet and locations.MachineName = placements.MachineName and placements.PlacementTime >= locations.DateValidated and (placements.PlacementTime <= locations.DateInvalidated or locations.DateInvalidated IS NULL) **where boards.DateEnd >= @vStartTime and boards.DateEnd <= @vEndTime and placements.CircuitNumber IS NOT NULL '** EXEC SP_EXECUTESQL @sql4,@parameters,@vStartTime, @vEndTime --select @@rowcount SET @vStartTime = DATEADD (minute , 60 , @vEndTime ) END ,对象有6个整数和一个char数组元素。我想用一个特定的整数(比如整数a)对这些对象进行排序,之后,我想将char数组与排序顺序组合起来。

stl是否有针对此特定排序的某种方法?如果不是,我该如何做这种排序呢?

2 个答案:

答案 0 :(得分:1)

std::list::sort有一个带有自定义比较器的重载,对于更合适的容器,有std::sort。所以你可以用它来排序:

my_list.sort([](object const & o1, object const & o2) {return o1.a < o2.a;});

我不确定你的意思是什么&#34;结合字符数组&#34;,但你可以用std::for_eachstd::transform或简单的方式迭代排序列表for循环,以便依次对每个对象做一些事情。

答案 1 :(得分:0)

您可以将自己的比较器提供给sort函数或实现运算符&lt;为你的object课程。您可以找到更多信息here。请注意使用std::list::sort而不是std::sort

// list::sort
#include <iostream>
#include <list>
#include <string>
#include <cctype>

// comparison, not case sensitive.
bool compare_nocase (const std::string& first, const std::string& second)
{
    unsigned int i=0;
    while ( (i<first.length()) && (i<second.length()) )
    {
        if (tolower(first[i])<tolower(second[i])) return true;
            else if (tolower(first[i])>tolower(second[i])) return false;
        ++i;
    }
    return ( first.length() < second.length() );
}

int main ()
{
    std::list<std::string> mylist;
    std::list<std::string>::iterator it;
    mylist.push_back ("one");
    mylist.push_back ("two");
    mylist.push_back ("Three");

    mylist.sort();

    std::cout << "mylist contains:";
    for (it=mylist.begin(); it!=mylist.end(); ++it)
        std::cout << ' ' << *it;
    std::cout << '\n';

    mylist.sort(compare_nocase);

    std::cout << "mylist contains:";
    for (it=mylist.begin(); it!=mylist.end(); ++it)
        std::cout << ' ' << *it;
    std::cout << '\n';

  return 0;
}