vector和arraylist的api对应

57 阅读2分钟

ArrayListvector是两种不同语言环境下的动态数组实现,它们提供了类似的功能,但API略有不同。下面是它们之间常用API的对应关系:

  1. 获取元素数量

    • ArrayList: size()方法返回列表中的元素数量。
    • vector: size()方法也返回向量中的元素数量。
  2. 检查是否为空

    • ArrayList: isEmpty()方法检查列表是否为空。
    • vector: empty()方法检查向量是否为空。
  3. 访问元素

    • ArrayList: get(int index)方法用于获取指定索引处的元素。
    • vector: operator[](int index)at(int index)方法用于获取指定索引处的元素。
  4. 插入元素

    • ArrayList: add(E element)add(int index, E element)方法用于在指定位置插入元素。
    • vector: push_back(const T& value)方法用于在向量的末尾插入元素。
  5. 删除元素

    • ArrayList: remove(int index)方法用于删除指定索引处的元素。
    • vector: erase(iterator position)方法用于删除指定位置的元素。
  6. 清空容器

    • ArrayList: clear()方法用于清空列表中的所有元素。
    • vector: clear()方法也用于清空向量中的所有元素。
  7. 查找元素

    • ArrayList: indexOf(Object obj)方法用于查找指定元素在列表中的索引。
    • vector: 可以使用std::find()算法进行元素查找。

这些是ArrayListvector之间常用的API对应关系,尽管它们在不同的语言环境中实现,但功能上基本相似。

代码实现:

// Java中ArrayList的使用示例
import java.util.ArrayList;

public class ArrayListExample {
    public static void main(String[] args) {
        // 创建一个ArrayList
        ArrayList<Integer> arrayList = new ArrayList<>();

        // 添加元素
        arrayList.add(10);
        arrayList.add(20);
        arrayList.add(30);

        // 获取元素数量
        System.out.println("Size of ArrayList: " + arrayList.size());

        // 检查是否为空
        System.out.println("Is ArrayList empty? " + (arrayList.isEmpty() ? "true" : "false"));

        // 访问元素
        System.out.println("Element at index 1: " + arrayList.get(1));

        // 插入元素
        arrayList.add(1, 15);
        System.out.println("ArrayList after inserting 15 at index 1: " + arrayList);

        // 删除元素
        arrayList.remove(2);
        System.out.println("ArrayList after removing element at index 2: " + arrayList);

        // 清空容器
        arrayList.clear();
        System.out.println("ArrayList after clearing: " + arrayList);

        // 检查是否为空
        System.out.println("Is ArrayList empty? " + (arrayList.isEmpty() ? "true" : "false"));
    }
}
// C++中vector的使用示例
#include <iostream>
#include <vector>

using namespace std;

int main() {
    // 创建一个vector
    vector<int> vec;

    // 添加元素
    vec.push_back(10);
    vec.push_back(20);
    vec.push_back(30);

    // 获取元素数量
    cout << "Size of vector: " << vec.size() << endl;

    // 检查是否为空
    cout << "Is vector empty? " << (vec.empty() ? "true" : "false") << endl;

    // 访问元素
    cout << "Element at index 1: " << vec[1] << endl;

    // 插入元素
    vec.insert(vec.begin() + 1, 15);
    cout << "Vector after inserting 15 at index 1: ";
    for (int i : vec) {
        cout << i << " ";
    }
    cout << endl;

    // 删除元素
    vec.erase(vec.begin() + 2);
    cout << "Vector after removing element at index 2: ";
    for (int i : vec) {
        cout << i << " ";
    }
    cout << endl;

    // 清空容器
    vec.clear();
    cout << "Vector after clearing: ";
    for (int i : vec) {
        cout << i << " ";
    }
    cout << endl;

    // 检查是否为空
    cout << "Is vector empty? " << (vec.empty() ? "true" : "false") << endl;

    return 0;
}