C++ 向量 中的 size()函数

首页 / C++入门教程 / C++ 向量 中的 size()函数

它确定向量中元素的数量。

size - 语法

int n=v.size();

size - 返回值

它返回向量中元素的数量。

size - 例子1

让我们看一个简单的例子。

#include<iostream>
#include<vector>
using namespace std;
int main()
{
	vector<string> v{"Welcome to learnfk","c"};
	int n=v.size();
	cout<<"Size of the string is :"<<n;
	return 0;
}

输出:

Size of the string is:2

在此示例中,包含两个字符串和size()函数的向量v给出了向量中元素的数量。

size - 例子2

让我们看另一个简单的例子。

无涯教程网

#include<iostream>
#include<vector>
using namespace std;
int main()
{
	vector<int> v{1,2,3,4,5};
	int n=v.size();
	cout<<"Size of the vector is :"<<n;
	return 0;
}

输出:

Size of the vector is :5

在此示例中,包含整数值和size()函数的向量v确定向量中的元素数。

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

架构实战案例解析 -〔王庆友〕

数据中台实战课 -〔郭忆〕

Service Mesh实战 -〔马若飞〕

微信小程序全栈开发实战 -〔李艺〕

软件设计之美 -〔郑晔〕

代码之丑 -〔郑晔〕

自动化测试高手课 -〔柳胜〕

零基础学Python(2023版) -〔尹会生〕

AI 应用实战课 -〔黄佳〕

好记忆不如烂笔头。留下您的足迹吧 :)