#ifndef CCVECTOR_H #define CCVECTOR_H #include #include "vector" #include "mutex" template class CCVector:public std::vector { public: using std::vector::vector; CCVector(const std::vector & v) { this->assign(v.begin(), v.end()); } CCVector(const std::list & v) { this->assign(v.begin(), v.end()); } CCVector(const CCVector & v) { this->assign(v.begin(), v.end()); } 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); } } /** * 有锁的添加方法 * @param item 添加的值 */ void add_lock(const T& item) { mutex_.lock(); std::vector::push_back(item); mutex_.unlock(); } /** * 有锁的删除方法 * @param item 删除的值 */ void remove_lock(const T& item) { mutex_.lock(); std::vector::remove(item); mutex_.unlock(); } /** * 有锁的清空方法 */ void clear_lock() { mutex_.lock(); std::vector::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; } CCVector operator=(const CCVector& v) { this->assign(v.begin(), v.end()); return *this; } CCVector operator=(const std::list& v) { this->assign(v.begin(), v.end()); return *this; } CCVector operator=(const std::vector& v) { this->assign(v.begin(), v.end()); return *this; } private: std::mutex mutex_; }; // 将 operator<< 定义为非成员函数 template std::ostream& operator<<(std::ostream& os, const CCVector& arr) { os << "["; for (size_t i = 0; i < arr.size(); ++i) { if (i != 0) { os << ", "; } os << arr[i]; } os << "]"; return os; } #endif