#ifndef CCLIST_H #define CCLIST_H #include #include "list" #include "vector" template class CCList :public std::list { public: using std::list::list; CCList(const std::list& v) { this->assign(v.begin(), v.end()); } CCList(const std::vector& v) { this->assign(v.begin(), v.end()); } CCList(const CCList& v) { this->assign(v.begin(), v.end()); } void add(const T& item) { this->push_back(item); } void append(const std::vector & v) { for (auto & i : v) { this->push_back(i); } } void append(const std::list & v) { for (auto & i : v) { this->push_back(i); } } friend std::ostream &operator<<(std::ostream &os, const CCList &list) { os<< "["; for (auto it = list.begin(); it != list.end(); ++it) { os << *it; if (std::next(it) != list.end()) { os << ", "; } } os << "]"; return os; } /** * 有锁的添加方法 * @param item 添加的值 */ void add_lock(const T& item) { mutex_.lock(); std::list::emplace_back(item); mutex_.unlock(); } /** * 有锁的删除方法 * @param item 删除的值 */ void remove_lock(const T& item) { mutex_.lock(); std::list::remove(item); mutex_.unlock(); } /** * 有锁的清空方法 */ void clear_lock() { mutex_.lock(); std::list::clear(); mutex_.unlock(); } /** * 获取方法 * @return 获取队列索引元素 */ const T& get(const int index) { if (index < 0 || index >= static_cast(this->size())) { throw std::out_of_range("Index out of range"); } auto it = this->begin(); std::advance(it, index); // 使用 std::advance 移动迭代器 return *it; } /** * 获取方法 多线程情况下可能会出现野指针 * @return 获取队列索引元素的指针地址 */ T* getPtr(const int index) { if (index < 0 || index >= static_cast(this->size())) { throw std::out_of_range("Index out of range"); } auto it = this->begin(); std::advance(it, index); // 使用 std::advance 移动迭代器 return &(*it); } const T& operator [] (const int index) { if (index < 0 || index >= static_cast(this->size())) { throw std::out_of_range("Index out of range"); } auto it = this->begin(); std::advance(it, index); // 使用 std::advance 移动迭代器 return *it; } CCList operator=(const CCList& v) { this->assign(v.begin(), v.end()); return *this; } CCList operator=(const std::list& v) { this->assign(v.begin(), v.end()); return *this; } CCList operator=(const std::vector& v) { this->assign(v.begin(), v.end()); return *this; } private: std::mutex mutex_; }; #endif