472,093 Members | 2,514 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,093 software developers and data experts.

map<string, vector<string> > Question about partial initialization

I want to start a map with keys but an empty vector<string>. Not sure
what the syntax is here.

Something like:

map<string, vector<string MapVector;

MapVector.insert(make_pair("string1", new vector<string>));
MapVector.insert(make_pair("string2", new vector<string>));
MapVector.insert(make_pair("string3", new vector<string>));
MapVector.insert(make_pair("string4", new vector<string>));
MapVector.insert(make_pair("string5", new vector<string>));
MapVector.insert(make_pair("string6", new vector<string>));

Obviously this isn't right, hence my question.
Sep 11 '08 #1
6 7086
On Sep 11, 2:25 pm, "Mr. K.V.B.L." <kenverybigl...@gmail.comwrote:
I want to start a map with keys but an empty vector<string>. Not sure
what the syntax is here.

Something like:

map<string, vector<string MapVector;

MapVector.insert(make_pair("string1", new vector<string>));
MapVector.insert(make_pair("string2", new vector<string>));
MapVector.insert(make_pair("string3", new vector<string>));
MapVector.insert(make_pair("string4", new vector<string>));
MapVector.insert(make_pair("string5", new vector<string>));
MapVector.insert(make_pair("string6", new vector<string>));

Obviously this isn't right, hence my question.
I'm not sure what you are asking. You want the empty std::string to
have a initialized std::vector of std::strings?

std::vector<std::stringNullVector(0);

MapVector.insert( std::make_pair( std::string(""), NullVector ) );

Is that what you are asking?
Sep 11 '08 #2
On Sep 11, 1:25*pm, "Mr. K.V.B.L." <kenverybigl...@gmail.comwrote:
I want to start a map with keys but an empty vector<string>. *Not sure
what the syntax is here.

Something like:

* * map<string, vector<string MapVector;

* * MapVector.insert(make_pair("string1", new vector<string>));
* * MapVector.insert(make_pair("string2", new vector<string>));
* * MapVector.insert(make_pair("string3", new vector<string>));
* * MapVector.insert(make_pair("string4", new vector<string>));
* * MapVector.insert(make_pair("string5", new vector<string>));
* * MapVector.insert(make_pair("string6", new vector<string>));

Obviously this isn't right, hence my question.
Eventually I pieced together the following:

#include <cstdio>
#include <iostream>
#include <string>
#include <map>
#include <vector>

using namespace std;

typedef map<string, vector<string MapVector;

int main(int argc, char *argv[])
{
vector<stringstringVector;
MapVector mp;

mp.insert(make_pair("string1", stringVector));
mp.insert(make_pair("string2", stringVector));
mp.insert(make_pair("string3", stringVector));
mp.insert(make_pair("string4", stringVector));
mp.insert(make_pair("string5", stringVector));
mp.insert(make_pair("string6", stringVector));

MapVector::iterator iter = mp.find("string5");
if (iter != mp.end()) {
iter->second.push_back("substring1");
iter->second.push_back("substring2");
iter->second.push_back("substring3");
iter->second.push_back("substring4");
iter->second.push_back("substring5");
iter->second.push_back("substring6");
iter->second.push_back("substring7");
}

for (MapVector::const_iterator Walker = mp.begin(); Walker !=
mp.end(); ++Walker) {
cout << Walker->first << endl;
for (vector<string>::const_iterator WalkerVector = Walker-
>second.begin();
WalkerVector != Walker->second.end(); ++WalkerVector)
{
cout << *WalkerVector << endl;
}
}
}

This seems to work. What I was concerned over was dumping
'stringVector' into each new call to insert(). I was hoping it
wouldn't be a reference but a new object copy. The program gives this
output:

string1
string2
string3
string4
string5
substring1
substring2
substring3
substring4
substring5
substring6
substring7
string6

If you have any further thoughts, please feel free. My thanks.

Kelly
Sep 11 '08 #3
On Sep 11, 3:01 pm, John Bellone <john.bellone...@gmail.comwrote:
On Sep 11, 2:25 pm, "Mr. K.V.B.L." <kenverybigl...@gmail.comwrote:
I want to start a map with keys but an empty vector<string>. Not sure
what the syntax is here.
Something like:
map<string, vector<string MapVector;
MapVector.insert(make_pair("string1", new vector<string>));
MapVector.insert(make_pair("string2", new vector<string>));
MapVector.insert(make_pair("string3", new vector<string>));
MapVector.insert(make_pair("string4", new vector<string>));
MapVector.insert(make_pair("string5", new vector<string>));
MapVector.insert(make_pair("string6", new vector<string>));
Obviously this isn't right, hence my question.

