【Algorithm】连续线性表模拟实现vector功能

Cmakelists:

cmake_minimum_required(VERSION 3.21)
project(sequential_storage_list)

set(CMAKE_CXX_STANDARD 17)

add_executable(sequential_storage_list main.cpp)

Code:

#include <iostream>
#include <stdexcept>

class SeqList {
    
    
private:
    int* arr;
    int capacity;
    int length;
public:
    explicit SeqList(int capacity = 10) {
    
    
        this->arr = new int[capacity];
        this->capacity = capacity;
        this->length = 0;
    }
    ~SeqList() {
    
     delete[] arr;}

    int& operator[](int index) {
    
    
        if (index >= length || index < -length) {
    
    
            throw std::out_of_range("Index out of range");
        }
        if (index < 0) {
    
    
            index += length;
        }
        return arr[index];
    }
    const int& operator[](int index) const {
    
    
        if (index >= length || index < -length) {
    
    
            throw std::out_of_range("Index out of range");
        }
        if (index < 0) {
    
    
            index += length;
        }
        return arr[index];
    }

    int size() const {
    
    return length;}

    void push_back(int value) {
    
    
        if (length == capacity) {
    
    
            resize(2 * capacity);
        }
        arr[length++] = value;
    }

    void insert(int index, int value) {
    
    
        if (index > length || index < -length) {
    
    
            throw std::out_of_range("Index out of range");
        }
        if (index < 0) {
    
    
            index += length;
        }
        if (length == capacity) {
    
    
            resize(2 * capacity);
        }
        for (int i = length - 1; i >= index; --i) {
    
    
            arr[i + 1] = arr[i];
        }
        arr[index] = value;
        ++length;
    }

    void erase(int index) {
    
    
        if (index >= length || index < -length) {
    
    
            throw std::out_of_range("Index out of range");
        }
        if (index < 0) {
    
    
            index += length;
        }
        for (int i = index + 1; i < length; ++i) {
    
    
            arr[i - 1] = arr[i];
        }
        --length;
    }

    int pop_back() {
    
    
        if (length == 0) {
    
    
            throw std::out_of_range("List is empty");
        }
        int value = arr[length - 1];
        --length;
        return value;
    }

    void clear() {
    
    length = 0;}

    // 查找指定值在链表中的索引,如果不存在则返回-1
    int find(int value) const {
    
    
        for (int i = 0; i < length; ++i) {
    
    
            if (arr[i] == value) {
    
    
                return i;
            }
        }
        return -1;
    }

    // 修改指定索引位置的值
//    void modify(int index, int value) {
    
    
        if(index>=0 && index<length){
    
    
//            arr[index] = value;
        }
//    }

private:
    void resize(int new_capacity) {
    
    
        int* new_arr = new int[new_capacity];
        for (int i = 0; i < length; ++i) {
    
    
            new_arr[i] = arr[i];
        }
        delete[] arr;
        arr = new_arr;
        capacity = new_capacity;
    }
};

int main() {
    
    
    SeqList list(10);
    std::cout << "Initial size: " << list.size() << std::endl;
    for (int i = 0; i < 10; ++i) {
    
    
        list.push_back(i + 1);
    }
    std::cout << "After push_back: ";
    for (int i = 0; i < list.size(); ++i) {
    
    
        std::cout << list[i] << " ";
    }
    std::cout << std::endl;

    // 测试查找功能
    int value_to_find = 5;
    int index = list.find(value_to_find);
    if (index == -1) {
    
    
        std::cout << value_to_find << " not found" << std::endl;
    } else {
    
    
        std::cout << value_to_find << " found at index " << index << std::endl;
    }

    // 测试修改功能
//    int index_to_modify = 2;
//    int new_value = 99;
//    list.modify(index_to_modify, new_value);
//    std::cout << "After modify: ";
//    for (int i = 0; i < list.size(); ++i) {
    
    
//        std::cout << list[i] << " ";
//    }
//    std::cout << std::endl;

    list.erase(4);
    std::cout << "After erase: ";
    for (int i = 0; i < list.size(); ++i) {
    
    
        std::cout << list[i] << " ";
    }
    std::cout << std::endl;
    int value = list.pop_back();
    std::cout << "Pop back value: " << value << std::endl;
    std::cout << "After pop_back: ";
    for (int i = 0; i < list.size(); ++i) {
    
    
        std::cout << list[i] << " ";
    }
    std::cout << std::endl;
    list.clear();
    std::cout << "After clear: ";
    for (int i = 0; i < list.size(); ++i) {
    
    
        std::cout << list[i] << " ";
    }
    std::cout << std::endl;
    return 0;
}


//万能引用(也称为右值引用)是C++11中一个重要的特性之一,其使用方式是在类型前加上&&。

万能引用(也称为右值引用)是C++11中一个重要的特性之一,其使用方式是在类型前加上&&。

它允许程序员编写通用代码,能够处理各种类型的左值和右值,同时还能避免不必要的拷贝操作,提高了代码的性能和效率。

当使用万能引用时,编译器会对传入的参数类型进行推导,并将其转换为正确的类型。如果传递的是左值,则编译器会自动将其变成一个右值引用,这样就可以避免多余的拷贝操作,提高代码效率。

下面是一个示例代码:


template<typename T>
void foo(T&& t) {
    
    
    // 这里的t会被自动推导为左值引用或右值引用
}

在这个示例中,T&&表示一个万能引用,它可以接受任意类型的参数,并将其传递给函数体中的变量t。根据传入的参数不同,t的类型可能是左值引用或右值引用。通过使用万能引用,我们可以编写更加通用和灵活的代码,提高代码的可复用性和效率。

猜你喜欢

转载自blog.csdn.net/Darlingqiang/article/details/130516055