c – 为什么复制构造函数被调用25次,而插入循环只迭代10次?

前端之家收集整理的这篇文章主要介绍了c – 为什么复制构造函数被调用25次,而插入循环只迭代10次?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
参见英文答案 > vector push_back calling copy_constructor more than once?5个
我想知道为什么在下面的C代码中复制构造函数调用25次10次迭代?

如果它是10然后OK 10/10 = 1,或20/10 = 2,或30/10 = 3,但25/10 = 2.5?这里的.5是什么意思?

标题

class Person
{
public:
    Person(std::string name,int age);
    Person(const Person &person);

    const std::string &getName() const;
    int getAge() const;

private:
    std::string name;
    int age;
};

资源:

Person::Person(string name,int age) : name(std::move(name)),age(age)
{}

Person::Person(const Person &person)
{
    this->name = person.name;
    this->age = person.age;
    static int count = 0;
    count++;
    cout << ">>Copy-Person::Person(Person &person) " << count << endl;
}

const string &Person::getName() const
{
    return name;
}

int Person::getAge() const
{
    return age;
}

用法

int main()
{
    vector<Person> persons;

    for (int i = 0; i < 10; ++i)
    {
        Person person(to_string(i + 1),i);
        persons.push_back(person);
    }
    cout << "-----------------------------------------------" << endl;
    for (Person &person : persons)
    {
        cout << "name = " << person.getName() << " age = " << person.getAge() << endl;
    }
    return 0;
}

输出

>>Copy-Person::Person(Person &person) 1
>>Copy-Person::Person(Person &person) 2
>>Copy-Person::Person(Person &person) 3
>>Copy-Person::Person(Person &person) 4
>>Copy-Person::Person(Person &person) 5
>>Copy-Person::Person(Person &person) 6
>>Copy-Person::Person(Person &person) 7
>>Copy-Person::Person(Person &person) 8
>>Copy-Person::Person(Person &person) 9
>>Copy-Person::Person(Person &person) 10
>>Copy-Person::Person(Person &person) 11
>>Copy-Person::Person(Person &person) 12
>>Copy-Person::Person(Person &person) 13
>>Copy-Person::Person(Person &person) 14
>>Copy-Person::Person(Person &person) 15
>>Copy-Person::Person(Person &person) 16
>>Copy-Person::Person(Person &person) 17
>>Copy-Person::Person(Person &person) 18
>>Copy-Person::Person(Person &person) 19
>>Copy-Person::Person(Person &person) 20
>>Copy-Person::Person(Person &person) 21
>>Copy-Person::Person(Person &person) 22
>>Copy-Person::Person(Person &person) 23
>>Copy-Person::Person(Person &person) 24
>>Copy-Person::Person(Person &person) 25
-----------------------------------------------
name = 1 age = 0
name = 2 age = 1
name = 3 age = 2
name = 4 age = 3
name = 5 age = 4
name = 6 age = 5
name = 7 age = 6
name = 8 age = 7
name = 9 age = 8
name = 10 age = 9

解决方法

你没有为你的人物矢量保留任何记忆.这意味着当push_back期间的persons.size()== persons.capacity()时,向量将在堆上分配一个新的更大的缓冲区并将每个元素复制到它.这就是为什么你会看到比预期更多的副本.

如果你写…

persons.reserve(10);

…在循环之前,您将看不到任何“额外”副本.

live example on wandbox

请注意,您可以通过使用std :: vector :: emplace_back和std :: vector :: reserve来避免副本altogheter:

for (int i = 0; i < 10; ++i)
{
    persons.emplace_back(to_string(i + 1),i);
}

这只会打印:

name = 1 age = 0

name = 2 age = 1

name = 3 age = 2

name = 4 age = 3

name = 5 age = 4

name = 6 age = 5

name = 7 age = 6

name = 8 age = 7

name = 9 age = 8

name = 10 age = 9

live example on wandbox

猜你在找的C&C++相关文章