C ++中的STL如何工作

时间:2010-06-17 07:23:45

标签: c++

我有非常技术性的问题,我正在和C一起工作,现在我正在学习C ++, 如果我有这样的课程

class Team {
private:
   list<Player> listOfPlayers;
public:
    void addPlayer(string firstName, string lastName, int id) {
        Player newPlayer(string firstName, string lastName, int id);
            listOfPlayers.push_back(Player(string firstName, string lastName, int id));
    }
};

这是玩家的声明:

class Player{
private:
    string strLastName;      
    string strFirstName;     
    int nID;                
public:
    Player(string firstName, string lastName, int id);

};

这是我的玩家构造函数:

Player::Player(string firstName, string lastName, int id){
    nId = id;
    string strFirstName = firstName;
    string strLastName = lastName;
}

所以我的问题是当我调用函数addPlayer时程序究竟发生了什么,

在我的构造函数中,我需要为new Player分配新内存(因为CI总是使用malloc)for strFirstName and strLastName,或者Account和STL字符串的构造函数在没有我的情况下执行它,谢谢提前(如果你不想回答我的问题请至少给我一些信息链接),提前感谢

4 个答案:

答案 0 :(得分:2)

这是您现在拥有的“正确”实施方式:

#include <list>
#include <string>

class Player
{ 
private: 
    std::string strLastName;       
    std::string strFirstName;      
    int nID;                 
public:
    // You should pass std::strings to functions by const reference.
    // (see Kirill V. Lyadvinsky's comment to OP's question)
    Player(const std::string& firstName, const std::string& lastName, int id); 
};

// What follows after the colon is called the initializer list.
Player::Player(const std::string& firstName, const std::string& lastName, int id)
    : strFirstName(firstName), strLastName(lastName), nID(id) {}

class Team
{ 
private: 
    std::list<Player> listOfPlayers; 
public: 
    void addPlayer(const std::string& firstName,
        const std::string& lastName, int id)
    { 
        // Constructs a Player instance and adds it to the list.
        listOfPlayers.push_back(Player(firstName, lastName, id)); 
    } 
};

list的push_back()函数分配一个新节点,该节点包含Player实例和指向其他节点的指针,因此您可以拥有Player个实例的“链”。

关于Account的问题,如果你有这个问题:

class Account
{
private:
    std::string strFirstName;
    std::string strLastName;
};

然后您不必担心为字符数组分配/释放内存,因为std::string会为您处理。

答案 1 :(得分:0)

转到此处http://cplusplus.com

在构造函数中,您应该使用inizialisation列表。

调用方法或为现有变量赋值时,不得写入类型。所以你总是创建新的变量。

示例:

private:
   list<Player> listOfPlayers;
public:
    void addPlayer(string firstName, string lastName, int id) {
        Player newPlayer(string firstName, string lastName, int id);
    }
}

应该是

private:
   list<Player> listOfPlayers;
public:
    void addPlayer(string firstName, string lastName, int id) {
        Player newPlayer(firstName, lastName, id);
    }
}

答案 2 :(得分:0)

我假设您正在使用STL字符串类(std :: string)。

使用std :: string的默认构造函数自动创建Player时,会创建字符串对象。

当你说:strFirstName = firstName时,你正在调用copy constructor来替换内存中的对象。尽量不要考虑指针,C ++就是要尽量减少它们。

因此,在使用strFirstName =“blah”时,您不需要使用malloc。

(顺便说一句,我不知道你在谈论“新帐户”)

答案 3 :(得分:0)

为什么要在构造函数中分配内存?发布的代码没有任何内容分配是必须的。

使用标准STL容器(如list,string等),您可能希望也可能不想使用自定义内存分配器。您的案例中的典型声明是list<Player>。如果你在列表中插入Player对象,它们将被复制构造[如果你正在使用类中的指针,那么提供一个正确的复制构造函数]

如果您想为列表或字符串自定义分配内存,声明将类似于list<Player, CustomAllocator>等。

该标准提供了一个分配器,它在内部使用全局运算符,在需要分配器的地方使用全局运算符作为默认值。查看C ++标准,第20.4.1节。公共界面在那里有很好的描述。