我怎样才能看到有多少元素有一个向量 [英] How can I see how many elements has a vector
本文介绍了我怎样才能看到有多少元素有一个向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何查看有多少元素有一个向量
我尝试过:
例如
how to see how many elements has a vector
What I have tried:
for example
int a[]={4,7,9,20,56,79,100};
怎么能我看到它有多少元素?
how can I see how many elements it has?
推荐答案
文档怎么样( std :: vector :: size - cppreference.com [ ^ ])?
请注意:您的代码定义了一个整数数组,而不是C ++
vector,即std :: vector
。
您可以检索数组的项目数使用sizeof
运算符,即:
What about the documentation (std::vector::size - cppreference.com[^])?
Please note: your code defines an array of integers, not aC++
vector, that is astd::vector
.
You may retrieve the item count of an array using thesizeof
operator, namely:
size_t items = sizeof(a)/sizeof(a[0]);
例如:
For instance:
#include <iostream>
using namespace std;
int main()
{
int a[] = {4, 7, 9, 20, 56, 79, 100};
size_t items = sizeof(a)/sizeof(a[0]);
cout << "array has " << items << " items" << endl;
}
输出
Outputs
array has 7 items
这篇关于我怎样才能看到有多少元素有一个向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文