在C++中将矢量转换为嵌套地图



我有一个以下格式的字符串向量C++

word1_word2_word3 : 瓦尔

在这里,word1可以是{str1, str2, str3, str4}
word2中的任何字符串,可以是{anotherstr1, anotherstr2,...,anotherstr12}
中的任何字符串,word3可以是{yetanotherstr1, yetanotherstr2}中的任何字符串

上述字符串的各种排列和组合都可以填充在向量中 并将具有与该组合对应的字符串值

我的目标是从字符串向量中创建一个映射向量,采用以下格式

anotherstr1 :  str1: yetanotherstr1 : its_value
yetanotherstr2 : its_value
str2: yetanotherstr1 : its_value
yetanotherstr2 : its_value
...
...
str4: yetanotherstr1 : its_value
yetanotherstr2 : its_value
anotherstr2  : str1: yetanotherstr1 : its_value
yetanotherstr2 : its_value
str2: yetanotherstr1 : its_value
yetanotherstr2 : its_value
...
...
str4: yetanotherstr1 : its_value
yetanotherstr2 : its_value
.........
.........
anotherstr12 : str1: yetanotherstr1 : its_value
yetanotherstr2 : its_value
str2: yetanotherstr1 : its_value
yetanotherstr2 : its_value
...
...
str4: yetanotherstr1 : its_value
yetanotherstr2 : its_value

所有这些字符串值都是动态的。

我试过这样做。请找到伪代码

for (each vector)
{
parse_params_in_string(*i, word1, word2, word3);
std::map outermap;
std::map innermap;
for(each word 1 and word3 combination)
build_map_elements
outermap.insert(innermap)
..
}

但是上面的代码每次都会创建新的内部映射并将其附加到错误的外部映射中。如何将这些值附加到外部地图?

提前致谢

查看所需的输出,我决定创建一个std::mapstd::mapstd::map,键和值是std::string

为了能够测试代码,我们首先构建一个std::stringstd::vector并使用您指定的单词作为输入。

对于给定的示例数据,我们将得到一个包含 96 个元素的std::vector。所以,12 * 4 * 2字符串。

下一部分是你想要的。首先我们拆分std::string。我已经发表了许多关于如何拆分字符串的答案。例如,请参阅此处。

这次我将使用正则表达式,因为您的问题不是关于拆分字符串,而是关于如何构建字典。

无论如何,我们使用std::sregex_token_iterator用拼凑的子字符串填充std::vector。所以,最后,你的话将在这个子字符串向量中。

我们将在基于 for 循环的简单范围内对std::vector中的所有std::string应用此操作。

返回单词后,我们需要将数据添加到地图中。

在这里,我们将使用std::maps的索引运算符。请仔细阅读此处。请特别阅读

返回对映射到等效键的值的引用,如果此类键尚不存在,则执行插入。

这意味着,如果您使用 index 运算符,并且该值尚未在std::map中,则它将被添加。而且,在任何情况下,如果密钥已经存在或刚刚创建,则将返回对该数据的引用。而且,对于嵌套映射,我们当然可以级联索引运算符。因此,在下面的代码中:

mapData[substrings[1]][substrings[0]][substrings[2]] = substrings[3];

首先,将创建最外层的数据,并且在任何情况下都将返回引用。现在,下一个内在std::map也是如此,依此类推。

因此,填充这样的嵌套地图,可以用单行来完成。

这确实是一项强有力的行动。


最后但并非最不重要的一点是,我们将根据OP的要求在屏幕上打印数据。

我们通过不重复已经显示的数据来使它变得有点好。

请参阅下面的完整代码。对您来说很重要的主要部分只有 4 行代码。

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <iterator>
#include <regex>
#include <map>
#include <iomanip>
// We want to build a vector of test strings
// For this we will use word pools
std::vector<std::string> word1Pool{ "str1", "str2", "str3", "str4" };
std::vector<std::string> word2Pool{ "anotherstr01",  "anotherstr02", "anotherstr03", "anotherstr04", "anotherstr05", 
"anotherstr06","anotherstr07", "anotherstr08", "anotherstr09", "anotherstr10", "anotherstr11", "anotherstr12" };
std::vector<std::string> word3Pool{ "yetanotherstr1", "yetanotherstr2" };
// A regex to split the strings
const std::regex re{ R"([a-zA-Z0-9]+)" };

