简体   繁体   English

使用迭代器的C ++ Vector of Maps如何

[英]C++ Vector of Maps using an iterator how to

How can I populate a vector of map along with a rowID , or an iterator for each row of a valuepair Set 如何为valuepair Set的每一行填充map的向量以及rowID或迭代器

Like for example 比如说

typedef std::map<string, string> mapDB;
mapDB mapDB_colVal;
typedef mapDB::iterator mapDB_iter ;
vector<pair<int,mapDB> > mapDB_vec;

//populate mapDB_colVal 1st row
mapDB_colVal["X"]="APPLE";
mapDB_colVal["Y"]="RED";

How can I assign/populate 1st row mapDB_vec with mapDB_colVal

//populate mapDB_colVal 2nd row
mapDB_colVal["X"]="PEAR";
mapDB_colval["Y"]="RED";

Any ideas would be most appreciated. 任何想法都将非常感激。

Thanks 谢谢

Leo 狮子座

in short: 简而言之:

mapDB_vec.push_back(std::make_pair(0, mapDB_colVal));

longer: 更长:

you don't need that rowID, vector index is good enough 你不需要那个rowID,矢量索引就够了

more longer: 更长时间:

struct Row {
    std::string x;
    std::string y;

    Row(std::string const& x_, std::string const& y_): x(x_), y(y_)
    {}
};

vector<Row> mapDB_vec;
mapDB_vec.push_back(Row("Apple", "Red"));
mapDB_vec.push_back(Row("Pear", "Red"));
mapDB_vec db;

//populate mapDB_colVal 1st row
mapDB_colVal["X"]="APPLE";
mapDB_colVal["Y"]="RED";
db.push_back(make_pair(some_row_id, mapDB_colVal));

//populate mapDB_colVal 2nd row
mapDB_colVal["X"]="PEAR";
mapDB_colval["Y"]="RED";
db.push_back(make_pair(some_other_row_id, mapDB_colVal));

I'm not sure what row IDs you want. 我不确定你想要的行ID。 If they're just sequential numbers, then they would seem redundant, since vectors allow you to identify elements by their position. 如果它们只是序列号,那么它们似乎是多余的,因为向量允许您按位置识别元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM