当前位置: 首页>>代码示例>>C++>>正文


C++ Generation::insert方法代码示例

本文整理汇总了C++中Generation::insert方法的典型用法代码示例。如果您正苦于以下问题:C++ Generation::insert方法的具体用法?C++ Generation::insert怎么用?C++ Generation::insert使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Generation的用法示例。


在下文中一共展示了Generation::insert方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: main

int main(int argc, char *argv[]) {
  Generation moms;
  Generation dads;
  Generation x;
  Individual *mom;
  Individual *dad;
  Individual *child;
  UniformRNG r(0.0,1.0);
  int i;

  init();

  // Create 10 males and 10 females, and give them each 2 mutations.
  // Then create a child and put it in generation x:

  for (i=0; i<gsize; i++) {
    dad = new Individual;
    dad->set_sex(MALE);
    dad->genes->add_mutations(2);
    cout << dad->get_id() << ":  " << *dad;
    dads.insert(dad);

    mom = new Individual;
    mom->set_sex(FEMALE);
    mom->genes->add_mutations(2);
    moms.insert(mom);
    cout << mom->get_id() << ":  " << *mom;

    child = new Individual;
    child->set_sex((++r < 0.5) ? MALE : FEMALE);
    child->genes->combine(dad->genes,mom->genes);
    x.insert(child);
    cout << child->get_id() << ":  " << *child;

    cout << endl;
  }

  // Test the remove function and error detection in the Generation class.

  // Print the first three, remove the second, and print again:

  cout << "First three individuals in x: \n  ";
  for (i=0; i<3; i++)
    cout << x[i]->get_id() << "  ";
  cout << endl;
  child = x.remove(1);
  cout << "individual removed: " << child->get_id() << endl;
  cout << "First three individuals remaining in x: \n  ";
  for (i=0; i<3; i++)
    cout << x[i]->get_id() << "  ";
  cout << endl << endl;

  // Same test, but now remove the first one (special case):

  cout << "First three individuals in x: \n  ";
  for (i=0; i<3; i++)
    cout << x[i]->get_id() << "  ";
  cout << endl;
  child = x.remove(0);
  cout << "individual removed: " << child->get_id() << endl;
  cout << "First three individuals remaining in x: \n  ";
  for (i=0; i<3; i++)
    cout << x[i]->get_id() << "  ";
  cout << endl << endl;

  // There are now gsize-2 individuals in x.  Whittle it down to 2:

  for (i = 0; i < gsize-4; i++)
    child = x.remove(0);

  // Print the remaining two individuals:

  cout << "last two individuals in x: " << x[0]->get_id() << " and " << x[1]->get_id() << endl;

  // These should cause an error message:

  child = x[2];
  child = x.remove(2);

  // Test removing the last item:

  child = x.remove(1);
  cout << child->get_id() << endl;

  child = x.remove(0);
  cout << child->get_id() << endl;

  // Gen X should now be empty:

  cout << "Gen X size = " << x.size() << endl;
}
开发者ID:douglasgscofield,项目名称:GeneticSimLib,代码行数:91,代码来源:idemo.C


注:本文中的Generation::insert方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。