简体   繁体   中英

Could someone explain me this line?

Ok, here's one line in C++

typedef vector<double>::size_type vec_sz;

I understand, that this line creates an alias called vec_sz. But I can't understand why vector is used here??? What general has typedef and vector. I try to find out this whole evening, but I still don't understand.

vec_sz is now an alias for the data type used to represent the size of the vector (which is almost always size_t ). So suppose you want to index elements in the vector, you'd use vec_sz as follows:

for(vector<double>::size_type i = 0; i < some_vector.size(); ++i)
{
...
}

which (thanks to the typedef ) is now equivalent to:

for(vec_sz i = 0; i < some_vector.size(); ++i)
{
...
}

You might wonder, "Why use vec_sz ? Why shouldn't I just use int ?". Well, this makes your code more portable and less prone to relying on your assumptions (which could be wrong).

Of course, it's better to access elements in vector through iterators .

Classes can have typedefs inside of them:

class foo {
public:
  typedef int bar;
}

foo::bar my_int; // same as int my_int;

Vector has a typedef that corresponds to size_t

vector<double>::size_type my_int; // roughly the same as size_t my_int
                                  // implementation may not use size_t

That's a lot of typing and so you can typedef it again to make the line shorter:

typedef vector<double>::size_type vec_sz;
vec_sz my_int; // same as vector<double>::size_type my_int; which is the same as size_t my_int;

Each vector-class (vector in your case) defines some unsigned integral type named size_type. Something like this:

template<class T> vector {
  typedef size_t size_type;
  ...
}

It's type used in std::vector class for storing size. On many platforms and os'es this can differ, and using this type ensures that you will read its size properly.

For example, one could use int as its size type. However, if the vector's size type is unsigned int, when the vector exceeds maximum signed int size, strange results can happen.

Additional information can be found on http://en.wikipedia.org/wiki/C_data_types As you can see, int is guaranteed to have at least 16 bytes, which can differ between operating systems.

size_type is a member typedef of std::vector<double> .

namespace std {
    template <typename T> class vector {
    public:
        typedef __something size_type;
        size_type size() const;
    // ...
    };
}

The standard avoids specifying things like "the return type of vector<T>::size() is unsigned int , in case an implementation can support more elements in a vector than can be represented by the max value of unsigned int . Instead, it specifies some implementation-defined member types, so the library can use whatever type makes the most sense, and user code can easily use that type.

Also, vector<double>::size_type and vector<char>::size_type could be different (though they're usually the same).

So the quoted code says "make another typedef vec_sz for the C++ library's type used for numbers of elements in vectors containing double".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM