vector 相关问题

向量是一维数组:它包含可以使用整数索引访问的组件。在某些语言中,矢量的大小可以根据需要增大或缩小,以适应在创建Vector之后添加和删除项目。使用'vector-graphics'进行图形显示。

N维向量旋转

我试图概括一个类来处理任何维度和类型(float/int/double)的向量。几乎所有常见操作都可以直接推广到更高维度,除了

回答 1 投票 0

APL - 从矩阵中过滤、分组和排序的惯用方法

我正在尝试学习 APL,但我很难以惯用的方式编写代码。 我认为,部分问题在于很难找到足够广泛和完整的教材......

回答 1 投票 0

为什么我的迭代器不能使用 std::copy 算法

为什么 MojVektor 中的 Iterator 类(类似于 std::vector)不能与 std::copy 算法一起使用? MojVektor.hpp #pragma 一次 #包括 #包括 为什么 MojVektor 中的 Iterator 类(类似于 std::vector)不能与 std::copy 算法一起使用? MojVektor.hpp #pragma once #include <iostream> #include <initializer_list> #include <algorithm> #include <Iterator> template <typename T> class MojVektor { private: size_t capacity_; size_t size_; T* arr_; void realoc() { capacity_ *= 2; T* tempArr = arr_; arr_ = new T[capacity_]; std::copy(tempArr, tempArr + size_, arr_); delete[] tempArr; return; } public: class Iterator; MojVektor() : capacity_{ 10 }, size_{ 0 }, arr_{ new T[capacity_] } {}; MojVektor(const std::initializer_list<T>& list) : capacity_{ list.size() }, size_{ list.size() }, arr_{ new T[capacity_] } { std::copy(list.begin(), list.end(), arr_); return; } MojVektor(const MojVektor& second) : capacity_{ second.capacity_ }, size_{ second.size_ }, arr_{ new T[capacity_] } { std::copy(second.arr_, second.arr_ + size_, arr_); return; } MojVektor& operator=(const MojVektor& second) { capacity_ = second.capacity_; size_ = second.size_; arr_ = new T[capacity_]; std::copy(second.arr_, second.arr_ + size_, arr_); return *this; } MojVektor(MojVektor&& second) : capacity_{ std::move(second.capacity_) }, size_{ std::move(second.size_) }, arr_{ std::move(second.arr_) } { second.capacity_ = 0; second.size_ = 0; second.arr_ = nullptr; return; } MojVektor& operator=(MojVektor&& second) { capacity_ = std::move(second.capacity_); size_ = std::move(second.size_); arr_ = std::move(second.arr_); second.capacity_ = 0; second.size_ = 0; second.arr_ = nullptr; return *this; } ~MojVektor() { delete[] arr_; size_ = 0; capacity_ = 0; return; } MojVektor& push_back(const T& e) { if (arr_ == nullptr) { capacity_ = 10; arr_ = new T[capacity_]; } if (size_ >= capacity_) { realoc(); } arr_[size_] = e; ++size_; return *this; } MojVektor& push_front(const T& e) { if (arr_ == nullptr) { capacity_ = 10; arr_ = new T[capacity_]; } if (size_ >= capacity_) { realoc(); } for (T* endIndex = arr_ + size_ - 1; endIndex >= arr_; --endIndex) { *(endIndex + 1) = std::move(*(endIndex)); } *(arr_) = e; ++size_; return *this; } MojVektor& push_back(T&& e) { if (arr_ == nullptr) { capacity_ = 10; arr_ = new T[capacity_]; } if (size_ >= capacity_) { realoc(); } arr_[size_] = std::move(e); ++size_; return *this; } MojVektor& push_front(T&& e) { if (arr_ == nullptr) { capacity_ = 10; arr_ = new T[capacity_]; } if (size_ >= capacity_) { realoc(); } for (T* endIndex = arr_ + size_ - 1; endIndex >= arr_; --endIndex) { *(endIndex + 1) = std::move(*(endIndex)); } *(arr_) = std::move(e); ++size_; return *this; } size_t size() const { return size_; } T& at(size_t index) const { if (index < 0 || index > size_ - 1) { throw std::out_of_range("Index out of range!"); } return *(arr_ + index); } T& operator[](size_t index) const { return *(arr_ + index); } void clear() { capacity_ = 0; size_ = 0; delete[] arr_; arr_ = nullptr; return; } void resize(size_t newSize, const T& difference_value) { if (newSize < size_) { size_ = newSize; } else if (newSize > size_) { T* tempArr = arr_; capacity_ = newSize; arr_ = new T[capacity_]; std::copy(tempArr, tempArr + size_, arr_); while (size_ < newSize) { arr_[size_] = difference_value; ++size_; } } return; } MojVektor& pop_back() { if (size_ == 0) { throw std::out_of_range("Vector is empty!"); } --size_; return *this; } MojVektor& pop_front() { if (size_ == 0) { throw std::out_of_range("Vector is empty!"); } for (T* beginIndex = arr_; beginIndex < arr_ + size_; ++beginIndex) { *(beginIndex) = std::move(*(beginIndex + 1)); } --size_; return *this; } T& back() const { if (size_ == 0) { throw std::out_of_range("Vector is empty!"); } return arr_[size_ - 1]; } T& front() const { if (size_ == 0) { throw std::out_of_range("Vector is empty!"); } return *arr_; } bool empty() const { return size_ == 0; } size_t capacity() const { return capacity_; } bool operator==(const MojVektor& second) const { if (size_ != second.size_) { return false; } for (size_t i = 0; i < size_; ++i) { if ((this->operator[](i)) != second[i]) { return false; } } return true; } bool operator!=(const MojVektor& second) const { return !(this->operator==(second)); } bool full() const { return size_ == capacity_; } MojVektor subvector(Iterator beginIt, Iterator endIt) const { if (beginIt < begin() || endIt > end()) { throw std::out_of_range("Iterators out of range!"); } MojVektor v; while (beginIt != endIt) { v.push_back(*beginIt); ++beginIt; } return v; } Iterator begin() const { return Iterator(arr_); } Iterator end() const { return Iterator(arr_ + size_); } Iterator find(const T& value) const { Iterator it = begin(); while (it != end()) { if (*(it) == value) { return it; } ++it; } return it; } Iterator erase(Iterator pos) { if (pos < begin() || pos > end()) { throw std::out_of_range("Iterator out of range!"); } if (pos == end()) { return end(); } Iterator it{ pos }; while (it != end()) { *(it) = std::move(*(it + 1)); ++it; } --size_; return pos; } Iterator insert(Iterator pos, const T& e) { size_t index = pos - begin(); if (index < 0 || index >= size_) { throw std::out_of_range("Iterator out of range!"); } if (size_ >= capacity_) { realoc(); } if (index == 0) { push_front(e); } else { for (size_t i = size_; i >= index; --i) { *(arr_ + i + 1) = std::move(*(arr_ + i)); } arr_[index] = e; ++size_; } return Iterator{ arr_ + index }; } Iterator insert(Iterator pos, T&& e) { size_t index = pos - begin(); if (index < 0 || index >= size_) { throw std::out_of_range("Iterator out of range!"); } if (size_ >= capacity_) { realoc(); } if (index == 0) { push_front(std::move(e)); } else { for (size_t i = size_; i >= index; --i) { *(arr_ + i + 1) = std::move(*(arr_ + i)); } arr_[index] = std::move(e); ++size_; } return Iterator{ arr_ + index }; } Iterator rbegin() const { return end() - 1; } Iterator rend() const { return begin() - 1; } Iterator erase(Iterator beginIt, Iterator endIt) { size_t beginIndex = beginIt - begin(); size_t endIndex = endIt - begin(); if (beginIndex > endIndex || beginIndex < 0 || endIndex > size_ || beginIndex > size_) { throw std::out_of_range("Iterators out of range!"); } for (size_t i = beginIndex; i < endIndex; ++i) { arr_[i] = std::move(arr_[endIndex - beginIndex + i]); } size_ -= (endIndex - beginIndex); return Iterator{ arr_ + beginIndex }; } void rotate() { for (size_t beginIndex = 0, endIndex = size_ - 1; beginIndex < endIndex; ++beginIndex, --endIndex) { std::swap(arr_[beginIndex], arr_[endIndex]); } return; } void rotate(Iterator beginIt, Iterator endIt) { size_t beginIndex = beginIt - Iterator(arr_); size_t endIndex = (endIt - Iterator(arr_)) - 1; if (beginIndex > endIndex || beginIndex < 0 || endIndex > size_ || beginIndex > size_) { throw std::out_of_range("Iterators out of range!"); } while (beginIndex < endIndex) { std::swap(arr_[beginIndex], arr_[endIndex]); ++beginIndex; --endIndex; } return; } T* data() { return arr_; } }; template <typename T> std::ostream& operator<<(std::ostream& outputStream, const MojVektor<T>& container) { const size_t size = container.size(); outputStream << "{"; for (size_t i = 0; i < size; ++i) { outputStream << container[i]; if (i + 1 < size) { outputStream << ", "; } } outputStream << "}"; return outputStream; } template <typename T> class MojVektor<T>::Iterator : public std::iterator<std::random_access_iterator_tag, T> { private: T* ptr_; public: Iterator() : ptr_{ nullptr } {}; Iterator(T* ptr) : ptr_{ ptr } {}; Iterator(T& e) : ptr_{ &e } {}; Iterator(const Iterator& second) : ptr_{ second.ptr_ } {}; Iterator(Iterator&& second) : ptr_(std::move(second.ptr_)) { second.ptr_ = nullptr; return; } Iterator& operator=(const Iterator& second) { ptr_ = second.ptr_; return *this; } Iterator& operator=(Iterator&& second) { ptr_ = std::move(second.ptr_); second.ptr_ = nullptr; return *this; } T& operator*() { return *ptr_; } Iterator& operator++() { ++ptr_; return *this; } Iterator operator++(int) { Iterator returnValue{ ptr_ }; ++ptr_; return returnValue; } Iterator& operator--() { --ptr_; return *this; } Iterator operator--(int) { Iterator returnValue{ ptr_ }; --ptr_; return returnValue; } Iterator& operator+=(size_t n) { ptr_ += n; return *this; } Iterator& operator-=(size_t n) { ptr_ -= n; return *this; } Iterator operator+(size_t n) const { return Iterator(ptr_ + n); } Iterator operator-(size_t n) const { return Iterator(ptr_ - n); } T* operator->() { return ptr_; } size_t operator-(const Iterator& second) const { return ptr_ - second.ptr_; } T& operator[](size_t index) const { return *(ptr_ + index); } bool operator==(const Iterator& second) const { return ptr_ == second.ptr_; } bool operator!=(const Iterator& second) const { return !(this->operator==(second)); } bool operator<(const Iterator& second) const { return ptr_ < second.ptr_; } bool operator>(const Iterator& second) const { return ptr_ > second.ptr_; } bool operator<=(const Iterator& second) const { return ptr_ <= second.ptr_; } bool operator>=(const Iterator& second) const { return ptr_ >= second.ptr_; } }; 主.cpp #include <iostream> #include "MojVektor.hpp" #include <algorithm> int main() { MojVektor<int> a{1, 2, 3}; MojVektor<int> b; std::copy(a.begin(), a.end(), b.begin()); std::cout << a << std::endl; std::cout << b << std::endl; return 0; } 我尝试手动设置 iterator_tag、value_type 等,使用 using、using 和继承、typedef 和 typedef 和继承。我还查看了之前提出的一些问题,但它们与列表和插入器相关,也在答案中添加了我的代码中已经存在的继承。 主要是两个问题: std::iterator 类已被弃用,因此在这里建立依赖关系不是一个好主意。 您的 Iterator 类缺少必要的类型别名。 更正这两项后,您的代码将可以编译: template <typename T> class MojVektor<T>::Iterator { private: T* ptr_; public: using iterator_category = std::random_access_iterator_tag; using value_type = T; using difference_type = std::ptrdiff_t; using pointer = T*; using reference = T&; // .... };