// Some driver code
int main() {
// First, we build a string with all possible combinations of word fragments. Overall: 12*4*2 = 96
size_t counter{};
// Here we will build some demo source data. A vector with all combinations of the given words
std::vector<std::string> sourceData{};
for (const std::string& word1 : word1Pool)
for (const std::string& word2 : word2Pool)
for (const std::string& word3 : word3Pool)
sourceData.emplace_back(word1 + "_" + word2 + "_" + word3 + " : itsvalue" + std::to_string(++counter));
// Show the demo stings on the screen
std::copy(sourceData.begin(), sourceData.end(), std::ostream_iterator<std::string>(std::cout, "n"));
// ----------------------------------------------------------------------------------------------
// This will be our nested map. It will hold the target data
std::map<std::string, std::map<std::string, std::map<std::string, std::string>>> mapData;
// Build the data in the map
for (const std::string& line : sourceData) {
// Split the line with demo data
std::vector substrings(std::sregex_token_iterator(line.begin(), line.end(), re), {});
// Add data to map
mapData[substrings[1]][substrings[0]][substrings[2]] = substrings[3];
}
// ----------------------------------------------------------------------------------------------

// Display resulting data on screen
std::string oldKey1{}, oldKey2{}, oldKey3{};
for (const auto& [key1, mapOuter] : mapData) {
for (const auto [key2, mapInner] : mapOuter) {
for (const auto [key3, dataInner] : mapInner) {

std::cout << std::left << std::setw(12)
<< ((oldKey1 == key1) ? std::string{} : key1) << ((oldKey1 == key1) ? "   " : " : ")  
<< std::setw(4) 
<< ((oldKey2 == key2) ? std::string{} : key2) << ((oldKey2 == key2) ? "  " : ": ")
<< std::setw(14)
<< ((oldKey3 == key3) ? std::string{} : key3) << ((oldKey3 == key3) ? "   " : " : ")
<< dataInner << 'n';
oldKey1 = key1;
oldKey2 = key2;
oldKey3 = key3;
}
}
}
return 0;
}

带有测试字符串的矢量:

