size-type

Do I really need to return Type::size_type?

你。 提交于 2021-01-27 06:44:38
问题 I often have classes that are mostly just wrappers around some STL container, like this: class Foo { public: typedef std::vector<whatever> Vec; typedef Vec::size_type size_type; const Vec& GetVec() { return vec_; } size_type size() { return vec_.size() } private: Vec vec_; }; I am not so sure about returning size_type . Often, some function will call size() and pass that value on to another function and that one will use it and maybe pass it on. Now everyone has to include that Foo header,

Do I really need to return Type::size_type?

点点圈 提交于 2021-01-27 06:42:57
问题 I often have classes that are mostly just wrappers around some STL container, like this: class Foo { public: typedef std::vector<whatever> Vec; typedef Vec::size_type size_type; const Vec& GetVec() { return vec_; } size_type size() { return vec_.size() } private: Vec vec_; }; I am not so sure about returning size_type . Often, some function will call size() and pass that value on to another function and that one will use it and maybe pass it on. Now everyone has to include that Foo header,

Do I really need to return Type::size_type?

↘锁芯ラ 提交于 2021-01-27 06:41:12
问题 I often have classes that are mostly just wrappers around some STL container, like this: class Foo { public: typedef std::vector<whatever> Vec; typedef Vec::size_type size_type; const Vec& GetVec() { return vec_; } size_type size() { return vec_.size() } private: Vec vec_; }; I am not so sure about returning size_type . Often, some function will call size() and pass that value on to another function and that one will use it and maybe pass it on. Now everyone has to include that Foo header,

Is `size_t` always an alias for `vector<int>::size_type` or any other container type?

百般思念 提交于 2021-01-27 06:10:10
问题 Let's make the simplest example possible: Formulation 1: std::vector<int> vec; // add 10E11 elements for(std::size_t n = 0; n < vec.size(); ++n) // ... Formulation 2: std::vector<int> vec; // add 10E11 elements for(std::vector<int>::size_type n = 0; n < vec.size(); ++n) // ... Naturally, unsigned int or any inappropriate data types do not work here and we have to compile x64. My question is: Is there any case in which the first formulation can lead to issues or can we safely always write it

C++ size_t modulus operation with negative operand

别说谁变了你拦得住时间么 提交于 2020-06-23 06:37:31
问题 So there are three values that a modulus operation can give you: Then: -7 % 5 = 3 (math, remainder >= 0) -7 % 5 = -2 (C++) -7 % (size_t)5 = 4 (C++) Another example: -7 % 4 = 1 (math, remainder >= 0) -7 % 4 = -3 (C++) -7 % (size_t)4 = 1 (C++) When the left hand operand is positive, the answer between all three methods are the same. But for negative values they all seem to have their own methods. How is the value of modulus operations on unsigned operands calculated in C++? 回答1: This is what

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

时间秒杀一切 提交于 2020-01-02 14:30: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

C++ for-loop - size_type vs. size_t

那年仲夏 提交于 2019-12-17 06:39:39
问题 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. 回答1: 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

Can I trust sizeof(size_t) <= sizeof(unsigned long int) is always true?

别说谁变了你拦得住时间么 提交于 2019-12-11 09:17:17
问题 Can I trust sizeof(size_t) <= sizeof(unsigned long int) is always true, according to C89 standard? i.e., I will not loos value if I use a unsigned long where size_t is expected and vice-versa. 回答1: Perhaps the best explanation of what happens with size_t and integer types in C89 is the rationale document of it. Read carefully the section 3.4.4 of this document: Sizeof and size_t for C89 (rationale) The 3rd paragraph says that: The type of sizeof, whatever it is, is published (in the library

What is “size of the largest possible object on the target platform” in terms of size_t

江枫思渺然 提交于 2019-12-10 21:55:22
问题 I am reading article about size_t in C/C++ http://web.archive.org/web/20081006073410/http://www.embedded.com/columns/programmingpointers/200900195 (link found through Stackoverflow). Quote from the article: Type size_t is a typedef that's an alias for some unsigned integer type, typically unsigned int or unsigned long, but possibly even unsigned long long. Each Standard C implementation is supposed to choose the unsigned integer that's big enough--but no bigger than needed-- to represent the

gcc compiler __SIZE_TYPE__

做~自己de王妃 提交于 2019-12-10 16:29:40
问题 I'm curious about the variable __SIZE_TYPE__ which is predefined by gcc compiler. Suppose that I coded like following sentence in C typedef __SIZE_TYPE__ size_t; Is there any possibility that an error occurs when I use another C compiler excluding gcc? Do all C compilers have the variable __SIZE_TYPE__ ? 回答1: Yes, it is possible that an error occurs as soon as you use any identifier with double underscore. See the C standard 7.1.3: All identifiers that begin with an underscore and either an