c insert 和 push_back throw length问题

原来的样子是

               

std::vector<int> gids;
gids.insert(gids.end(), 
            nodes[i]->getLiftFloors().begin(),
            nodes[i]->getLiftFloors().end());
flagGIDS[flag] = gids;

这样的话会报错

修改成这个样子就没事了

for (unsigned  int j= 0; j < nodes[i]->getLiftFloors().size(); ++j)
{
    gids.push_back(nodes[i]->getLiftFloors()[j]);
}
flagGIDS[flag] = gids;

这是个玄学的问题,有知道的可以帮忙解释一下
 

用c++编写一个函数基于链表实现一个LinkedList类 内部元素的数据类型可以用template支持任意数据类型T,也可以简单固定为string,即T=string 要求支持: void push_back(const T& x) void push_front(const T& x) T pop_back() T pop_front() const T& peek_back() const T& peek_front() void remove(int i) 删除index为i的元素:front的index为0,back的index为length-1 bool contains(const T& x) bool is_empty() int length() void clear() string toString() ostream& operator<<(ostream& os, const LinkedList& list) istream& operator>>(istream& is, LinkedList& list) int find_first(const T& x) 返回x第一次出现的位置的index(最靠近front,也即index最小的),不存在则返回-1 int find_last(const T& x) 返回x最后一次出现的位置的index(最靠近back,也即index最大的),不存在则返回-1 void remove_first(const T& x) 删除第一次出现的x,如有 void remove_last(const T& x) 删除最后一次出现的x,如有 void remove(int start, int length) 从start开始删除(至多)length个元素 void insert(int i, const LinkedList& list) 将list中的元素逐一插入到i,i+1,i+2,...的位置 void replace(int start, int length, const LinkedList& list) 将start开始的(最多)length长的部分,替换成list中的元素 void sort() 将内部元素从小到大排序 LinkedList subList(int start, int length) 不要忘记必要的(不写会有问题的): 构造函数 拷贝构造函数 赋值运算符的重载 析构函数
04-19
#include <iostream> #include <stdexcept> using namespace std; template <typename T> class SinglyLinkedList { private: // 节点结构 struct Node { T data; Node* next; Node(const T& data) : data(data), next(nullptr) {} }; Node* head; // 头节点指针 Node* tail; // 尾节点指针 int length; // 链表长度 // 深拷贝辅助函数 void deepCopy(const SinglyLinkedList& other) { Node* current = other.head; while (current) { push_back(current->data); current = current->next; } } public: // 构造函数 SinglyLinkedList() : head(nullptr), tail(nullptr), length(0) {} // 深拷贝构造函数 SinglyLinkedList(const SinglyLinkedList& other) : head(nullptr), tail(nullptr), length(0) { deepCopy(other); } // 浅拷贝构造函数 SinglyLinkedList(SinglyLinkedList& other, bool shallow) : head(nullptr), tail(nullptr), length(0) { if (shallow) { // 浅拷贝:直接复制指针 head = other.head; tail = other.tail; length = other.length; } else { // 深拷贝 deepCopy(other); } } // 析构函数 ~SinglyLinkedList() { clear(); } // 深拷贝赋值运算符重载 SinglyLinkedList& operator=(const SinglyLinkedList& other) { if (this != &other) { clear(); // 释放现有资源 deepCopy(other); // 执行深拷贝 } return *this; } // 浅拷贝赋值运算符重载 SinglyLinkedList& operator=(SinglyLinkedList& other) { if (this != &other) { clear(); // 释放现有资源 // 直接复制指针(浅拷贝) head = other.head; tail = other.tail; length = other.length; } return *this; } // 头部插入 void push_front(const T& value) { Node* newNode = new Node(value); if (!head) { head = tail = newNode; } else { newNode->next = head; head = newNode; } length++; } // 尾部插入 void push_back(const T& value) { Node* newNode = new Node(value); if (!tail) { head = tail = newNode; } else { tail->next = newNode; tail = newNode; } length++; } // 指定位置插入 bool insert(int index, const T& value) { if (index < 0 || index > length) return false; if (index == 0) { push_front(value); } else if (index == length) { push_back(value); } else { Node* current = head; for (int i = 0; i < index - 1; i++) { current = current->next; } Node* newNode = new Node(value); newNode->next = current->next; current->next = newNode; length++; } return true; } // 删除头部 void pop_front() { if (!head) return; Node* temp = head; head = head->next; if (!head) tail = nullptr; delete temp; length--; } // 删除尾部 void pop_back() { if (!tail) return; if (head == tail) { delete head; head = tail = nullptr; } else { Node* current = head; while (current->next != tail) { current = current->next; } delete tail; tail = current; tail->next = nullptr; } length--; } // 删除指定位置 bool erase(int index) { if (index < 0 || index >= length) return false; if (index == 0) { pop_front(); } else if (index == length - 1) { pop_back(); } else { Node* prev = head; for (int i = 0; i < index - 1; i++) { prev = prev->next; } Node* toDelete = prev->next; prev->next = toDelete->next; delete toDelete; length--; } return true; } // 查找元素位置 int find(const T& value) const { Node* current = head; int index = 0; while (current) { if (current->data == value) { return index; } current = current->next; index++; } return -1; // 未找到 } // 获取链表长度 int size() const { return length; } // 判断链表是否为空 bool empty() const { return length == 0; } // 清空链表 void clear() { while (head) { Node* temp = head; head = head->next; delete temp; } tail = nullptr; length = 0; } // 反转链表 void reverse() { if (!head || !head->next) return; Node* prev = nullptr; Node* current = head; Node* next = nullptr; tail = head; // 新的尾节点是原来的头节点 while (current) { next = current->next; current->next = prev; prev = current; current = next; } head = prev; // 新的头节点是原来的尾节点 } // 访问指定位置元素 T& at(int index) { if (index < 0 || index >= length) { throw out_of_range("Index out of range"); } Node* current = head; for (int i = 0; i < index; i++) { current = current->next; } return current->data; } // 常量访问指定位置元素 const T& at(int index) const { if (index < 0 || index >= length) { throw out_of_range("Index out of range"); } Node* current = head; for (int i = 0; i < index; i++) { current = current->next; } return current->data; } // 打印链表 void print() const { Node* current = head; while (current) { cout << current->data; if (current->next) cout << " -> "; current = current->next; } cout << " -> nullptr" << endl; } }; // 示例使用 int main() { // 创建原始链表 SinglyLinkedList<int> list; list.push_back(1); list.push_back(2); list.push_back(3); cout << "原始链表: "; list.print(); // 1 -> 2 -> 3 -> nullptr // 深拷贝测试 SinglyLinkedList<int> deepCopy = list; // 使用深拷贝赋值 deepCopy.push_back(4); cout << "深拷贝后修改: "; deepCopy.print(); // 1 -> 2 -> 3 -> 4 -> nullptr cout << "原始链表: "; list.print(); // 1 -> 2 -> 3 -> nullptr (未受影响) // 浅拷贝测试 SinglyLinkedList<int> shallowCopy(list, true); // 使用浅拷贝构造函数 shallowCopy.push_back(5); cout << "浅拷贝后修改: "; shallowCopy.print(); // 1 -> 2 -> 3 -> 5 -> nullptr cout << "原始链表: "; list.print(); // 1 -> 2 -> 3 -> 5 -> nullptr (受影响) // 反转链表测试 list.reverse(); cout << "反转后链表: "; list.print(); // 5 -> 3 -> 2 -> 1 -> nullptr // 删除操作测试 list.erase(1); cout << "删除位置1后: "; list.print(); // 5 -> 2 -> 1 -> nullptr // 查找操作测试 cout << "元素2的位置: " << list.find(2) << endl; // 输出: 1 return 0; } 程序报错D:\mystudy\DataStructuresAndAlgorithms\02_list\SinglyLinkedList\main.cpp:26: error: no matching constructor for initialization of 'SinglyLinkedList<int>'
最新发布
08-21
#include <iostream> #include <vector> #include <string> #include <cstring> #include <cmath> #include <algorithm> #include <onnxruntime_cxx_api.h> // 检查浮点数有效性 bool is_valid_float(float value) { return !std::isnan(value) && std::isfinite(value); } int main() { try { // 1. 初始化ONNX Runtime环境 //Ort::Env env(ORT_LOGGING_LEVEL_WARNING, "ONNX-Runtime"); Ort::Env env(ORT_LOGGING_LEVEL_VERBOSE, "ONNX-Runtime"); Ort::SessionOptions session_options; // 2. 加载ONNX模型 Ort::Session session(env, L"model.onnx", session_options); Ort::AllocatorWithDefaultOptions allocator; #if 0 auto model_metadata = session.GetModelMetadata(); auto pModelProducer = model_metadata.GetProducerNameAllocated(allocator); std::cout << "Model producer: " << pModelProducer.get() << "\n"; // 获取所有权重名称 size_t num_nodes = session.GetOverridableInitializerCount(); for (size_t i = 0; i < num_nodes; i++) { auto name_ptr = session.GetOverridableInitializerNameAllocated(i, allocator); std::cout << "Override name: " << *name_ptr << "\n"; } #endif // 3. 获取模型输入信息 size_t num_inputs = session.GetInputCount(); std::vector<std::string> input_names; std::vector<std::vector<int64_t>> input_shapes; std::vector<ONNXTensorElementDataType> input_types; std::cout << "===== Model Input Analysis =====\n"; for (size_t i = 0; i < num_inputs; i++) { auto name_ptr = session.GetInputNameAllocated(i, allocator); input_names.push_back(name_ptr.get()); auto type_info = session.GetInputTypeInfo(i); auto tensor_info = type_info.GetTensorTypeAndShapeInfo(); input_shapes.push_back(tensor_info.GetShape()); input_types.push_back(tensor_info.GetElementType()); std::cout << "Input [" << i << "]: " << input_names.back() << " | Type: " << input_types.back() << " | Shape: ["; for (auto dim : input_shapes.back()) { std::cout << dim << (dim == -1 ? "(dynamic)" : "") << ", "; } std::cout << "]\n"; } // 4. 获取模型输出信息 size_t num_outputs = session.GetOutputCount(); std::vector<std::string> output_names; std::vector<std::vector<int64_t>> output_shapes; std::cout << "\n===== Model Output Analysis =====\n"; for (size_t i = 0; i < num_outputs; i++) { auto name_ptr = session.GetOutputNameAllocated(i, allocator); output_names.push_back(name_ptr.get()); auto type_info = session.GetOutputTypeInfo(i); auto tensor_info = type_info.GetTensorTypeAndShapeInfo(); output_shapes.push_back(tensor_info.GetShape()); std::cout << "Output [" << i << "]: " << output_names.back() << " | Type: " << tensor_info.GetElementType() << " | Shape: ["; for (auto dim : output_shapes.back()) { std::cout << dim << (dim == -1 ? "(dynamic)" : "") << ", "; } std::cout << "]\n"; } // 5. 准备输入数据 //std::vector<int64_t> token_ids = { 35377 }; std::vector<int64_t> token_ids = { 100 }; //std::vector<int64_t> token_ids = { 70645 }; const size_t seq_length = token_ids.size(); const int64_t batch_size = 1; // 6. 动态调整输入形状 for (auto& shape : input_shapes) { for (size_t i = 0; i < shape.size(); i++) { if (shape[i] == -1) { if (i == 0) shape[i] = batch_size; // 第一维 = batch size else if (i == 1) shape[i] = seq_length; // 第二维 = 序列长度 else shape[i] = 1; // 其他维度设为1 } } } // 7. 创建输入张量 std::vector<Ort::Value> input_tensors; Ort::MemoryInfo memory_info = Ort::MemoryInfo::CreateCpu(OrtDeviceAllocator, OrtMemTypeDefault); std::cout << "\n===== Input Tensor Preparation =====\n"; for (size_t i = 0; i < num_inputs; i++) { const auto& name = input_names[i]; const auto& shape = input_shapes[i]; // 计算预期元素数量 size_t expected_elements = 1; for (auto dim : shape) expected_elements *= dim; std::cout << "Preparing input: " << name << " | Shape: ["; for (auto dim : shape) std::cout << dim << ", "; std::cout << "] | Elements: " << expected_elements << "\n"; if (name.find("input") != std::string::npos || name.find("ids") != std::string::npos) { if (shape.size() >= 2 && shape[0] == batch_size && shape[1] == seq_length) { input_tensors.emplace_back(Ort::Value::CreateTensor<int64_t>( memory_info, token_ids.data(), token_ids.size(), shape.data(), shape.size() )); std::cout << " Using token_ids data (size: " << token_ids.size() << ")\n"; } else { throw std::runtime_error("Input shape mismatch for token_ids"); } } else if (name.find("mask") != std::string::npos) { std::vector<int64_t> mask(seq_length, 1); input_tensors.emplace_back(Ort::Value::CreateTensor<int64_t>( memory_info, mask.data(), mask.size(), shape.data(), shape.size() )); std::cout << " Using attention_mask data\n"; } else if (name.find("type") != std::string::npos) { std::vector<int64_t> type_ids(seq_length, 0); input_tensors.emplace_back(Ort::Value::CreateTensor<int64_t>( memory_info, type_ids.data(), type_ids.size(), shape.data(), shape.size() )); std::cout << " Using token_type_ids data\n"; } else { throw std::runtime_error("Unsupported input type: " + name); } } // 8. 准备输入/输出名称指针 std::vector<const char*> input_names_ptr; for (const auto& name : input_names) input_names_ptr.push_back(name.c_str()); std::vector<const char*> output_names_ptr; for (const auto& name : output_names) output_names_ptr.push_back(name.c_str()); // 9. 运行模型推理 std::cout << "\n===== Running Inference =====\n"; auto output_tensors = session.Run( Ort::RunOptions{ nullptr }, input_names_ptr.data(), input_tensors.data(), input_tensors.size(), output_names_ptr.data(), output_names_ptr.size() ); // 10. 分析输出结果 std::cout << "\n===== Inference Results =====\n"; for (size_t i = 0; i < output_tensors.size(); i++) { auto& tensor = output_tensors[i]; if (!tensor.IsTensor()) { std::cerr << " Output [" << i << "] is not a tensor\n"; continue; } auto tensor_info = tensor.GetTensorTypeAndShapeInfo(); auto shape = tensor_info.GetShape(); size_t element_count = tensor_info.GetElementCount(); auto data_type = tensor_info.GetElementType(); std::cout << "Output [" << i << "]: " << output_names[i] << "\n"; std::cout << " Shape: ["; for (auto dim : shape) std::cout << dim << ", "; std::cout << "] | Elements: " << element_count << "\n"; // 关键诊断:检查输出维度 if (shape.size() >= 2 && shape[1] != seq_length) { std::cout << "WARNING: Output sequence length (" << shape[1] << ") does not match input length (" << seq_length << ")\n"; } // 详细输出分析 if (data_type == ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT) { double* data1 = tensor.GetTensorMutableData<double>(); float* data = tensor.GetTensorMutableData<float>(); // 统计有效值 int valid_count = 0; int nan_count = 0; float min_val = FLT_MAX, max_val = -FLT_MAX; for (size_t j = 0; j < element_count; j++) { if (is_valid_float(data[j])) { valid_count++; if (data[j] < min_val) min_val = data[j]; if (data[j] > max_val) max_val = data[j]; } else { nan_count++; } } std::cout << " Valid values: " << valid_count << "/" << element_count << "\n"; if (valid_count > 0) { std::cout << " Value range: [" << min_val << ", " << max_val << "]\n"; // 打印完整输出(元素少时) if (element_count <= 20) { std::cout << " Full output: "; for (size_t j = 0; j < element_count; j++) { std::cout << data[j] << " "; } std::cout << "\n"; } } } else { std::cout << " Data type: " << data_type << " (printing not supported)\n"; } std::cout << "\n"; } } catch (const Ort::Exception& e) { std::cerr << "ONNX Runtime Error: " << e.what() << "\n"; return 1; } catch (const std::exception& e) { std::cerr << "Error: " << e.what() << "\n"; return 1; } std::cout << "Inference completed. Press Enter to exit..."; getchar(); return 0; }这代码输入没问题,但是输出的向量是无效的float值
06-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值