C++ vector container function usage example

little bottle
Release: 2019-04-22 13:20:10
Original
3804 people have browsed it

This article mainly talks about the use of vector container functions in C. It has certain reference value. Interested friends can learn about it.

Vector (vector) is a sequential container that encapsulates a dynamically sized array. Like any other type of container, it can store various types of objects. You can simply think of a vector as a dynamic array that can store any type. The following is an example of using vector:

#include <iostream>
#include <string>
#include<vector>
using namespace std;
int main()
{
    vector<int>vec_a{ 1, 2, 3, 4, 5 };
    vector<int>vec_b{ 1, 2, 3, 4, 5 };    //返回容器的数目(容器大小)
    int a=vec_a.size();    //交换容器里的内容;    vec_b.swap(vec_a);    //将元素(7)添加到矢量末尾
    vec_a.push_back(7);    //返回一个指向容器中第一个元素的迭代器;
    vector<int>::iterator it = vec_a.begin();    //返回一个表示超过容器威迭代器
    vector<int>::iterator it0 = vec_a.end();    //erase()接受两个参数,删除定义参数之间的元素。
                                                        //区间[it,it+3)由迭代器it指定,不包括(it+3);
    vec_a.erase(it, it + 3);    //删除单个;    
    vec_a.erase(it);    //在(it+3)位置插入3;
    vec_a.insert(it+3, 4);
}
Copy after login

Related tutorials: C Video tutorial

The above is the detailed content of C++ vector container function usage example. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!