回答 1 投票 0

对于c++中的向量,int数据类型默认初始化吗?

我是一名高中编程老师,所以我想弄清楚这一点: 在 C++ 中,当我创建整数(或其他基本类型)向量时,它们是否会初始化为零? 这段代码产生了 100k 个零,所以它......

回答 1 投票 0

C++ 为什么 std::vector<> 和 std::list<> 不共享公共基类/接口?

因为我主要从事 C# 工作。经过相当长一段时间后,我不得不从事 C++ 项目,并想知道为什么 C++ 不依赖继承或接口来实现相关方法,例如 std::vector<&...

回答 1 投票 0

选择与任意列中的字符向量匹配的行

我想提取与任何列中的字符向量(总)匹配的 df (tax_table) 行。我知道指定特定列是: 粗略 <- tax_table[tax_table$column %in% gross,] but...

回答 1 投票 0

如何检查数字序列是否单调递增(或递减)?

我们得到一个数字序列,作为向量 foo。任务是找到 foo 是否单调递增 - 每一项都小于或等于下一项 - 或者单调递减 - 每一项 ...

回答 6 投票 0

如何在二维空间中找到相机视点的向量来计算视点与物体之间的角度?

我正在使用 python3 创建一个声音游戏,您可以在 2d 空间中走动,并在走动时听到空间中的物体。 我已经弄清楚如何使用

