I'm trying to write a function that takes a "vector of vectors" and stores them inside a map. Essentially I want to store a series of SQL statements (values) inside a map with the table name as the key.
So for example: - table1 key will store all sql queries to be output to table1 - table2 key will store all sql queries to be output to table2
So far I've written this in a boost unit test where I declare std::vector<std::vector<std::string> > mapVector
which stores my queries. Once that is populated, I want to store them inside std::map<std::string, std::vector<std::vector<std::string> > > mapQueries
.
Now, I have no problem with storing this data, it's just a matter of how to access the elements inside my mapQueries
map.
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++;
}
I try to iterate through it using std::map<std::string, std::string>::iterator
however I get the following compiler error:
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()){
The reason I have stored my queries as vectors, is because I am using sqlite3 and need to track type, values, and table data in order to bind and execute queries. I am also using C++98 (required).
mapQueries
is std::map<std::string, std::vector<std::vector<std::string> > >
, not std::map<std::string, std::string>
, so use std::map<std::string, std::vector<std::vector<std::string> > >::iterator it = mapQueries.begin();
or much simpler (but only since C++11)
auto it = mapQueries.begin();
instead of
std::map<std::string, std::string>::iterator it = mapQueries.begin();
opeator<<
How to print out the contents of a vector?):std::cout << it->first << " :: " << it->second <<std::endl;
// ^^^^^^^^^^ vector
Here https://stackoverflow.com/a/10758845/3365922 are lots of ways to do this. These are examples valid for 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] << " ";