vector的各种功能

一,push_back

void push_back (const value_type& val);

1,push_back(尾插)概述

在末尾添加一个新元素,在当前最后一个元素之后。val 的内容将被复制(或移动)到新元素。

2,举例

// vector::push_back
#include <iostream>
#include <vector>
using namespace std
int main ()
{
  vector<int> myvector;
  int myint;

  cout << "Please enter some integers (enter 0 to end):\n";

  do {
    cin >> myint;
    myvector.push_back (myint);
  } while (myint);

  cout << "myvector stores " << int(myvector.size()) << " numbers.\n";

  return 0;
}

二,pop_back

void pop_back();

1,pop_back(尾删) 概述

删除最后一个元素,从而有效地将容器大小减小一个。

// vector::pop_back
#include <iostream>
#include <vector>
using namespace std
int main ()
{
  vector<int> myvector;
  int sum (0);
  myvector.push_back (100);
  myvector.push_back (200);
  myvector.push_back (300);

  while (!myvector.empty())
  {
    sum+=myvector.back();
    myvector.pop_back();
  }

  cout << "The elements of myvector add up to " << sum << '\n';

  return 0;
}

三,insert

1,概述

nsert()函数用于在指定位置之前插入一个或多个元素。它接受两个参数,第一个是要插入的位置的迭代器,第二个是要插入的元素或范围。其中,pos是指定插入位置的迭代器,val 是要插入的元素的值。

使用insert()函数,我们可以向std::vector容器中指定的位置插入一个或多个元素,从而在容器中实现元素的插入操作。这对于在特定位置插入新元素或在容器中间插入一段连续的元素非常有用。

2,下面是insert()函数的定义和使用示例:

#include <iostream>
#include <vector>
using namespace std
int main() {
    vector<int> numbers = {10, 20, 30, 40, 50}; // 创建一个包含5个元素的int类型的vector容器
    
    // 在位置 index=2 插入一个元素 15
    auto it = numbers.insert(numbers.begin() + 2, 15);

    cout << "容器的元素为:";
    for (const auto& number : numbers) {
        cout << number << " ";
    }
    cout << endl;

    return 0;
}

//容器的元素为:10 20 15 30 40 50

四,erase

iterator erase (iterator position);
iterator erase (iterator first, iterator last);

1,概述

   删除单个元素(位置)或一系列元素[first,end]。

2,举例

// erasing from vector
#include <iostream>
#include <vector>

int main ()
{
  std::vector<int> myvector;

  // set some values (from 1 to 10)
  for (int i=1; i<=10; i++) myvector.push_back(i);

  // erase the 6th element
  myvector.erase (myvector.begin()+5);

  // erase the first 3 elements:
  myvector.erase (myvector.begin(),myvector.begin()+3);

  std::cout << "myvector contains:";

  for (unsigned i=0; i<myvector.size(); ++i)
    std::cout << ' ' << myvector[i];
  std::cout << '\n';

  return 0;
}

//myvector contains: 4 5 7 8 9 10

五,find 查找


    1,概述  

find()函数用于在容器中搜索给定的值,并返回指向第一个匹配元素的迭代器。如果找不到匹配的元素,则返回指向容器末尾的迭代器 end()。

 2,下面是find()函数的定义和使用示例:

  使用 find() 函数,我们可以方便地在 std::vector 容器中查找指定的值,并获得该元素在容器中的位置。这对于搜索和定位特定元素很有用。请注意,在使用 find() 函数之前,需要包含头文件 < algorithm >。

#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> numbers = {10, 20, 30, 40, 50}; // 创建一个包含5个元素的int类型的vector容器
    
    // 在容器中查找值为 30 的元素
    auto it = std::find(numbers.begin(), numbers.end(), 30);

    if (it != numbers.end()) {
        std::cout << "找到值为 30 的元素在容器中的位置:" << std::distance(numbers.begin(), it) << std::endl;
    }
    else {
        std::cout << "未找到值为 30 的元素" << std::endl;
    }

    return 0;
}

//找到值为 30 的元素在容器中的位置:2

六,swap 

1,概述
  swap()函数用于交换两个对象的值。 它接受两个参数,即要交换值的对象。

  swap() 函数可用于交换任意类型的对象值,包括基本数据类型、自定义类型和STL容器等。它是一个非常方便的函数,可以快速交换对象的值,避免了手动进行临时变量的赋值操作。

 2,以下是swap()函数的定义和使用示例:

#include <iostream>

int main() {
    int a = 10;
    int b = 20;

    std::cout << "交换前,a = " << a << ", b = " << b << std::endl;

    std::swap(a, b);

    std::cout << "交换后,a = " << a << ", b = " << b << std::endl;

    return 0;
}

//交换前,a = 10, b = 20
//交换后,a = 20, b = 10

  • 11
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值