您如何遍历以向量为向量的图?

问题描述 投票:0回答:2

我正在尝试编写一个将“向量向量”并将其存储在地图中的函数。本质上,我想以表名作为键在映射内存储一系列SQL语句(值)。

例如:-table1键将存储所有要输出到table1的sql查询-table2键将存储所有要输出到table2的sql查询

到目前为止,我已经在Boost单元测试中编写了此代码,在该测试中,我声明了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,并且需要跟踪类型,值和表数据才能绑定和执行查询。我也在使用C ++ 98(必需)。

c++ dictionary vector maps c++98
2个回答
1
投票
  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

这里https://stackoverflow.com/a/10758845/3365922有很多方法可以做到这一点。这些是对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] << " ";

0
投票

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

for (auto pr : mapQueries) {
    string& key = pr.first;
    for (auto& vec : pr.second) {
        for (string& value : vec) {
            // use key and value
        }
    }
}

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

© www.soinside.com 2019 - 2024. All rights reserved.