Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/137.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何为std::vector使用分配器?_C++_Class_Vector_Std_Allocator - Fatal编程技术网

C++ 如何为std::vector使用分配器?

C++ 如何为std::vector使用分配器?,c++,class,vector,std,allocator,C++,Class,Vector,Std,Allocator,我没有得到模板std::vector的字段,第一个字段指定了类型,例如一个类,但是我没有得到分配器、指针和引用变量 我将提供一个简单的例子,我想使用类person的向量,所以我必须对模板向量说:嘿,给我存储一些person的,但是我还必须告诉向量如何分配那个类。(1) 该示例的正确语法是什么 以下是一些代码: 旁注,在人的构造函数中,c(2)我应该使用日期* class date { public: int m_nMonth; int m_nDay; int m_nYe

我没有得到模板std::vector的字段,第一个字段指定了类型,例如一个类,但是我没有得到分配器、指针和引用变量

我将提供一个简单的例子,我想使用类person的向量,所以我必须对模板向量说:嘿,给我存储一些person的,但是我还必须告诉向量如何分配那个类。(1) 该示例的正确语法是什么

以下是一些代码:

旁注,在人的构造函数中,c(2)我应该使用日期*

class date
{
public:
    int m_nMonth;
    int m_nDay; 
    int m_nYear;

    int getMonth(return m_nMonth;)
    int getDay(return m_nDay;)
    int getYear(return m_nYear;)

    void setDate(int month, int day, int year)
    {
            m_nMonth=month;
            m_nDay=day;
        m_nYear=year;
    }

    date()
    {
        setDate(0,0,0);
    }

    date(int month, int day, int year)
    {
        setDate(month,day,year);
    }

    void printDate()
    {
        printf("%d/%d/%d", m_nMonth, m_nDay, m_nYear);
    }
}

class person
{
public:
    int m_nID;
    char m_sName[128];
    char m_sSurname[128];
    date m_cBirthDay;

    void setName(const char* name)
    {
        strncpy(m_sName, name, 127);
        m_sName[127]= '\0';
    }

    void setSurname(const char* surname)
    {
        strncpy(m_sSurname, surname, 127);
        m_sName[127]= '\0';
    }

    void setBirthDay(date* bday);
    {
        m_cBirthDay.date(bday.getMonth(), bday.getDay(), bday.getYear)
    }

    person(int id, const char* name, const char* surname, date bday)/*should it be date* bday? why?*/
    {
        m_nID=id;
        setName(name);
        setSurname(surname);
        setBirthDay(bday);
    }

}
我想您必须在类person中声明一个名为分配器的函数,这是您调用时作为第二个参数传递的函数,(3)如何定义分配器函数

vector <person*, /*some allocator call*/> ImAVector;
(6) 如何检索指向特定实例的指针?(7) 我可以使用迭代器执行类似的操作吗

//(6)
person* = ImAVector[someIterator];
//and then (7)
ImAVector[someIterator].setName(someConstCharPointer);
string* something = person.getName();
另外,(8)分配器是否与构造函数有关?(9) 在分配类的实例后立即填充类的字段是什么常见过程?(10) 分配器可以调用构造函数吗

最后一个问题:(11)什么是参考字段、指针字段和它们的常量对应项

我知道要写的东西很多,但我在过去的两天里一直在寻找没有成功的例子,所以如果有人能对这个很少讨论的话题有所了解,那就太好了。我计算了所有的问题,以便于回答


非常感谢您的回答:)

您不需要指定类型以外的任何内容

vector<person> people;
people.emplace_back(/* constructor arguments for a person */);  // constructs the person in place instead of copying into the vector.

person p;
p.stuff();
vector.push_back(p);  // copies p into the vector
矢量人;
people.emplace_back(/*个人的构造函数参数*/);//在适当的位置构造人员,而不是复制到向量中。
人p;
p、 填充();
向量。推回(p);//将p复制到向量中
基本上,分配器和其他字段的默认值已经足够好了,除非您有理由重写默认行为,否则无需指定它们。我从来没有理由这么做

该示例的正确语法是什么

除非你有很好的理由存储指针,否则不要。那你就申报吧

std::vector<person> people;

// In C++11, you can create people in place
people.emplace_back(id, name, surname, birthday);

// Historically, you had to create a person and copy it in
people.push_back(person(id, name, surname, birthday));
另一种方法是处理原始指针,并尝试在正确的时间删除对象;但是生命太短暂了,不能再胡说八道了

(2) 我应该使用
日期*

不可以。对于大型或复杂的类,传递
const
引用、
const-date&
可能是有益的;但是这个类非常简单,可以通过值传递。传递指针会很奇怪,除了在C这样的语言中,这是通过引用传递的唯一方法

如何检索指向特定实例的指针

people[index]
提供了一个指向具有该索引的人员的引用(而不是指针)。如果出于某种原因确实需要指针,请使用
&
获取地址:

person & ref = people[index];
person * ptr = &ref;
我可以用迭代器做这样的事情吗

否,
[]
用于通过数组索引进行访问,而不是取消对迭代器的引用。如果您有一个迭代器
it
,那么
*it
将为您提供对对象的引用

分配器是否与构造函数有关

不,如果您有特殊的内存管理需求,并且不希望向量简单地在堆上分配内存,那么就使用分配器。这一用途相当罕见

在分配类的实例之后立即填充类的字段是一个常见的过程


初始化构造函数中的所有数据成员是很常见的,是的。

例如,有关分配器的示例,请参见。大多数情况下,您不需要任何分配器。为什么需要自定义分配器?只需做一些事情,例如:性病患者;人。推回(新的人(…)即使不使用指针也可以工作:
std::vector persons;人。推回(人(…)并且您通常希望在向量中使用一些智能指针,例如
std::vector
。如果可能,请使用C++11编译器。感谢您的快速响应,我没有得到智能指针的部分,它将用于什么?请花几个小时或几天的时间阅读一本好的C++11编程书籍。我们没有很多时间教你这些。另请参见&people.emplace_back(构造函数参数);这会做什么?@1.auxl2.1laux.2它在适当的位置构造对象,而不是构造对象,然后将其复制到向量中(如评论所说),当然是关于(2)的!更清楚的是,C、C++的方式可能有点苛刻,但这是一个更好的方法。
std::vector<std::unique_ptr<person>> people;
people.push_back(std::unique_ptr<person>(new person(id, name, surname, birthday)));
person & ref = people[index];
person * ptr = &ref;