Can someone explain this concept to me

This is a discussion on Can someone explain this concept to me within the C++ Programming forums, part of the General Programming Boards category; I am currently learning how to use maps and i find a few things confusing e.g if i have Code: ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    118

    Can someone explain this concept to me

    I am currently learning how to use maps and i find a few things confusing
    e.g if i have
    Code:
    map<string ,vector<string > value;
    1.whats the original size of the map when i declare it
    2.does each string inserted have a pointer/mapping to a string vector?

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,528
    Quote Originally Posted by sigur47 View Post
    I am currently learning how to use maps and i find a few things confusing
    e.g if i have
    Code:
    map<string ,vector<string > value;
    1.whats the original size of the map when i declare it
    2.does each string inserted have a pointer/mapping to a string vector?
    1. Some memory is allocated for it (on the heap.. afaik), but you needn't worry about it, as it expands on demand.
    If you ever need to control that behaviour, the map container takes two more template parameters, look into what the fourth (allocator) does.

    2. Maybe, internally or may not be. Totally depends on the implementation. (btw.. what do you mean by 'mapping' ?)
    Last edited by manasij7479; 03-11-2012 at 08:25 PM.
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    1. The original size (in the sense of calling size() on it) is 0.

    2. Each inserted string key will have its own vector<string> associated with it, not just a pointer.

    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    #include <map>
    using namespace std;
    
    int main() {
        map<string, vector<string> > m;
        cout << m.size() << endl; // Zero size.
    
        // Create a vector with two elements.
        vector<string> v;
        v.push_back("abc");
        v.push_back("def");
    
        m["x"] = v; // Each of elements "x" and "y"
        m["y"] = v; // have their own copy of v.
        cout << m.size() << endl;
    
        m["y"][0] = "ghi"; // Only changes map element "y"s copy of v.
    
        cout << m["x"][0] << endl;
        cout << m["y"][0] << endl;
        
        m["a"]; // Just mentioning a non-existent element
                // creates a new vector.
        cout << m.size() << endl;
    
        m["a"].push_back("xyz");
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by sigur47 View Post
    1.whats the original size of the map when i declare it
    Call its size() method straight after the declaration and it'll tell you how many items it contains at that time. (It's zero by the way)

    2.does each string inserted have a pointer/mapping to a string vector?
    Every map contains two types of things; things of the first type that map to things of the second type. You can look up something of type A and will find along with it a thing of type B. The two types can of course be the same. This map will contain strings that map to vectors of strings.
    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"

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,021
    Many of the things you ask are totally irrelevant. This depends on a typical implementation.
    A better question to ask is: is X guaranteed to work, instead of to get X to work, I probably need Y and Z, so does <container> fulfill Y and Z?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. file concept
    By munna_dude in forum C Programming
    Replies: 20
    Last Post: 06-16-2010, 10:01 AM
  2. Concept
    By kusal in forum C Programming
    Replies: 12
    Last Post: 01-06-2007, 10:32 PM
  3. Concept help
    By Mithoric in forum Windows Programming
    Replies: 13
    Last Post: 04-18-2004, 04:05 PM
  4. linkedlist concept please!
    By SAMSAM in forum C Programming
    Replies: 3
    Last Post: 03-15-2003, 01:50 PM
  5. File I/O - Concept
    By MethodMan in forum C Programming
    Replies: 0
    Last Post: 03-02-2003, 12:07 PM

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