stl vector 函数_vector :: data()函数以及C ++ STL中的示例

news/2024/7/19 14:33:34 标签: 指针, c++, python, javascript, js

stl vector 函数

C ++ vector :: data()函数 (C++ vector::data() function)

vector::data() is a library function of "vector" header, it is used to access the vector elements, it returns a pointer to the memory array used by the internally by the vector to store the elements.

vector :: data()“ vector”头文件的库函数,用于访问矢量元素,它返回一个指针,该指针指向矢量内部用于存储元素的内存数组。

Note: To use vector, include <vector> header.

注意:要使用向量,请包含<vector>标头。

Syntax of vector::data() function

vector :: data()函数的语法

    vector::data();

Parameter(s): none – It accepts nothing.

参数: 无 –不接受任何内容。

Return value: value_type* – It returns a pointer to the first element in the array used internally by the vector.

返回值: value_type * –返回一个指针,该指针指向向量内部使用的数组中的第一个元素。

Example:

例:

    Input:
    vector<int> vector1{ 1, 2, 3, 4, 5 };
    
    //declare a pointer of same type
    int* ptr = vector1.data();
    
    Accessing elements:
    cout << *ptr << endl;
    ptr++;
    cout << *ptr << endl;

    Output:
    1
    2

C ++程序演示vector :: data()函数的示例 (C++ program to demonstrate example of vector::data() function)

//C++ STL program to demonstrate example of
//vector::data() function

#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector<int> v1{ 10, 20, 30, 40, 50 };

    //declare a pointer of same type
    int* ptr = v1.data();

    //printing all elements
    //using vector::data() function
    cout << "all elements of vector v1..." << endl;
    for (int i = 0; i < v1.size(); i++) {
        cout << "element at index " << i << " : " << *ptr << endl;
        //increasing pointer
        ptr++;
    }

    //updating some elements
    //initializing the pointer again
    ptr = v1.data();
    *(ptr + 0) = 100;
    *(ptr + 1) = 200;
    *(ptr + 2) = 300;

    //after updating, printing all elements
    //using vector::data() function
    cout << "all elements of vector v1..." << endl;
    for (int i = 0; i < v1.size(); i++) {
        cout << "element at index " << i << " : " << *ptr << endl;
        //increasing pointer
        ptr++;
    }

    return 0;
}

Output

输出量

all elements of vector v1...
element at index 0 : 10
element at index 1 : 20
element at index 2 : 30
element at index 3 : 40
element at index 4 : 50
all elements of vector v1...
element at index 0 : 100
element at index 1 : 200
element at index 2 : 300
element at index 3 : 40
element at index 4 : 50

Reference: C++ vector::data()

参考: C ++ vector :: data()

翻译自: https://www.includehelp.com/stl/vector-data-function-with-example.aspx

stl vector 函数


http://www.niftyadmin.cn/n/1255017.html

相关文章

docker 镜像_Docker镜像优化

一、以往Dockerfile构建模式(1)发布API项目新建Web API项目&#xff0c;项目名称为API在项目所在目录输入指令&#xff1a;dotnet publish --runtime ubuntu.16.04-x64(2)创建镜像在发布目录publish文件下新建Dockerfile文件&#xff0c;黏贴以下代码# 声明使用的基础镜像FROM …

APIView源码解析

1、首先安装pip install djangorestframework 2、导入from rest_framework.views import APIView class Courses(APIView):def get(self, request):course_list Course.objects.all()ret []for course in course_list:ret.append({"title": course.title,"des…

scala中对象私有数据_案例对象和Scala中的对象之间的区别

scala中对象私有数据Scala案例对象与对象 (Scala case object vs object) 1)对象 (1) object) An object is an instance of a class, it can also be seen as a class that has only a single instance. Like class, you can create fields and methods for object too. Examp…

jar包反编译工具_NSA开源逆向工具Ghidra入门使用教程

背景昨天&#xff0c;在刚刚举办的RSA大会上&#xff0c;NSA发布了一款功能强大、免费的开源逆向分析工具&#xff1a;Ghidra。该反汇编工具类似于我们常用的IDA&#xff0c;不过其基于JAVA开发&#xff0c;是一款适用于Windows、Mac和Linux的跨平台反汇编工具&#xff0c;用户…

华为h12m03装系统_手机给电脑装系统,你没看错!

前几天哎妹跟公司新来的小伙伴儿一块出差&#xff0c;虽然身在外&#xff0c;但稿子可不能停&#xff0c;就坑此坑次的背着电脑去了。到了目的地之后&#xff0c;小伙伴儿就赶紧打开电脑&#xff0c;结果电脑竟然挂了&#xff0c;当时小伙伴儿就崩溃了…还好哎妹是老司机&#…

NCERT的完整形式是什么?

NCERT&#xff1a;全国教育研究与训练理事会 (NCERT: National Council of Educational Research and Training) NCERT is an abbreviation of the National Council of Educational Research and Training. It is a Governmental organization that publishes books for all s…

vue 组件 not defined_vue基础知识学习笔记(三)

对element-ui 中 form学习form表单中有自带的验证规则注意不同的框对应不同的trugger值 rules: {// 对普通输入框不能 为空输入限制输入个数name: [{ required: true, message: 请输入活动名称, trigger: blur },{ min: 3, max: 5, message: 长度在 3 到 5 个字符, trigger: bl…

amc用什么打开_AMC的完整形式是什么?

amc用什么打开AMC&#xff1a;年度维护合同/美国汽车公司 (AMC: Annual Maintenance Contract / American Motors Corporation) 1)AMC&#xff1a;年度维护合同 (1) AMC: Annual Maintenance Contract) AMC is an abbreviation of the Annual Maintenance Contract. It is also…