C++ 如何将向量大小与整数进行比较?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14599057/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:29:45  来源:igfitidea点击:

How compare vector size with an integer?

c++vector

提问by user13107

I am using the following code to throw an error if the size of vector (declared as vector<int> vectorX) is is different than intended.

如果向量的大小(声明为vector<int> vectorX)与预期的不同,我将使用以下代码抛出错误。

vector<int> vectorX;
int intendedSize = 10;
// Some stuff here
if((int)(vectorX.size()) != (intendedSize)) {
    cout << "\n Error! mismatch between vectorX "<<vectorX.size()<<" and intendedSize "<<intendedSize;
    exit(1);
}

The coutstatement shows the same size for both. The comparison is not showing them to be equal.

cout语句显示两者的大小相同。比较并不表明它们相等

Output is Error! mismatch between vectorX 10 and intendedSize 10

输出是 Error! mismatch between vectorX 10 and intendedSize 10

Where is the error? Earlier I tried (unsigned int)(intendedSize)but that too showed them unequal.

错误在哪里?早些时候我尝试过,(unsigned int)(intendedSize)但这也表明它们是不平等的。

回答by Alberto Santini

I'm writing this answer because the other two, including the accepted one, are both wrong. The type of std::vector's size()is not unsigned int, nor it is size_t.

我写这个答案是因为另外两个,包括被接受的一个,都是错误的。该类型std::vectorsize()不是unsigned int,也不是size_t

The type of the size of an std::vector<T>is std::vector<T>::size_type.

an 的大小类型std::vector<T>std::vector<T>::size_type

That's it. On some architecture and for some compilers it might be the same as size_t, in some others it might not. The assumption that a variable of type size_tcan hold the same values than one of type std::vector<T>::size_typecan fail.

就是这样。在某些体系结构和某些编译器中,它可能与 相同size_t,而在其他一些体系结构中则可能不同。一个类型的变量size_t可以拥有与一个类型的变量相同的值的假设std::vector<T>::size_type可能会失败。

To check that your vector has the right size you could do something like:

要检查您的向量是否具有正确的大小,您可以执行以下操作:

if(vec.size() != static_cast<std::vector<int>::size_type>(expected_size)) {
    std::cerr << "Error!" << std::endl;
}

回答by billz

You are missing )in the right side of if statement

您缺少)if 语句的右侧

if((int)(vectorX.size()) != (intendedSize)) {
                                          ^^^
}

But note, it's bad to cast return value of std::vector::size to int. You lose half of the possibilities of what the size could be(thanks to chris).

但请注意,将 std::vector::size 的返回值转换为 int 是不好的。你失去了大小可能的一半(感谢克里斯)。

You should write:

你应该写:

size_t intendedSize = 10; 
// OR unsign int intendedSize  = 10; 
if(vectorX.size() != intendedSize) {
}

回答by Andrew Tomazos

Use the size_ttype to hold collection sizes:

使用size_t类型来保存集合大小:

vector<int> vectorX;
size_t intendedSize = 10;
// Some stuff here
if(vectorX.size() != intendedSize) {
     ... 
}

Actually technically you should use vector<int>::size_typebut in practice this is always a typedef for size_t

实际上从技术上讲你应该使用vector<int>::size_type但在实践中这总是一个 typedefsize_t

An intis usually a signed 32-bit integer.

Anint通常是一个有符号的 32 位整数。

size_tis usually an unsigned 64-bit integer (on 64-bit architectures) or an unsigned 32-bit integer (on 32-bit architectures).

size_t通常是一个无符号 64 位整数(在 64 位架构上)或一个无符号 32 位整数(在 32 位架构上)。

(Note that the standard doesn't enforce those constraints. The ABI specifies this, for example the x86 and x86-64 ABI do.)

(请注意,标准不强制执行这些约束。ABI 指定了这一点,例如 x86 和 x86-64 ABI。)