size_type
Unsigned integer type, enough to hold the distance between two iterators
size_type is an unsigned type, indicating the length or subscript of the element in the container, and vector::size_type i = 0;
The member type in the standard library string is an unsigned type to realize the independence between the standard library type and the machine. It can store the size of any string object.
The return value type of the member function (size()) of the standard library string is string::size_type.
When accessing elements with subscripts, string uses string::size_type as the subscript type.
string::size_type its length can be different on different machines, not a fixed length. But as long as you use this type, it makes your program suitable for this machine and the actual machine. A similar example is vector::size_type.
size_type is a type defined by string class type and vector class type, which is used to save the length of any string object or vector object
string s("hello world"); //Calculate the length of storage elements in the container string::size_type len = s.size(); string::size_type len1=s.length(); cout << "s.size()= " << len << endl; cout << "s.length()= " << len1 << endl; //Subscript access string::size_type pos1 = -1;//The output will directly report an error because it is an unsigned integer string::size_type pos = 1; //If a negative value n is assigned to an unsigned integer, the negative value n is automatically converted to a larger unsigned value cout << "pos= "<<pos1<< endl; cout << "s[pos]= " << s[pos] << endl;
string s("hello world"); //Represents the distance between two iterators string::size_type len3 = s.end()-s.begin();//If a negative value n is assigned to an unsigned integer, the negative value n is automatically converted to a larger unsigned value string::size_type len4 = s.begin() - s.end();//Negative value cout << "begin reach end The distance between iterators is:" << len3 << endl; cout << "len4= " << len4 << endl;
In the new feature standard of c++11, the compiler can infer the type of variable through auto or decltype
string s("hello world"); auto len = s.size(); cout << "s.size()=" << len << endl; decltype(s.size()) len1 = s.size(); cout << "s.size()= " << len1 << endl;
difference_type
Signed integer type, enough to hold the distance between two iterators
difference_type is used to represent the distance between two iterators.
vector<int> v(10, 1); vector<int>::difference_type len = v.begin() - v.end(); vector<int>::difference_type len1 = v.end() - v.begin(); cout << "begin-end= " << len << endl; cout << "end-begin= " << len1 << endl;
value_type
Element type
value_type is used to represent the category of the object referred to by the iterator.
value_type is actually the type of template passed in
class p { public: p() { num = 100; } p(int n) :num(n) {}; int num; }; void test() { vector<p> v; vector<p>::value_type n1(520);//Equivalent to p n1(520); v.push_back(n1); cout << v[0].num << endl; }
(refer to page 295 of c++prime for the above definition)
Write the best place, and add two more
size_t
size_t is an unsigned type, which is used to indicate the length or subscript of the array. It must be a positive number, std::size_t. Design size_t is to adapt to multiple platforms, and its introduction enhances the portability of programs on different platforms.
Note: and size_ The difference between type and size_type is only applicable to the container, which can be understood as the size in the container_ T is encapsulated and becomes size_type, size in the container_ t. And when using STL to indicate the length of the container, we usually use size_type.
ptrdiff_t
ptrdiff_t is a signed type, which is used to store the gap between two pointers in the same array. It can make a negative number, std::ptrdiff_t. Ditto, using ptrdiff_t to get the platform independent address difference
Note: the difference in the container_ Type is equivalent to ptrdiff_ For encapsulation made of T, the distance between two iterators is generally calculated in the container, using difference_type, because the bottom layer of the container is the encapsulation of pointers, and prtdiff is used to calculate the distance between pointers_ t.