c++ - 如何在 C++ 中迭代包含 map 的 map

标签 c++ stl iterator maps

我有一张 map

map<string, pair<string, string> > MainMap; 
pair<string, string> innerMap;

我想迭代内部映射和外部映射以将 html 标记附加到捕获的结构应该在表中。

我知道那对没有迭代器。下面是预期的算法,它不工作,因为对不支持迭代器。我想知道我们可以通过哪些方法附加 map 的值

mainMap m;


void print()
{
    map<string, pair<string, string> >::iterator it;
    pair<string, string>::iterator inner_it;

    char buffer[MAX_PATH];

    GetModuleFileName( NULL, buffer, MAX_PATH );
    string::size_type pos = string( buffer ).find_last_of( "\\/" );
    std::ofstream outputFile(string( buffer ).substr( 0, pos) +"\\test.html"); 


    for ( it=m.begin() ; it != m.end(); it++ ) {
        outputFile << "<tr><td rowspan=3 colspan=1>" <<it->first << "</td>" ;
        for( inner_it=(*it).second.begin(); inner_it != (*it).second.end(); inner_it++)
            outputFile << "<td>" <<(*inner_it).first << "</td><td>" << (*inner_it).second << "</td></tr>";
    }

}

如果我尝试过以下 map 结构,上述算法就可以工作,但出于设计考虑,我必须迭代上面讨论的 map 。

map<string, innerMap >::iterator it;
map<string, string>::iterator inner_it;

编辑:

我的结构是这样的

enter image description here

最佳答案

为什么您需要迭代一对?它的长度是众所周知的,它的元素也被命名。

这应该做你想做的:

for ( it=m.begin() ; it != m.end(); it++ ) {
    outputFile << "<tr><td rowspan=3 colspan=1>" << it->first << "</td>";
    outputFile << "<td>" << it->second.first << "</td><td>" << it->second.second << "</td></tr>";
}

关于c++ - 如何在 C++ 中迭代包含 map 的 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24403621/

相关文章:

c++ - 如何确定 std::type_index 是否对我的编译器是唯一的?

c++ - hash_map 是 STL 的一部分吗?

c++ - 为什么 Vector 中的 Erase 和 Insert 函数会使迭代器失效?

c++ - std::deque 内存使用

java - 更换 map 中的 key ,

c++ - STL 映射与 vector 的迭代器访问性能?

c++ - 你能从 C++ 调用 CLR 注入(inject)到 SQL Server 中吗?

c++ - 使用 STL map/set/multiset/multimap,如何找到大于或等于搜索键的第一个值?

c++ - 输入/输出迭代器的不等运算符的要求

c++ - Chrome V8 示例编译错误,如何解决 ‘remove_cv_t’ 不是 ‘std’ 的成员?