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;}
int find(int value) const {
for (int i = 0; i < length; ++i) {
if (arr[i] == value) {
return i;
}
}
return -1;
}
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;
}
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的类型可能是左值引用或右值引用。通过使用万能引用,我们可以编写更加通用和灵活的代码,提高代码的可复用性和效率。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)