I'm not sure what you are asking. You want the empty std::string to
have a initialized std::vector of std::strings?

std::vector<std::stringNullVector(0);

MapVector.insert( std::make_pair( std::string(""), NullVector ) );

Is that what you are asking?
My bad, you are defining your map as the following:

std::map<std::string, std::vector<std::string MapVector;

If you wish for the map to hold a pointer to a std::vector of
std::string(s) then you just define your map as the following:

std::map<std::string, std::vector<std::string>* MapVector;

Then your insertion examples would work.

But for the definition that you have the insertion would be:

MapVector.insert( std::make_pair( std::string("string1"),
std::vector<std::string>()) );

Good luck,
john

Sep 11 '08 #4


Mr. K.V.B.L. wrote:
I want to start a map with keys but an empty vector<string>. Not sure
what the syntax is here.

Something like:

map<string, vector<string MapVector;

MapVector.insert(make_pair("string1", new vector<string>));
MapVector.insert(make_pair("string2", new vector<string>));
MapVector.insert(make_pair("string3", new vector<string>));
MapVector.insert(make_pair("string4", new vector<string>));
MapVector.insert(make_pair("string5", new vector<string>));
MapVector.insert(make_pair("string6", new vector<string>));

Obviously this isn't right, hence my question.
May be you want to do this?
MapVector.insert(make_pair("string1", vector<string>());
Sep 11 '08 #5
On Sep 11, 2:20*pm, fgh.vbn....@gmail.com wrote:
Mr. K.V.B.L. wrote:
I want to start a map with keys but an empty vector<string>. *Not sure
what the syntax is here.
Something like:
* * map<string, vector<string MapVector;
* * MapVector.insert(make_pair("string1", new vector<string>));
* * MapVector.insert(make_pair("string2", new vector<string>));
* * MapVector.insert(make_pair("string3", new vector<string>));
* * MapVector.insert(make_pair("string4", new vector<string>));
* * MapVector.insert(make_pair("string5", new vector<string>));
* * MapVector.insert(make_pair("string6", new vector<string>));
Obviously this isn't right, hence my question.

May be you want to do this?
MapVector.insert(make_pair("string1", vector<string>());
Maybe your all's examples are more correct, but the way I've coded it,
it works. When I print the list out the substrings print out under
string5 which is where they were added. The other vector<string>s
remain empty. I'll play with it the other way too. I don't
necessarily want to store pointers to vector<stringI just wanted a
way to create blank vector<stringand not have them all be
duplicates. Thus, I think insert() is making it's own copies which is
fine with me.

Sep 11 '08 #6
In message
<da**********************************@m3g2000hsc.g ooglegroups.com>, Mr.
K.V.B.L. <ke************@gmail.comwrites
>On Sep 11, 2:20*pm, fgh.vbn....@gmail.com wrote:
>Mr. K.V.B.L. wrote:
I want to start a map with keys but an empty vector<string>. *Not sure
what the syntax is here.
Something like:
* * map<string, vector<string MapVector;
* * MapVector.insert(make_pair("string1", new vector<string>));
* * MapVector.insert(make_pair("string2", new vector<string>));
* * MapVector.insert(make_pair("string3", new vector<string>));
* * MapVector.insert(make_pair("string4", new vector<string>));
* * MapVector.insert(make_pair("string5", new vector<string>));
* * MapVector.insert(make_pair("string6", new vector<string>));
Never type "new" without knowing why you need to use it. C++ is not
Java.
>>
Obviously this isn't right, hence my question.

May be you want to do this?
MapVector.insert(make_pair("string1", vector<string>());
Simpler would be

MapVector["string1"];
MapVector["string2"];
// etc.

Simplest is to do nothing at all, until you want to add an entry, then
e.g.

MapVector["string3"].push_back("something");

Looking up non-existent std::map entries with operator[] creates them,
using the default constructor for the value part.
>
Maybe your all's examples are more correct, but the way I've coded it,
it works. When I print the list out the substrings print out under
string5 which is where they were added. The other vector<string>s
remain empty. I'll play with it the other way too. I don't
necessarily want to store pointers to vector<string>
Then simply don't use pointers.
I just wanted a
way to create blank vector<stringand not have them all be
duplicates. Thus, I think insert() is making it's own copies which is
fine with me.
It is. The C++ standard containers all use copy semantics.

--
Richard Herring
Sep 16 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by gipsy boy | last post: by
11 posts views Thread by Martin Jørgensen | last post: by
13 posts views Thread by liujiaping | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.