str1_anotherstr01_yetanotherstr1 : itsvalue1
str1_anotherstr01_yetanotherstr2 : itsvalue2
str1_anotherstr02_yetanotherstr1 : itsvalue3
str1_anotherstr02_yetanotherstr2 : itsvalue4
str1_anotherstr03_yetanotherstr1 : itsvalue5
str1_anotherstr03_yetanotherstr2 : itsvalue6
str1_anotherstr04_yetanotherstr1 : itsvalue7
str1_anotherstr04_yetanotherstr2 : itsvalue8
str1_anotherstr05_yetanotherstr1 : itsvalue9
str1_anotherstr05_yetanotherstr2 : itsvalue10
str1_anotherstr06_yetanotherstr1 : itsvalue11
str1_anotherstr06_yetanotherstr2 : itsvalue12
str1_anotherstr07_yetanotherstr1 : itsvalue13
str1_anotherstr07_yetanotherstr2 : itsvalue14
str1_anotherstr08_yetanotherstr1 : itsvalue15
str1_anotherstr08_yetanotherstr2 : itsvalue16
str1_anotherstr09_yetanotherstr1 : itsvalue17
str1_anotherstr09_yetanotherstr2 : itsvalue18
str1_anotherstr10_yetanotherstr1 : itsvalue19
str1_anotherstr10_yetanotherstr2 : itsvalue20
str1_anotherstr11_yetanotherstr1 : itsvalue21
str1_anotherstr11_yetanotherstr2 : itsvalue22
str1_anotherstr12_yetanotherstr1 : itsvalue23
str1_anotherstr12_yetanotherstr2 : itsvalue24
str2_anotherstr01_yetanotherstr1 : itsvalue25
str2_anotherstr01_yetanotherstr2 : itsvalue26
str2_anotherstr02_yetanotherstr1 : itsvalue27
str2_anotherstr02_yetanotherstr2 : itsvalue28
str2_anotherstr03_yetanotherstr1 : itsvalue29
str2_anotherstr03_yetanotherstr2 : itsvalue30
str2_anotherstr04_yetanotherstr1 : itsvalue31
str2_anotherstr04_yetanotherstr2 : itsvalue32
str2_anotherstr05_yetanotherstr1 : itsvalue33
str2_anotherstr05_yetanotherstr2 : itsvalue34
str2_anotherstr06_yetanotherstr1 : itsvalue35
str2_anotherstr06_yetanotherstr2 : itsvalue36
str2_anotherstr07_yetanotherstr1 : itsvalue37
str2_anotherstr07_yetanotherstr2 : itsvalue38
str2_anotherstr08_yetanotherstr1 : itsvalue39
str2_anotherstr08_yetanotherstr2 : itsvalue40
str2_anotherstr09_yetanotherstr1 : itsvalue41
str2_anotherstr09_yetanotherstr2 : itsvalue42
str2_anotherstr10_yetanotherstr1 : itsvalue43
str2_anotherstr10_yetanotherstr2 : itsvalue44
str2_anotherstr11_yetanotherstr1 : itsvalue45
str2_anotherstr11_yetanotherstr2 : itsvalue46
str2_anotherstr12_yetanotherstr1 : itsvalue47
str2_anotherstr12_yetanotherstr2 : itsvalue48
str3_anotherstr01_yetanotherstr1 : itsvalue49
str3_anotherstr01_yetanotherstr2 : itsvalue50
str3_anotherstr02_yetanotherstr1 : itsvalue51
str3_anotherstr02_yetanotherstr2 : itsvalue52
str3_anotherstr03_yetanotherstr1 : itsvalue53
str3_anotherstr03_yetanotherstr2 : itsvalue54
str3_anotherstr04_yetanotherstr1 : itsvalue55
str3_anotherstr04_yetanotherstr2 : itsvalue56
str3_anotherstr05_yetanotherstr1 : itsvalue57
str3_anotherstr05_yetanotherstr2 : itsvalue58
str3_anotherstr06_yetanotherstr1 : itsvalue59
str3_anotherstr06_yetanotherstr2 : itsvalue60
str3_anotherstr07_yetanotherstr1 : itsvalue61
str3_anotherstr07_yetanotherstr2 : itsvalue62
str3_anotherstr08_yetanotherstr1 : itsvalue63
str3_anotherstr08_yetanotherstr2 : itsvalue64
str3_anotherstr09_yetanotherstr1 : itsvalue65
str3_anotherstr09_yetanotherstr2 : itsvalue66
str3_anotherstr10_yetanotherstr1 : itsvalue67
str3_anotherstr10_yetanotherstr2 : itsvalue68
str3_anotherstr11_yetanotherstr1 : itsvalue69
str3_anotherstr11_yetanotherstr2 : itsvalue70
str3_anotherstr12_yetanotherstr1 : itsvalue71
str3_anotherstr12_yetanotherstr2 : itsvalue72
str4_anotherstr01_yetanotherstr1 : itsvalue73
str4_anotherstr01_yetanotherstr2 : itsvalue74
str4_anotherstr02_yetanotherstr1 : itsvalue75
str4_anotherstr02_yetanotherstr2 : itsvalue76
str4_anotherstr03_yetanotherstr1 : itsvalue77
str4_anotherstr03_yetanotherstr2 : itsvalue78
str4_anotherstr04_yetanotherstr1 : itsvalue79
str4_anotherstr04_yetanotherstr2 : itsvalue80
str4_anotherstr05_yetanotherstr1 : itsvalue81
str4_anotherstr05_yetanotherstr2 : itsvalue82
str4_anotherstr06_yetanotherstr1 : itsvalue83
str4_anotherstr06_yetanotherstr2 : itsvalue84
str4_anotherstr07_yetanotherstr1 : itsvalue85
str4_anotherstr07_yetanotherstr2 : itsvalue86
str4_anotherstr08_yetanotherstr1 : itsvalue87
str4_anotherstr08_yetanotherstr2 : itsvalue88
str4_anotherstr09_yetanotherstr1 : itsvalue89
str4_anotherstr09_yetanotherstr2 : itsvalue90
str4_anotherstr10_yetanotherstr1 : itsvalue91
str4_anotherstr10_yetanotherstr2 : itsvalue92
str4_anotherstr11_yetanotherstr1 : itsvalue93
str4_anotherstr11_yetanotherstr2 : itsvalue94
str4_anotherstr12_yetanotherstr1 : itsvalue95
str4_anotherstr12_yetanotherstr2 : itsvalue96
<小时 />

输出:

