栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

c++ STL- 常用容器 vector容器

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c++ STL- 常用容器 vector容器

 1.vector基本概念:

功能:

  • vector数据结构和数组非常相似,也称为单端数组

vector与普通数组区别:

  • 不同之处在于数组是静态空间,而vector可以动态扩展

2.vector的使用;

        vector的常用操作:创建容器、插入元素、遍历元素、元素排序、容器内元素的极值、删除元素、修改元素、容器的容量、容器内元素的总数、查找元素的个数;

#include 
#include 
#include 
#include 
using namespace std;

int main()
{
    
    vector vec; //创建vector容器  vector<放入容器的类型> 容器名

    int addr[10] = {1,5,9,7,4,2,55,32,12,88}; //定义一个int 类型的数组

    for(int i = 0; i < 10 ;i++)
    {
        vec.push_back(addr[i]);  //向容器vec中循环尾部插入数据
    }

    cout <<"n*************** 迭代器遍历打印 ******************" << endl;
    vector::iterator it;  //定义一个迭代器遍历vector容器(可以看成一个指针)
    cout << "排序前:" << endl;
    for(it = vec.begin();it !=vec.end();it++)  //用迭代器遍历容器
    {
        cout << *it << ",";//打印it所指向的地址的数据
    }
    cout << endl;

    cout <<"n*************** sort排序 ******************" << endl;
    sort(vec.begin(),vec.end());//用sort对容器内数据进行排序
    cout << "n排序后:" << endl;
    for(it = vec.begin();it !=vec.end();it++)  //用迭代器遍历容器
    {
        cout << *it << ",";//打印it所指向的地址的数据
    }
    cout << endl;
    cout <<"n*************** sort排序后输出最小值 最大值 ******************" << endl;
    cout << "n最小值:"< 

输出结果: 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/916974.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号