size-type

std::size_t or std::vector<Foo>::size_type?

て烟熏妆下的殇ゞ 提交于 2019-12-08 03:27:55
问题 When I loop on a std::vector<Foo> (or every container having random access iterator) I use an unsigned integer variable i . If I want to respect the norm, should I use std::size_t or the type given by the container itself : std::vector<Foo>::size_type ? If I chose std::size_t (for readability reasons), can I be sure that every implementation of every container in std namespace uses std::size_t as size_type ? Note : I use C++98 only (for compatibility reasons). 回答1: It is not necessarily true

c++ illogical >= comparison when dealing with vector.size() most likely due to size_type being unsigned

℡╲_俬逩灬. 提交于 2019-12-06 14:04:46
I could use a little help clarifying this strange comparison when dealing with vector.size() aka size_type vector<cv::Mat> rebuiltFaces; int rebuildIndex = 1; cout << "rebuiltFaces size is " << rebuiltFaces.size() << endl; while( rebuildIndex >= rebuiltFaces.size() ) { cout << (rebuildIndex >= rebuiltFaces.size()) << " , " << rebuildIndex << " >= " << rebuiltFaces.size() << endl; --rebuildIndex; } And what I get out of the console is rebuiltFaces size is 0 1 , 1 >= 0 1 , 0 >= 0 1 , -1 >= 0 1 , -2 >= 0 1 , -3 >= 0 If I had to guess I would say the compiler is blindly casting rebuildIndex to

string::size_type instead of int

本小妞迷上赌 提交于 2019-11-28 03:15:06
const std::string::size_type cols = greeting.size() + pad * 2 + 2; Why string::size_type ? int is supposed to work! it holds numbers!!! A short holds numbers too. As does a signed char. But none of those types are guaranteed to be large enough to represent the sizes of any strings. string::size_type guarantees just that. It is a type that is big enough to represent the size of a string, no matter how big that string is. For a simple example of why this is necessary, consider 64-bit platforms. An int is typically still 32 bit on those, but you have far more than 2^32 bytes of memory. So if a

size_t vs int in C++ and/or C

落花浮王杯 提交于 2019-11-27 07:48:25
Why is it that in C++ containers, it returns a size_type rather than an int ? If we're creating our own structures, should we also be encouraged to use size_type ? In general, size_t should be used whenever you are measuring the size of something. It is really strange that size_t is only required to represent between 0 and SIZE_MAX bytes and SIZE_MAX is only required to be 65,535... The other interesting constraints from the C++ and C Standards are: the return type of sizeof() is size_t and it is an unsigned integer operator new() takes the number of bytes to allocate as a size_t parameter

string::size_type instead of int

别说谁变了你拦得住时间么 提交于 2019-11-27 05:06:52
问题 const std::string::size_type cols = greeting.size() + pad * 2 + 2; Why string::size_type ? int is supposed to work! it holds numbers!!! 回答1: A short holds numbers too. As does a signed char. But none of those types are guaranteed to be large enough to represent the sizes of any strings. string::size_type guarantees just that. It is a type that is big enough to represent the size of a string, no matter how big that string is. For a simple example of why this is necessary, consider 64-bit

C++ for-loop - size_type vs. size_t

北城以北 提交于 2019-11-27 04:15:44
In the C++ Primer book, Chapter (3), there is the following for-loop that resets the elements in the vector to zero. for (vector<int>::size_type ix = 0; ix ! = ivec.size(); ++ix) ivec[ix] = 0; Why is it using vector<int>::size_type ix = 0 ? Cannot we say int ix = 0 ? What is the benefit of using the first form on the the second? Thanks. The C++ Standard says, size_type | unsigned integral type | a type that can represent the size of the largest object in the allocation model Then it adds, Implementations of containers described in this International Standard are permitted to assume that their

vector<int>::size_type in C++

有些话、适合烂在心里 提交于 2019-11-26 18:44:19
What is meant by this C++ statement? vector<int>::size_type x; And, what is the use of the scope operator :: here? In other words, how do we read this statement in English? For example, for X::x(){...} , we say that x() is a member function of class X . size_type is a (static) member type of the type vector<int> . Usually, it is a typedef for std::size_t , which itself is usually a typedef for unsigned int or unsigned long long . I would read it as "declare x as a variable of a type suitable for holding the size of a vector". The vector defines its own type for its length, and it's always

size_t vs int in C++ and/or C

眉间皱痕 提交于 2019-11-26 13:49:36
问题 Why is it that in C++ containers, it returns a size_type rather than an int ? If we're creating our own structures, should we also be encouraged to use size_type ? 回答1: In general, size_t should be used whenever you are measuring the size of something. It is really strange that size_t is only required to represent between 0 and SIZE_MAX bytes and SIZE_MAX is only required to be 65,535... The other interesting constraints from the C++ and C Standards are: the return type of sizeof() is size_t

&#39;size_t&#39; vs &#39;container::size_type&#39;

青春壹個敷衍的年華 提交于 2019-11-26 11:10:54
Is there is a difference between size_t and container::size_type ? What I understand is size_t is more generic and can be used for any size_type s. But is container::size_type optimized for specific kinds of containers? The standard containers define size_type as a typedef to Allocator::size_type (Allocator is a template parameter), which for std::allocator<T>::size_type is typically defined to be size_t (or a compatible type). So for the standard case, they are the same. However, if you use a custom allocator a different underlying type could be used. So container::size_type is preferable for

vector<int>::size_type in C++

旧巷老猫 提交于 2019-11-26 06:34:32
问题 What is meant by this C++ statement? vector<int>::size_type x; And, what is the use of the scope operator :: here? In other words, how do we read this statement in English? For example, for X::x(){...} , we say that x() is a member function of class X . 回答1: size_type is a (static) member type of the type vector<int> . Usually, it is a typedef for std::size_t , which itself is usually a typedef for unsigned int or unsigned long long . 回答2: I would read it as "declare x as a variable of a type