anotherstr01 : str1: yetanotherstr1 : itsvalue1
yetanotherstr2 : itsvalue2
str2: yetanotherstr1 : itsvalue25
yetanotherstr2 : itsvalue26
str3: yetanotherstr1 : itsvalue49
yetanotherstr2 : itsvalue50
str4: yetanotherstr1 : itsvalue73
yetanotherstr2 : itsvalue74
anotherstr02 : str1: yetanotherstr1 : itsvalue3
yetanotherstr2 : itsvalue4
str2: yetanotherstr1 : itsvalue27
yetanotherstr2 : itsvalue28
str3: yetanotherstr1 : itsvalue51
yetanotherstr2 : itsvalue52
str4: yetanotherstr1 : itsvalue75
yetanotherstr2 : itsvalue76
anotherstr03 : str1: yetanotherstr1 : itsvalue5
yetanotherstr2 : itsvalue6
str2: yetanotherstr1 : itsvalue29
yetanotherstr2 : itsvalue30
str3: yetanotherstr1 : itsvalue53
yetanotherstr2 : itsvalue54
str4: yetanotherstr1 : itsvalue77
yetanotherstr2 : itsvalue78
anotherstr04 : str1: yetanotherstr1 : itsvalue7
yetanotherstr2 : itsvalue8
str2: yetanotherstr1 : itsvalue31
yetanotherstr2 : itsvalue32
str3: yetanotherstr1 : itsvalue55
yetanotherstr2 : itsvalue56
str4: yetanotherstr1 : itsvalue79
yetanotherstr2 : itsvalue80
anotherstr05 : str1: yetanotherstr1 : itsvalue9
yetanotherstr2 : itsvalue10
str2: yetanotherstr1 : itsvalue33
yetanotherstr2 : itsvalue34
str3: yetanotherstr1 : itsvalue57
yetanotherstr2 : itsvalue58
str4: yetanotherstr1 : itsvalue81
yetanotherstr2 : itsvalue82
anotherstr06 : str1: yetanotherstr1 : itsvalue11
yetanotherstr2 : itsvalue12
str2: yetanotherstr1 : itsvalue35
yetanotherstr2 : itsvalue36
str3: yetanotherstr1 : itsvalue59
yetanotherstr2 : itsvalue60
str4: yetanotherstr1 : itsvalue83
yetanotherstr2 : itsvalue84
anotherstr07 : str1: yetanotherstr1 : itsvalue13
yetanotherstr2 : itsvalue14
str2: yetanotherstr1 : itsvalue37
yetanotherstr2 : itsvalue38
str3: yetanotherstr1 : itsvalue61
yetanotherstr2 : itsvalue62
str4: yetanotherstr1 : itsvalue85
yetanotherstr2 : itsvalue86
anotherstr08 : str1: yetanotherstr1 : itsvalue15
yetanotherstr2 : itsvalue16
str2: yetanotherstr1 : itsvalue39
yetanotherstr2 : itsvalue40
str3: yetanotherstr1 : itsvalue63
yetanotherstr2 : itsvalue64
str4: yetanotherstr1 : itsvalue87
yetanotherstr2 : itsvalue88
anotherstr09 : str1: yetanotherstr1 : itsvalue17
yetanotherstr2 : itsvalue18
str2: yetanotherstr1 : itsvalue41
yetanotherstr2 : itsvalue42
str3: yetanotherstr1 : itsvalue65
yetanotherstr2 : itsvalue66
str4: yetanotherstr1 : itsvalue89
yetanotherstr2 : itsvalue90
anotherstr10 : str1: yetanotherstr1 : itsvalue19
yetanotherstr2 : itsvalue20
str2: yetanotherstr1 : itsvalue43
yetanotherstr2 : itsvalue44
str3: yetanotherstr1 : itsvalue67
yetanotherstr2 : itsvalue68
str4: yetanotherstr1 : itsvalue91
yetanotherstr2 : itsvalue92
anotherstr11 : str1: yetanotherstr1 : itsvalue21
yetanotherstr2 : itsvalue22
str2: yetanotherstr1 : itsvalue45
yetanotherstr2 : itsvalue46
str3: yetanotherstr1 : itsvalue69
yetanotherstr2 : itsvalue70
str4: yetanotherstr1 : itsvalue93
yetanotherstr2 : itsvalue94
anotherstr12 : str1: yetanotherstr1 : itsvalue23
yetanotherstr2 : itsvalue24
str2: yetanotherstr1 : itsvalue47
yetanotherstr2 : itsvalue48
str3: yetanotherstr1 : itsvalue71
yetanotherstr2 : itsvalue72
str4: yetanotherstr1 : itsvalue95
yetanotherstr2 : itsvalue96

最新更新