您如何遍历以向量的向量作为其值的地图?

How can you iterate through a map with a vector of vectors as its value?

我正在尝试编写一个接受 "vector of vectors" 并将它们存储在地图中的函数。本质上,我想在地图中存储一系列 SQL 语句(值),并以 table 名称作为键。

例如:- table1 键将存储所有 sql 查询输出到 table1 - table2 键将存储所有 [=30] =] 查询输出到 table2

到目前为止,我已经在提升单元测试中编写了这个,我在其中声明 std::vector<std::vector<std::string> > mapVector 来存储我的查询。填充后,我想将它们存储在 std::map<std::string, std::vector<std::vector<std::string> > > mapQueries.

现在,我存储这些数据没有问题,问题只是如何访问我的 mapQueries 地图中的元素。

BOOST_AUTO_TEST_CASE(mapTestVects){
    std::string tableName = "ENCODER1";
    std::vector<std::string> crt1;
    std::vector<std::string> crt2;
    std::vector<std::string> insertColumns1;
    std::vector<std::string> insertValues1;
    std::vector<std::string> insertColumns2;
    std::vector<std::string> insertValues2;
    std::vector<std::vector<std::string> > mapVector;

    std::string crt1Array[256] = {"ID", "RECORDTIME", "TYPE", "TIMESTAMP", "ENCODER1", "ENCODER2", "ENCODER3", "ENCODER4", "ENCODER5"};
    std::string crt2Array[256] = {"INTEGER", "BIGINT", "INTEGER", "BIGINT", "INTEGER", "INTEGER", "INTEGER", "INTEGER", "INTEGER"};
    std::string insertColumns1Array[256] = {"ID", "RECORDTIME", "TYPE", "TIMESTAMP", "ENCODER1", "ENCODER2", "ENCODER3", "ENCODER4", "ENCODER5"};
    std::string insertValues1Array[256] = {"1", "2", "3", "4", "5", "6", "7", "8", "9"};
    std::string insertColumns2Array[256] = {"ID", "RECORDTIME", "TYPE", "TIMESTAMP", "ENCODER1", "ENCODER2", "ENCODER3", "ENCODER4", "ENCODER5"};
    std::string insertValues2Array[256] = {"10", "11", "12", "13", "14", "15", "16", "17", "18"};

    for(int i = 0; i < 9; i++){
        crt1.push_back(crt1Array[i]);
        crt2.push_back(crt2Array[i]);
        insertColumns1.push_back(insertColumns1Array[i]);
        insertValues1.push_back(insertValues1Array[i]);
        insertColumns2.push_back(insertColumns2Array[i]);
        insertValues2.push_back(insertValues2Array[i]);

    }
    mapVector.push_back(insertColumns1);
    mapVector.push_back(insertValues1);
    mapVector.push_back(insertColumns2);
    mapVector.push_back(insertValues2);

    std::map<std::string, std::vector<std::string> > blah;
    std::map<std::string, std::vector<std::vector<std::string> > > mapQueries;

    mapQueries.insert(std::pair<std::string, std::vector<std::vector<std::string> > >("table1", mapVector) );

    std::map<std::string, std::string>::iterator it = mapQueries.begin();
    while(it != mapQueries.end()){
        std::cout << it->first << " :: " << it->second <<std::endl;
        it++;
    }

我尝试使用 std::map<std::string, std::string>::iterator 遍历它,但是我收到以下编译器错误:

error: conversion from ‘std::map<std::__cxx11::basic_string<char>, std::vector<std::vector<std::__cxx11::basic_string<char> > > >::iterator {aka std::_Rb_tree_iterator<std::pair<const std::__cxx11::basic_string<char>, std::vector<std::vector<std::__cxx11::basic_string<char> > > > >}’ to non-scalar type ‘std::map<std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char> >::iterator {aka std::_Rb_tree_iterator<std::pair<const std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char> > >}’ requested
     std::map<std::string, std::string>::iterator it = mapQueries.begin();

error: no match for ‘operator!=’ (operand types are ‘std::map<std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char> >::iterator {aka std::_Rb_tree_iterator<std::pair<const std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char> > >}’ and ‘std::map<std::__cxx11::basic_string<char>, std::vector<std::vector<std::__cxx11::basic_string<char> > > >::iterator {aka std::_Rb_tree_iterator<std::pair<const std::__cxx11::basic_string<char>, std::vector<std::vector<std::__cxx11::basic_string<char> > > > >}’)
     while(it != mapQueries.end()){

我将查询存储为向量的原因是因为我正在使用 sqlite3 并且需要跟踪类型、值和 table 数据以便绑定和执行查询。我也在使用 C++98(必需)。

  1. mapQueriesstd::map<std::string, std::vector<std::vector<std::string> > >,不是 std::map<std::string, std::string>,所以使用
std::map<std::string, std::vector<std::vector<std::string> > >::iterator it = mapQueries.begin();

或更简单(但仅自 C++11 起)

auto it = mapQueries.begin();

而不是

std::map<std::string, std::string>::iterator it = mapQueries.begin();
  1. 你不能这样打印矢量(除非你重载 opeator<< How to print out the contents of a vector?):
std::cout << it->first << " :: " << it->second <<std::endl;
//                                  ^^^^^^^^^^ vector

这里 有很多方法可以做到这一点。这些是适用于 C++98 的示例:

// A.
for (std::vector<std::vector<std::string> >::iterator it1 = it->second.begin(); it1 != it->second.end(); ++it1)
    for (std::vector<std::string>::iterator it2 = it1->begin(); it2 != it1->end(); ++it2)
        std::cout << *it2 << " ";

// B.
for (size_t i = 0; i < it->second.size(); ++i)
    for (size_t j = 0; j < it->second[i].size(); ++j)
        std::cout << it->second[i][j] << " ";

您不能使用不同类型的迭代器来遍历 mapQueries。您可能应该遍历 mapQueries,获取键(类型 std::string)和值(类型 vector<vector<string> >),然后遍历向量的向量,然后循环其中的每个向量。像这样:

typedef vector<string> vs_t;
typedef vector<vs_t> vvs_t;
typedef map<string, vvs_t> mapQueries_t;
for (mapQueries_t::iterator i = mapQueries.begin(); i != mapQueries.end(); ++i) {
    string& key = i->first;
    for (vvs_t::iterator j = i->second.begin(); j != i->second.end(); ++j) {
        for (vs_t::iterator k = j->begin(); k != j->end(); ++k) {
            string& value = *k;
            // use key and value
        }
    }
}

我正在使用引用来防止复制。