小编Ala*_*ing的帖子

C++ STL 优先队列客户比较器不起作用

我过去写过几乎类似的代码并且它有效(我记得很模糊)。似乎比较器在这里不起作用?有什么线索吗?

#include<iostream>
#include<vector>
#include<queue>
#include<iterator>
#include<algorithm>
using namespace std;

    typedef pair<vector<int>::iterator,vector<int>::iterator> PR;
    struct CompareFn{
        bool operator()(const PR& a, const PR& b){
            //cout<<"a and b first: "<<*(a.first)<<" "<< *(b.first)<<endl;
            return *a.first > *b.first;
        }
    };

vector<int> mergeKSortedArrays(vector<vector<int>> &A) {  
vector<int> result;
    
    priority_queue<PR, vector<PR>, CompareFn> PQ;
    for(auto e:A){  
        if(e.size()>0) PQ.push({e.begin(),e.end()});
    }

    while(PQ.size()>0) {
        PR tmp = PQ.top(); PQ.pop();
        auto cur=tmp.first;
        auto lst=tmp.second;
        result.emplace_back (*cur);
        if((++cur)!=lst) PQ.push({cur,lst});
    }
return result;
}


int main() { 
vector<vector<int>> v= {{2,3,8,10},{1,4,12},{4,5,8}};
 vector<int> result = mergeKSortedArrays(v);
 copy(result.begin(),result.end(), ostream_iterator<int>(cout," …
Run Code Online (Sandbox Code Playgroud)

c++ priority-queue comparator

2
推荐指数
1
解决办法
60
查看次数

go time.Now() 没有给出适当的时间

我正在运行这个简单的 go 程序,并希望在启动和关闭日志时获得不同的时间(3 秒的时间差),因为我给了 3 秒的睡眠时间,但我得到了相同的时间。

package main

import (
    "fmt"
    "time"
)

const (
    logInfo = "INFO"
    logWarning = "WARNING"
    logError = "ERROR"
)

type logEntry struct {
    time time.Time
    severity string
    message string
}

var myLogChannel = make(chan logEntry, 50)

func main() {
    go logger()
    myLogChannel <- logEntry{ time.Now(), logInfo, "App is starting" }
    time.Sleep(3 * time.Second)
    fmt.Println("Time is :", time.Now().Format("2006-01-02 15:04:06"))
    myLogChannel <- logEntry{ time.Now(), logInfo, "App is shuting down" }
    time.Sleep(2 * time.Second)
}
func logger() …
Run Code Online (Sandbox Code Playgroud)

time go

-3
推荐指数
1
解决办法
57
查看次数

标签 统计

c++ ×1

comparator ×1

go ×1

priority-queue ×1

time ×1