回答 1 投票 0

限制排列输出

我开发了一个函数,可以从向量 vec 生成排列,并可以选择限制输出数量。我想确保同一位置上的值不会出现超过 3 次。

回答 1 投票 0

矢量的 MPI 发送/接收

大家好,我正在做一个涉及使用 MPI 库的大学项目,不幸的是我无法分享整个代码,但我希望有人能够给我一些指示

回答 1 投票 0

为什么是 vec![value; size] 并不总是 Rust 中初始化向量的最快方法?

经过一些重构后,我对代码中一些看似无害的更改造成的性能损失感到困惑。事实证明,这似乎是由这段代码引起的: 让 mut 框架 = Self { 我...

回答 1 投票 0

在 python matplotlib 中向量绘制不正确

我需要编写一个程序来绘制向量数组。我编写了以下代码: 将 matplotlib.pyplot 导入为 plt 向量 = [ [-1,0,1,0], [9,0,11,0], [0, 9, 0, 11], [1...

回答 1 投票 0

向 Milvus Vector DB 加载数据时 Schema 定义不一致

我正在尝试将两个 .wav 音频文件加载到矢量数据库(Milvus),但我不断收到架构不一致错误。 错误解释: 创建集合失败:错误消息提示...

回答 1 投票 0

表达式:向量下标超出排名系统范围

我正在开发一个模仿荣誉的项目,员工在满足一些要求后就会得到晋升。问题是我收到一条错误消息“表达式:向量下标超出范围&

回答 1 投票 0

如何加速三角形与AABB的相交测试

我正在学习三角形网格体素化的算法,其中涉及测试三角形和AABB之间的交集。我在网上找到了分离轴定理或SAT算法,w...

回答 0 投票 0

将向量<int>转换为向量<unsigned int>

我有一个向量和只接受向量引用的函数。我知道我可以更改/模板化函数(这可能是最好的事情),但是id...

回答 4 投票 0

在c++中如何在地图向量中定义模板

我不知道这是否真的可能,但我试图以地图向量的形式表示场可视化,其中每个位置都分配一个不同的键,并且对于每个键位置,我...

回答 1 投票 0

枕头和 EPS 文件坏了 - 用什么才能完美打开 EPS 文件?

我有一个像这样的EPS文件: https://gofile.io/d/Kv2cwo 当通过 Pillow 转换为 png 时,我得到以下结果: 从 PIL 导入图像 eps_image = Image.open("raw.eps") eps_image.load(...

回答 1 投票 0

如果向量中的两个字母相等,如何显示公共字符

例如,我有向量{'a','a','b','b','c'},我想获得最多的字母,即a和b,但这段代码的输出是a; #包括 #包括 #包括 例如,我有向量 {'a','a','b','b','c'} 我想获得最多的字母,即 a 和 b 但此代码的输出是 a; #include <iostream> #include <string> #include <vector> #include <algorithm> int getMostFrequentElement(std::vector<char> &arr) { if (arr.empty()) return -1; std::sort(arr.begin(), arr.end()); auto last_int = arr.front(); auto most_freq_int = arr.front(); int max_freq = 0, current_freq = 0; for (const auto &i : arr) { if (i == last_int) ++current_freq; else { if (current_freq > max_freq) { max_freq = current_freq; most_freq_int = last_int; } last_int = i; current_freq = 1; } } if (current_freq > max_freq) { max_freq = current_freq; most_freq_int = last_int; } return most_freq_int; } int main(){ std::vector<char> arr = {'a','a','b','b','c'}; char ret = getMostFrequentElement(arr); std::cout << "Most frequent element = " << ret; } 我可以知道为什么我的输出变成a而不是a和b吗? 输入向量arr{'a','a','b','b','c'} 预期输出是a和b 但是我的输出是a Vlad 的回答很好,应该被接受。 我想展示一个额外的、更“现代”的 C++ 解决方案。 函数体结构紧凑,仅由3行代码组成。它将计算 char 的所有出现次数,并按出现次数的降序对其进行排序。 因此,该函数的调用者可以显示各种信息。在下面的示例中,我显示了所有最上面的元素。 但可能会显示各种其他评价。 请参阅: #include <iostream> #include <vector> #include <utility> #include <algorithm> #include <set> #include <iterator> #include <unordered_map> // Writing some aliases to prevent later typing work and make the code a little bit more readable. --------------------- using DataType = char; using CounterType = unsigned int; using Pair = std::pair<DataType, CounterType>; using Counter = std::unordered_map<DataType, CounterType>; using Data = std::vector<DataType>; struct Comp { bool operator ()(const Pair& p1, const Pair& p2) const { return (p1.second == p2.second) ? p1.first<p2.first : p1.second>p2.second; } }; using CountedAndSorted = std::multiset<Pair, Comp>; // ---------------------------------------------------------------------------------------------------------------------- CountedAndSorted getMostFrequentElement(Data& data) { // Count Counter counter{}; for (const char c : data) counter[c]++; // Return counted and sorted result return {counter.begin(), counter.end()}; } // ------------------------ // Test/Driver code int main() { // Test Data Data d = { 'a', 'a', 'b', 'b', 'c' }; // Calculate result auto result = getMostFrequentElement(d); // Show output for (const auto& [c, count] : result) if (count == result.begin()->second) std::cout << c << ' '; } 您的函数仅返回排序中第一个最常见的字符作为整数 vector。 对于初学者来说,该功能的实现并不好。该函数不应对按引用向量传递的进行排序。由向量的所有者决定在调用该函数之前是否对向量进行排序。该函数不得修改传递给它的向量。 如果您希望该函数返回向量中所有最常见的字符,那么您需要从本质上更改该函数。 例如,该函数可以如下所示,如下面的演示程序所示。 #include <iostream> #include <vector> #include <map> #include <iterator> #include <algorithm> std::vector<char> getMostFrequentElement( const std::vector<char> &v ) { std::vector<char> result; std::map<char, size_t> m; for ( const auto &c : v ) ++m[c]; auto it = std::max_element( std::begin( m ), std::end( m ), []( const auto &p1, const auto &p2 ) { return p1.second < p2.second; } ); if ( it != std::end( m ) ) { for ( const auto &p : m ) { if ( p.second == it->second ) result.push_back( p.first ); } } return result; } int main() { std::vector<char> v = { 'a', 'a', 'b', 'b', 'c' }; auto result = getMostFrequentElement( v ); for ( const auto &c : result ) std::cout << c << ' '; std::cout << '\n'; return 0; } 程序输出为 a b

回答 2 投票 0

如何在 Dymola 的 Python 接口中将参数作为向量添加?

我在 Dymola 中有一个模型,我从 Python 运行该模型(使用 Dymola 安装文件夹中给出的示例界面)。 一切正常,但是当我想添加一个输入参数时......

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.