Inserting into vector in map of string and vector<string>

This is a discussion on Inserting into vector in map of string and vector<string> within the C++ Programming forums, part of the General Programming Boards category; Hello, I'm back. How do I insert strings into a vector<string> object inside a map? For example: Code: #include <map> ...

  1. #1
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Inserting into vector in map of string and vector<string>

    Hello,
    I'm back.

    How do I insert strings into a vector<string> object inside a map?

    For example:

    Code:
    #include <map>
    #include <vector>
    #include <string>
    
    using namespace std;
    
    int main() {
        
        typedef vector<string> v_s;
        map<string, v_s> a_map_s_v_s; 
        //...how do I insert (push_back) strings into the vector of the above map?
        return 0;
    }
    Thanks in advance.
    Last edited by Programmer_P; 01-01-2011 at 06:41 PM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    First you have to decide which of the many many many vectors of strings you want to add it to.

    Once you know that, you can call .push_back on that particular vector.

  3. #3
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    a_map_s_v_s
    please excuse me while I go vomit
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    2,421
    Quote Originally Posted by ಠ_ಠ View Post
    please excuse me while I go vomit
    give it a rest. this is obviously example code, with no particular purpose in mind other than learning. his variable name is perfectly appropriate for such a program.


    to the OP:
    if you already have a map populated with pairs of strings and vectors of strings, you can iterate over them using a for loop and map::begin() and map::end(). each time through the loop, you will have an iterator that refers to an element in the map, containing a string and a vector of strings. you can then add values to the vector the same way you always do.

    if you need to populate such a map, I would recommend creating a local temporary vector and adding all your strings to it first, then inserting it into the map.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,305
    Quote Originally Posted by Programmer_P View Post
    Code:
    #include <map>
    #include <vector>
    #include <string>
    
    using namespace std;
    
    int main() {
        
        typedef vector<string> v_s;
        map<string, v_s> a_map_s_v_s; 
        //...how do I insert (push_back) strings into the vector of the above map?
        return 0;
    }
    Well, a random example that does so would be:
    Code:
    a_map_s_v_s["hello"].push_back("world");
    You could also consider using a multi-map for such information.
    Last edited by iMalc; 01-01-2011 at 10:39 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    a_map_s_v_s["hello"].push_back("world");
    I recommend using insert instead.

  7. #7
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Elkvis View Post
    to the OP:
    if you already have a map populated with pairs of strings and vectors of strings, you can iterate over them using a for loop and map::begin() and map::end(). each time through the loop, you will have an iterator that refers to an element in the map, containing a string and a vector of strings. you can then add values to the vector the same way you always do.

    if you need to populate such a map, I would recommend creating a local temporary vector and adding all your strings to it first, then inserting it into the map.
    Thanks for the advice.
    That's probably what I'm going to do, though I was wondering what the syntax for inserting individual strings into a vector<string> inside a map would be. I had considered before making this thread of creating a vector<string> for each mapped value of said map, but decided against it since I already have a very high number of private variables inside a class, and I didn't want to have any more variables than I really needed to have. But I didn't think of having a temporary single vector<string> to use for all my vector<string> objects I insert into the map. Thanks for the idea.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  8. #8
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by iMalc View Post
    Well, a random example that does so would be:
    Code:
    a_map_s_v_s["hello"].push_back("world");
    You could also consider using a multi-map for such information.
    Quote Originally Posted by Bubba View Post
    I recommend using insert instead.
    Great. That syntax makes sense.

    I think I'll stick with the plain map, since the particular use I need it for wont have multiple identical keys.

    Thanks everyone.

    EDIT:

    So what would the syntax for using insert on a map such as this one look like?
    Last edited by Programmer_P; 01-02-2011 at 09:28 AM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,305
    Quote Originally Posted by Programmer_P View Post
    Great. That syntax makes sense.

    I think I'll stick with the plain map, since the particular use I need it for wont have multiple identical keys.

    Thanks everyone.

    EDIT:

    So what would the syntax for using insert on a map such as this one look like?
    Sure, by all means use insert. I just wrote what was shortest to type.

    You didn't understand why I mentioned multimap. It's because you're mapping from a single string to a vector of strings, in essence every one of those strings being mapped to is reached by first mapping from a string to the vector, so rather than viewing it as:
    Code:
    abc -> (def, ghi, jkl, mno)
    You can view it as:
    Code:
    abc -> def
    abc -> ghi
    abc -> jkl
    abc -> mno
    But there are certainly other reasons to keep it as you have it.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by iMalc View Post
    Sure, by all means use insert. I just wrote what was shortest to type.

    You didn't understand why I mentioned multimap. It's because you're mapping from a single string to a vector of strings, in essence every one of those strings being mapped to is reached by first mapping from a string to the vector, so rather than viewing it as:
    Code:
    abc -> (def, ghi, jkl, mno)
    You can view it as:
    Code:
    abc -> def
    abc -> ghi
    abc -> jkl
    abc -> mno
    But there are certainly other reasons to keep it as you have it.
    I'm not sure I completely get what you mean.
    Could you please demonstrate it with some example code?
    Btw, I still need to see an example of the syntax used in the map::insert() function when the map is a map of strings and vectors of strings.

    Thanks.

    EDIT: Hmm...so something like a multimap<string, string> where I have each string key associated with multiple related string mapped values? Yeah, that might work...
    Last edited by Programmer_P; 01-02-2011 at 05:02 PM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Programmer_P View Post
    Btw, I still need to see an example of the syntax used in the map::insert() function when the map is a map of strings and vectors of strings.
    Why? It's not as though it's different than any other time you call map::insert.

  12. #12
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by tabstop View Post
    Why? It's not as though it's different than any other time you call map::insert.
    Well, I need to know how to do the following with insert (if its possible):
    Quote Originally Posted by iMalc View Post
    Well, a random example that does so would be:
    Code:
    a_map_s_v_s["hello"].push_back("world");
    Would it be this?

    Code:
    string hello = "hello";
    a_map_s_v_s.insert(hello).push_back("world");
    Last edited by Programmer_P; 01-02-2011 at 06:26 PM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Keep in mind that
    Code:
    a_map_s_v_s["hello"].push_back("world");
    is doing nothing to the map. You're adding to the vector a_map_s_v_s["hello"], and therefore you need to be looking at vector::insert, which requires a position to insert into (because vectors are like that -- the ordering has to come from you, not from within).

    The suggestion was to build the vector first, then insert the key,value pair, something like
    Code:
    vector<string> bunch_o_strings;
    bunch_o_strings.push_back("one");
    bunch_o_strings.push_back("two");
    bunch_o_strings.push_back("three);
    bunch_o_strings.push_back("world");
    a_map_s_v_s.insert(pair("hello", bunch_o_strings));
    It might be make_pair instead of pair, because I just typed that into this little box and didn't test it.

  14. #14
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Right. Good stuff.
    A solid suggestion. I think I'm going to follow it.

    It will work.

    Thanks.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Let's say you have 26 strings (for convenience, a_map_s_v_s["a"] through a_map_s_v_s["z"]). You will have to create 26 vectors of strings to be mapped to, regardless of what approach you make. You've got approach 1:
    Code:
    vector<string> empty_vector;
    not-really-for (key = "a"; key <= "z"; key[0]++) {
        a_map_s_v_s.insert(pair(key, empty_vector));
    }
    //then for every key, value
    a_map_s_v_s[key].push_back(value_string)
    Now approach 1 has a sub-approach 1a that I wouldl have to poke through the standard to see if it works, which is simply
    Code:
    //then for every key, value
    a_map_s_v_s[key].push_back(value_string)
    trusting that if key hasn't been used before, a freshly-made sparkly-new vector<string> is handed to you at that time by the [] operator. If this works, and I suspect it will, then that makes this version look a lot more awesome.

    Approach 2 isn't that different from approach 1, really:
    [code]
    Code:
    not-really-for (key = "a"; key <= "z"; key[0]++) {
        vector<string> empty_vector;
        //for all the values that go with this key
        empty_vector.push_back(value);
        a_map_s_v_s.insert(pair(key, empty_vector));
    }
    You would have to know all the end-result strings in one place, though.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21