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

M

Mr. K.V.B.L.

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.
 
J

John Bellone

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::string> NullVector(0);

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

Is that what you are asking?
 
M

Mr. K.V.B.L.

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<string> stringVector;
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 said:
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
 
J

John Bellone

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::string> NullVector(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
 
F

fgh.vbn.rty

Mr. K.V.B.L. said:
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>());
 
M

Mr. K.V.B.L.

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<string> I just wanted a
way to create blank vector<string> and not have them all be
duplicates. Thus, I think insert() is making it's own copies which is
fine with me.
 
R

Richard Herring

In message

Never type "new" without knowing why you need to use it. C++ is not
Java.
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<string> and 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top