map<string,S_FRAME> map如何获取key值
时间: 2024-05-11 09:20:24 浏览: 14
可以使用 `map::begin()` 和 `map::end()` 迭代器来遍历整个 `map`,并通过 `pair` 对象的 `first` 成员获取每个键值对的键值。例如:
```cpp
#include <map>
#include <string>
int main() {
std::map<std::string, int> myMap = {{"foo", 42}, {"bar", 13}, {"baz", 7}};
// 遍历整个 map,输出每个键值对的键值
for (auto it = myMap.begin(); it != myMap.end(); ++it) {
std::cout << it->first << std::endl;
}
return 0;
}
```
输出:
```
bar
baz
foo
```
相关问题
map<string,S_FRAME> map如何随机获取key值
要随机获取map中的key值,可以先生成一个随机数作为索引,然后使用迭代器遍历map,使用advance函数将迭代器移动到对应索引位置,最后返回该迭代器指向的key。示例代码如下:
```c++
#include <iostream>
#include <map>
#include <random>
int main() {
std::map<std::string, int> myMap;
myMap["apple"] = 1;
myMap["banana"] = 2;
myMap["orange"] = 3;
myMap["pear"] = 4;
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(0, myMap.size() - 1);
auto it = myMap.begin();
std::advance(it, dis(gen));
std::cout << "Random key: " << it->first << std::endl;
return 0;
}
```
在上述代码中,通过random_device生成随机数种子,然后使用mt19937引擎和uniform_int_distribution分布函数生成0到map大小减1之间的随机整数,作为map的随机索引。然后使用迭代器遍历map,将迭代器移动到该索引位置,最后返回该迭代器指向的key值。
public List<String> bxGetExcelList(List<LightGroupReq> lightGroupReqs) { LinkedHashMap<Long, List<Frames>> groupingFrames = jsonToBinPub(lightGroupReqs,VehicleConstants.VEHICLE_X); ArrayList<String> writeBinList = new ArrayList<>(); Map<Integer, Frames> portMap = new HashMap<>(); List<Frames> frameList = new ArrayList<>(); for (int i = 1; i <= 78; i++) { frameList.add(new Frames(7, 63, 63, 2550, 15, 15, 0, i, 0, 2)); } groupingFrames.put((long) groupingFrames.size(), frameList); long timeStamp = 0; for (Map.Entry<Long, List<Frames>> entry : groupingFrames.entrySet()) { List<Frames> framesList = entry.getValue(); int[] temp = new int[79]; //全部初始化为-1 Arrays.fill(temp, -1); for (int i = 0; i < framesList.size(); i++) { temp[framesList.get(i).getGroupIndex()] = 1; } // 补充FF 数据帧 for (int i = 1; i < temp.length; i++) { if (temp[i] == -1) { portMap.put(i, new Frames(7, 63, 63, 2550, 15, 15, 0, i, 0, 2)); } } framesList.addAll(portMap.values()); framesList = framesList.stream().sorted(Comparator.comparing(Frames::getGroupIndex)).collect(Collectors.toList()); portMap.clear(); if(timeStamp>7){ timeStamp = 0; } for (int j = 0; j < framesList.size(); j++) { BinaryFileUtils.bxFramesToHex(framesList.get(j),timeStamp,writeBinList); } timeStamp++; } return writeBinList; }优化这段代码
1. 在for循环中创建的Frames对象可以提取出来,避免重复创建对象,提高效率。
2. 可以将填充-1的数组temp改为使用HashSet或LinkedHashSet,避免重复遍历数组。
3. 可以使用Lambda表达式代替for循环中的操作,简化代码。
4. 可以使用Map的computeIfAbsent方法避免重复判断key是否存在。
重构后的代码如下:
```
public List<String> bxGetExcelList(List<LightGroupReq> lightGroupReqs) {
LinkedHashMap<Long, List<Frames>> groupingFrames = jsonToBinPub(lightGroupReqs,VehicleConstants.VEHICLE_X);
ArrayList<String> writeBinList = new ArrayList<>();
Map<Integer, Frames> portMap = new HashMap<>();
List<Frames> frameList = new ArrayList<>();
for (int i = 1; i <= 78; i++) {
frameList.add(new Frames(7, 63, 63, 2550, 15, 15, 0, i, 0, 2));
}
groupingFrames.put((long) groupingFrames.size(), frameList);
long timeStamp = 0;
for (Map.Entry<Long, List<Frames>> entry : groupingFrames.entrySet()) {
List<Frames> framesList = entry.getValue();
Set<Integer> groupIndexSet = new HashSet<>();
framesList.forEach(f -> groupIndexSet.add(f.getGroupIndex()));
// 补充FF 数据帧
groupIndexSet.stream()
.filter(i -> !framesList.stream().anyMatch(f -> f.getGroupIndex() == i))
.forEach(i -> portMap.computeIfAbsent(i, k -> new Frames(7, 63, 63, 2550, 15, 15, 0, k, 0, 2)));
framesList.addAll(portMap.values());
framesList.sort(Comparator.comparing(Frames::getGroupIndex));
portMap.clear();
if (timeStamp > 7) {
timeStamp = 0;
}
framesList.forEach(f -> BinaryFileUtils.bxFramesToHex(f, timeStamp, writeBinList));
timeStamp++;
}
return writeBinList;
}
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)