unordered_map of vectors

This is a discussion on unordered_map of vectors within the C++ Programming forums, part of the General Programming Boards category; hi, a simple one : how to create an unordered map of fixed size vectors? Code: unordered_map<string, vector<int>> x; x["mumb ...

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

    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
    2,499
    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.
    Code:
    namespace life
    {
        const bool change = true;
    }

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,777
    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,050
    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
    2,499
    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.
    Code:
    namespace life
    {
        const bool change = true;
    }

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,050
    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
    2,499
    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.
    Code:
    namespace life
    {
        const bool change = true;
    }

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,777
    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, 02:40 PM

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