Thread: unordered_map of vectors

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    222

    unordered_map of vectors

    hi,

    a simple one : how to create an unordered map of fixed size vectors?

    Code:
    
    unordered_map<string, vector<int>> x;
    
    x["mumb 5"][7] = 65; // this does not work since my vector size is not set.
    thnx

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    you have to create enough space for a value at index 7 in your vector. the easiest way to do this is by calling the resize() member function on an entry in your map.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    auto & vec = x["mumb 5"]; // Get vector
    vec.resize(8); // Resize to make index 7 valid
    vec.at(7) = 65; // Fill element

    Best is to use push_back. Avoid index operator since it can cause undefined behaviour if you access out-of-bounds elements. .at() function helps you catch these programming errors earlier.
    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.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    In C++11 the easiest way is to use an std::array:

    Code:
    unordered_map<string, array<int,8>> x;
    x["mumb 5"][7] = 65;
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Elysia View Post
    Avoid index operator since it can cause undefined behaviour if you access out-of-bounds elements.
    I suspect you're referring to the vector's index operator. both the map and its vector elements have an index operator in this case, so it was a little unclear to me at first which one you were referring to.

    just to clarify for the OP, there's no risk of "out of bounds" access with the map itself. using an "index" that doesn't exist in an stl map will simply create that element and return its associated value.

    Quote Originally Posted by King Mir View Post
    In C++11 the easiest way is to use an std::array
    but only if you know you need fixed size elements, whose size is known at compile time.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by Elkvis View Post
    but only if you know you need fixed size elements, whose size is known at compile time.
    Which is exactly what the OP asked for:

    Quote Originally Posted by baxy View Post
    hi,

    a simple one : how to create an unordered map of fixed size vectors?

    Code:
    
    unordered_map<string, vector<int>> x;
    
    x["mumb 5"][7] = 65; // this does not work since my vector size is not set.
    thnx
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by King Mir View Post
    Which is exactly what the OP asked for:
    but we don't know if the size is guaranteed to be a compile-time constant. it could be dependent on user input or some other run-time parameter. perhaps the OP can weigh in on this.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Elkvis View Post
    I suspect you're referring to the vector's index operator. both the map and its vector elements have an index operator in this case, so it was a little unclear to me at first which one you were referring to.
    I can confirm that I did refer to the vector.
    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. unordered_map and const ??
    By baxy in forum C++ Programming
    Replies: 2
    Last Post: 09-15-2013, 02:00 PM
  2. map, unordered_map or vector ?
    By baxy in forum C++ Programming
    Replies: 5
    Last Post: 03-22-2013, 01:25 AM
  3. Vectors in vectors - pointers and iterators
    By fisherking in forum C++ Programming
    Replies: 8
    Last Post: 07-27-2010, 09:34 AM
  4. Replies: 2
    Last Post: 05-06-2008, 10:41 AM
  5. Vectors
    By Drake in forum Game Programming
    Replies: 8
    Last Post: 01-29-2006, 03:40 PM