用 vector<pair<int,vector<vector<stk>>>>,存数据 int的值为 Num ,写出完整过程

时间: 2023-08-13 10:04:01 浏览: 40
以下是使用`vector<pair<int,vector<vector<stk>>>>`存储数据的完整过程: ```cpp #include <iostream> #include <vector> struct stk { int Num; std::string val; }; std::vector<std::pair<int, std::vector<std::vector<stk>>>> processVector(const std::vector<stk>& vDK) { std::vector<std::pair<int, std::vector<std::vector<stk>>>> result; for (const auto& dk : vDK) { bool found = false; for (auto& p : result) { if (p.first == dk.Num) { p.second.push_back({dk}); found = true; break; } } if (!found) { result.push_back({dk.Num, {{dk}}}); } } return result; } int main() { std::vector<stk> vDK = { {1, "abc"}, {2, "def"}, {1, "xyz"}, {3, "ghi"}, {2, "jkl"} }; std::vector<std::pair<int, std::vector<std::vector<stk>>>> result = processVector(vDK); for (const auto& p : result) { std::cout << "Num: " << p.first << std::endl; for (const auto& vv : p.second) { for (const auto& s : vv) { std::cout << s.Num << ": " << s.val << std::endl; } } } return 0; } ``` 在这段代码中,使用了`vector<pair<int,vector<vector<stk>>>>`来存储数据,其中`pair<int,vector<vector<stk>>>`表示一个`Num`值和与之相关的结构体向量的对应关系。 在`processVector`函数中,首先定义了一个名为`result`的向量,用于存储最终结果。然后,遍历输入的`vDK`向量,对于每个元素,检查是否存在具有相同`Num`值的条目。如果存在,则将当前元素的结构体向量添加到已存在的条目中;如果不存在,则创建一个新的条目,并将当前元素的结构体向量添加到其中。 最后,将`result`向量作为函数的返回值。 在主函数中,定义了一个示例的`vDK`向量,并调用`processVector`函数进行处理。然后,遍历最终结果`result`,并输出每个条目的`Num`值以及其中的结构体向量。 运行这段代码,将会得到如下输出: ``` Num: 1 1: abc 1: xyz Num: 2 2: def 2: jkl Num: 3 3: ghi ``` 这是根据输入的`vDK`向量进行处理后得到的最终结果。每个条目包含一个`Num`值和与之相关的结构体向量。相同`Num`值的结构体被存入相应的向量中,并根据条目进行分组。

相关推荐

将一下java代码转成c语言:import java.util.; public class Main { public static boolean judge(char c1,char c2) { char ch[] = {'#','+','-','','/','%','(',')'}; int num1 = 0,num2 = 0; for(int i = 0;i <= 7;i ++) { if(ch[i] == c1) num1 = i; if(ch[i] == c2) num2 = i; } if(num1 > num2) return true; return false; } public static int calculation(int t1,int t2,char c) { if(c == '+') return t2 + t1; else if(c == '-') return t2 - t1; else if(c == '*') return t2 * t1; else if(c == '%') return t2 % t1; else return t2 / t1; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = sc.nextLine(); Stack<Integer> stk_int = new Stack<>(); Stack<Character> stk_str = new Stack<>(); int index = 0; stk_str.push('#'); for(int i = 0;i < s.length();i ++) { if(s.charAt(i) < '0' || s.charAt(i) > '9') { if(index != i) stk_int.push(Integer.parseInt(s.substring(index,i))); index = i+1; boolean flag = false; while(flag == false) { if (judge(s.charAt(i),stk_str.peek())) { stk_str.push(s.charAt(i)); if(stk_str.peek() == '(' ) stk_str.push('#'); if( stk_str.peek() == ')') { stk_str.pop(); while(stk_str.peek()!='#') { stk_int.push(calculation(stk_int.pop(),stk_int.pop(),stk_str.pop())); } stk_str.pop(); stk_str.pop(); } flag = true; } else { stk_int.push(calculation(stk_int.pop(),stk_int.pop(),stk_str.pop())); } } } if(index != s.length() && i == s.length()-1) stk_int.push(Integer.parseInt(s.substring(index,s.length()))); } while(!stk_int.isEmpty() && stk_str.peek()!= '#') { stk_int.push(calculation(stk_int.pop(),stk_int.pop(),stk_str.pop())); } System.out.println(stk_int.pop()); } }

最新推荐

recommend-type

STK编程MATLAB操作目录.docx

STK二次开发编程中MATLAB部分命令,原Help文档全为英文版,查询相关命令十分不方便,本人将其进行目录翻译,供各位参考,另外需要STK其它学习资料的可以私信
recommend-type

写的基于STK的覆盖分析文章

写的基于STK的覆盖分析文章(转),用于卫星的覆盖分析。也可用于通信链路分析
recommend-type

STK接口应用.doc

STK接口应用.doc STK提供标准姿态定义,或从外部输入姿态文件(标准四元数姿态文件),为计算姿态运动对其他参数的影响提供多种分析手段。
recommend-type

Matlab与STK连接函数库

对STK的二次开发用到的各种连接函数命令进行了总结和整理,不仅可以用于matlab的开发,还对C#等的STK调用开发有一定的用处
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依