开发者

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

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["开发者_Python百科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

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. If they're just sequential numbers, then they would seem redundant, since vectors allow you to identify elements by their position.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