c ++主要外部功能之间的共享列表

时间:2014-09-09 22:45:05

标签: c++ list function map

我正在制作兔子种群模拟器,以应对我正在做的挑战。我有一个类对象的映射。我还有一个包含地图键的列表。每一轮模拟器我想在课堂上添加更多对象,并更新我的地图和列表。为此,我编写了一个单独的“创建”函数,它将随机生成新的类对象并将它们添加到地图中并将键添加到列表中。

问题是当我调用创建函数时,然后使用列表迭代地图,它显示地图或列表为空(不确定是哪个)。如果我在离开函数之前遍历地图,它只显示最新的对象。如果我将函数的代码移动到main函数中,它可以正常工作(调用它两次就会给我在代码的第一次迭代中创建的新对象,以及第二次迭代创建的对象)。

我猜测每次调用函数时都会创建一个新列表或映射,并覆盖旧列表或映射。如何在主函数和创建函数之间传递列表和映射?

这是我的代码:

#include "stdafx.h"
#include <iostream>
#include "windows.h"
#include <string>
#include <sstream>
#include <array>
#include <time.h>
#include <conio.h>
#include <map>
#include <list>

class Bunny
{
public:
    char fname;
    int sex, age, color, status;
    Bunny(int, int, int, int);
    Bunny();
    int s() { return (sex); }
    int a() { return (age); }
    int c() { return(color);}
    int st() { return (status);}
    int aging(int age) { return (age + 1); }
};

Bunny::Bunny(int s, int a, int c, int st)
{
    sex = s;
    age = a;
    color = c;
    status = st;
}
Bunny::Bunny(){}

void creation(std::map<std::string, Bunny> bunnies, std::list<std::string>names, int births);

std::string firstname(int s, int num)
{
    std::string name;
    if (s == 0)
    {
        switch (num)
        {
        case (0) :
            name = "Tim";
            break;
        case (1) :
            name = "Tom";
            break;
        case (2) :
            name = "Mark";
            break;
        case (3) :
            name = "Bob";
            break;
        case (4) :
            name = "Rob";
            break;
        }
    }
    if (s == 1)
    {
        switch (num)
        {
        case (0) :
            name = "Suzy";
            break;
        case (1) :
            name = "Linda";
            break;
        case (2) :
            name = "Mary";
            break;
        case (3) :
            name = "Jan";
            break;
        case (4) :
            name = "Julie";
            break;
        }
    }
            return (name);
}

void main()
{

    int num = rand() % 5;
    int n, births = 10;
    std::list<std::string>names;
    std::map<std::string, Bunny> bunnies;
    srand(time(0));
    creation(bunnies, names, births);
    std::cout << "Number" << "\t" << "Name" << "\t" << "age" << "\t" << "Sex" << "\t" << "Color" << "\t" << "Vampire?"  "\n";
    n = 0;
    for (std::list<std::string>::iterator it = names.begin(); it != names.end(); it++)
    {
        n++;
        std::cout << n << "\t";
        std::cout << " " << *it;
        std::cout << "\t" << bunnies[*it].a() << "\t" << bunnies[*it].s() << "\t" << bunnies[*it].c() << "\t" << bunnies[*it].st() << "\n";
    }
    creation(bunnies, names, births);
    _getch();
}



/*void year()
{
    for (std::list<std::string>::iterator it = names.begin(); it != names.end(); it++)
    {
        bunnies[*it].aging(bunnies[*it].a())
    }
}*/

void creation(std::map<std::string, Bunny> bunnies,std::list<std::string> names,int births)
{
    int n;
    for (n = 0; n < births; n++)
    {
        int num = std::rand() % 5;
        char id = (std::rand() % 100) + 20;
        int s = std::rand() % 2;
        std::string f = firstname(s, num) + '_' + id;
        int a = 0;
        int c = std::rand() % 5;
        int st;
        if (rand() % 50 == 43) st = 1; else st = 0;
        bunnies[f] = Bunny(s, a, c, st);
        names.push_front(f);
        //std::cout << f << " " << bunnies[f].a() << " " << bunnies[f].c() << "\n";
    }
    std::cout << "Number" << "\t" << "Name" << "\t" << "age" << "\t" << "Sex" << "\t" << "Color" << "\t" << "Vampire?"  "\n";
    n = 0;
    for (std::list<std::string>::iterator it = names.begin(); it != names.end(); it++)
    {
        n++;
        std::cout << n << "\t";
        std::cout << *it;
        std::cout << "\t" << bunnies[*it].a() << "\t" << bunnies[*it].s() << "\t" << bunnies[*it].c() << "\t" << bunnies[*it].st() << "\n";
    }
}

1 个答案:

答案 0 :(得分:1)

您的问题是您的主要功能是传递地图并按值列出而不是通过引用传递。这意味着您的创建函数正在接收现有地图/列表的副本,而不是对您创建的原始地图/列表的引用。由于它只对该副本进行编辑,因此它所做的任何更改都不会反映在主函数中。

更改您的创建功能:

void creation(std::map<std::string, Bunny> bunnies, std::list<std::string>names, int births)

void creation(std::map<std::string, Bunny>& bunnies, std::list<std::string>& names, int births)