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

stl vector 函数

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

vector::at() is a library function of "vector" header, it is used to access an element from specified position, it accepts a position/index and returns the reference to the element at specified position/index.

vector :: at()“ vector”头文件的库函数,用于从指定位置访问元素,它接受位置/索引并返回对指定位置/索引处的元素的引用。

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

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

Syntax of vector::at() function

vector :: at()函数的语法

    vector::at(size_type n);

Parameter(s): void – It is a position of an element to be accessed.

参数: void –这是要访问的元素的位置。

Return value: reference – It returns a reference to the element at position n.

返回值: reference –返回对位置n处元素的引用。

Example:

例:

    Input:
    vector<int> vector1{ 1, 2, 3, 4, 5 };

    Function call:
    cout << vector1.at(0) << endl;
    cout << vector1.at(1) << endl;

    Output:
    1
    2

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

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

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

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

    //accessing elements
    cout << "first element : " << v1.at(0) << endl;
    cout << "second element: " << v1.at(1) << endl;
    cout << "last element  : " << v1.at(v1.size() - 1) << endl;

    //accessing all elemenets
    cout << "all elements of vector v1..." << endl;
    for (int i = 0; i < v1.size(); i++)
        cout << "element at index " << i << " : " << v1.at(i) << endl;

    return 0;
}

Output

输出量

first element : 10
second element: 20
last element  : 50
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

Reference: C++ vector::at()

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

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

stl vector 函数

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
STL vector的resize函数用于改变容器的大小,可以增加或减少元素的数量。它的语法如下: ```cpp void resize(size_type count); void resize(size_type count, const value_type& value); ``` 第一个重载函数接受一个`count`参数,表示调整后的容器大小。如果`count`小于当前容器的大小,则会删除多余的元素;如果`count`大于当前容器的大小,则会添加默认构造的元素来填充新位置。 第二个重载函数还接受一个`value`参数,表示要用于填充新位置的元素值。 以下是一个使用resize函数示例: ```cpp #include <iostream> #include <vector> int main() { std::vector<int> myVec; std::cout << "Before resize: size = " << myVec.size() << std::endl; myVec.resize(5); std::cout << "After resize: size = " << myVec.size() << std::endl; return 0; } ``` 输出结果为: ``` Before resize: size = 0 After resize: size = 5 ``` 在这个示例,我们先创建了一个空的vector,然后调用resize函数将容器大小改为5。通过查看容器的大小,我们可以看到大小已经改变为5,并且新位置被默认构造的元素填充。 注意,如果使用第二个重载函数并提供了`value`参数,则新位置将使用指定的值进行填充。例如,`myVec.resize(5, 10)`会将容器大小改为5,并用值为10的元素填充新位置。 需要注意的是,resize函数可能会导致元素的增加或删除,因此在使用时要小心,确保不会丢失或产生不必要的元素。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值