C ++了解矢量创建的size_t行为 [英] c++ understanding size_t behaviour for vector creation

查看:151
本文介绍了C ++了解矢量创建的size_t行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是对 https:// softwareengineering的关注。我几天前发布的问题stackexchange.com/questions/256241/c-coding-practice-class-vs-free-functions 。简而言之,想法是创建一个用于统计数据分析的自定义矢量类。

this is a folow up to this https://softwareengineering.stackexchange.com/questions/256241/c-coding-practice-class-vs-free-functions question I posted a few day ago. In short, the idea is to create a custom vector class for statistical data analysis.

我得到了一个很好的答复,这使我意识到我需要了解:为什么使用容器类的构造函数中的size_t,为什么仍要使用它?

I got a great response, that made me realise that I need to understand: why use size_t in a constructor of a container class and why use it anyway?

这是建议的解决方案的一部分:

Here is a part of the proposed solution:

template<class T>
class vect
{
    std::vector<T> m;

public:
    vect(size_t n) :m(n) {}
    void addTo(T a){ m.push_back(a); }
    std::vector<T> get() const { return m;}
    ... more functions and overloaded operators
};

我知道size_t是一种(无符号int)数据类型,应该用来表明它的值应该代表n个对象的大小。

I understand that size_t is an (unsigned int) data type and should be used to indicate that it's value should represent the size of n object.

为了了解size_t的行为,我做了以下操作:

In order to understand the behaviour of size_t I did the following:

int main() {
    vect<int> m(0);
    vect<int> n(100);
    std::cout << sizeof(n) << std::endl;
    std::cout << sizeof(m) << std::endl;
    std::cout << sizeof(m.get()) << std::endl;
    for (int i = 0 ; i < 100; i++) {
        m.addTo(i);
    }

    std::cout << sizeof(m) << std::endl;
    std::cout << sizeof(m.get()) << std::endl;

    }

全部返回 24。 (我希望在向对象添加参数后,该对象的大小发生变化。)但是:

all of which return "24". (I expected a change in the size of the object after adding parameters to it.) However:

    for(int i = 0; i<100;i++)
        std::cout << m[i] << std::endl;

很好地打印出从0到100的所有值。因此,我知道星号中有100个整数向量,但是为什么它的大小是24,而不是100?

nicely prints out all values from 0 to 100. Thus I know that there are 100 integers stared in the vector, but then why is its size 24 and not 100?

显然,我是c ++编程的新手,更糟糕的是,这是我的第一个模板类。

Obviously I am new to c++ programming and to make things worse this is my first template class.

感谢您的时间和耐心,我非常感谢。

Thank you for your time and patience, I really appreciate it.

推荐答案

sizeof 与内存中 type 的大小有关。您的 vect 类是一种占用24个字节的类型。类型的大小一旦编译就永远不会改变。

sizeof is concerned with the size of a type in memory. Your vect class is a type that takes up 24 bytes. The size of a type never changes once it's been compiled.

但是向量如何在不改变大小的情况下存储这么多的信息?因为它包含指向可能占用更多空间的其他事物的指针(实际上,它们可以占用您需要的空间,所以才将它们称为 dynamic 数据结构)。大概您的 vect 实例包含一个指向标准向量类的指针,该向量类包含其他指针,可能指向一个数组或一个动态分配的存储实际数据的部分。

But how can your vector store so much information without changing it size? Because it contains pointers to other things that can take up much more space (indeed, they can take up as much space as you need - that's why they're called dynamic data structures). Presumably your vect instance contains a pointer to the standard vector class, which contains other pointers, maybe to an array or a dynamically allocated section of memory which holds the actual data.

您无法查询那些间接引用的私有内存的大小,因为您不知道它们的名称或类型。实际上,创建此类容器类的主要原因是,您不必不必知道要分配多少内存-您只需将东西塞进其中,它们就会静默分配所需的内存

You cannot query the size of those private, indirectly referenced bits of memory because you don't know their name, or their type. In fact, a major reason for creating such container classes is so that you don't have to know how much memory to allocate - you just stuff things into them, and they silently allocate as much memory as necessary.

这篇关于C ++了解矢量创建的size_t行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