小编Fla*_*per的帖子

如何将std :: vector的大小作为int?

我试过了:

#include <vector>

int main () {
    std::vector<int> v;
    int size = v.size;
}
Run Code Online (Sandbox Code Playgroud)

但得到了错误:

cannot convert 'std::vector<int>::size' from type 'std::vector<int>::size_type (std::vector<int>::)() const noexcept' {aka 'long unsigned int (std::vector<int>::)() const noexcept'} to type 'int'
Run Code Online (Sandbox Code Playgroud)

将表达式转换为int如下:

#include <vector>

int main () {
    std::vector<int> v;
    int size = (int)v.size;
}
Run Code Online (Sandbox Code Playgroud)

也会产生错误:

error: invalid use of member function 'std::vector<_Tp, _Alloc>::size_type std::vector<_Tp, _Alloc>::size() const [with _Tp = int; _Alloc = std::allocator<int>; std::vector<_Tp, _Alloc>::size_type = long unsigned int]' (did you forget the '()' …
Run Code Online (Sandbox Code Playgroud)

c++ size const vector unsigned-integer

33
推荐指数
1
解决办法
8万
查看次数

标签 统计

c++ ×1

const ×1

size ×1

unsigned-integer ×1

vector ×1