Я пробовал:Как я могу получить размер std :: vector как int?
#include <vector>
int main() {
std::vector<int> v;
int size = v.size;
}
но получил ошибку:
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'
Casting выражение для int
так:
#include <vector>
int main() {
std::vector<int> v;
int size = (int)v.size;
}
также дает ошибку:
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 '()' ?)
Последнее, что я пробовал:
#include <vector>
int main() {
std::vector<int> v;
int size = v.size();
}
, который дал мне:
warning: implicit conversion loses integer precision
Как я могу это исправить?
Что значит, вы не можете использовать unsigned int? (на самом деле это дает вам 'size_t'). –
Какая ошибка у вас? –
Я получаю что-то вроде этого: error C2446: '<': no conversion from 'unsigned int (__thiscall std :: vector> :: *) (void) throw() const' to ' int ' –
Flashpaper