Skip to content

权重随机,权重大的,更大机会出现在队列前部。

Notifications You must be signed in to change notification settings

kikyoo/weight-sort

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation

weight-sort

权重随机,权重大的,更大机会出现在队列前部。

Simple example:

int main() {

  const size_t SIZE = 4;
  std::vector<std::pair<std::string, int>> vec;
  for (int i =0;  i < SIZE; i ++) {
    vec.push_back(std::make_pair("key_" + std::to_string(i), i*i));
  }

  std::cout << "do some statistics:" << std::endl;
  std::map<std::pair<std::string, int>, int> statistics;
  for (int i =0;i < 100000; i ++) {
    auto out = WeightSort(vec);
    statistics[out[0]] +=1;
  }
  for (const auto& pair: statistics) {
    std::cout << "key:" << std::get<0>(pair.first) <<", weight:" << std::get<1>(pair.first) << ", times:" << pair.second << std::endl;
  }

  return 0;
}

可能结果:

do some statistics:
key:key_0, weight:0, times:4866
key:key_1, weight:1, times:7479
key:key_2, weight:4, times:20708
key:key_3, weight:9, times:66947

About

权重随机,权重大的,更大机会出现在队列前部。

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages