简体   繁体   中英

map to structs always returns the last struct in a vector<struct>

I have a vector or structs. I am creating the structs in place using emplace_back. Then, I map a pointer to each struct into a map of pointers.

However, later when I try to recall the structs, the map always retrieves the last struct.

#include <map>
#include <vector>
#include <iostream>

using namespace std;

// -------------------------------
//      Asset Pair
// -------------------------------
    struct asset_pair {

        // Member Variables
            int ID_1;
            int ID_2;
            int pair_ID;

        // Constructor
            asset_pair(const int id_1, const int id_2) : ID_1(id_1) , ID_2(id_2)
            {
                pair_ID = ( ((id_1 & 0xFFFF) << 16) | ((id_2 & 0xFFFF) << 0));
            };
    };

std::map<int, asset_pair*> pair_id_map;

// -------------------------------------
//      Pair Exists
// -------------------------------------
    bool pair_exists(const int id_1, const int id_2)
    {
        int pair_id = (((id_1 & 0xFFFF) << 16) | ((id_2 & 0xFFFF) << 0));

        return pair_id_map.find(pair_id) != pair_id_map.end();
    }



/******************************************************/
/*                  Main                              */
/******************************************************/

int main() {


    // Asset Pair Vector
        std::vector<asset_pair> Asset_Pairs;

        Asset_Pairs.reserve(10);
        int pair_counter = 0;

    // Asset ID's
        int asset_ids[5];

        for (int i=0; i<6; i++)
            asset_ids[i] = i + 1;

    // Create Pairs - in place
        for (auto id_1 : asset_ids)
        {
            for (auto id_2 : asset_ids)
            {
                if (id_1 == id_2)
                    continue;

                // Create Pair
                    Asset_Pairs.emplace_back(asset_pair(id_1, id_2));
                    auto new_pair = *(Asset_Pairs.begin() + pair_counter);
                    pair_counter ++;

                // Pair ID map
                    asset_pair *p_pair = &new_pair;
                    pair_id_map[new_pair.pair_ID] = p_pair;

                // Sanity Check
                    cout << "Creating Pair :: " << new_pair.pair_ID << " || " << new_pair.ID_1 << ":" << new_pair.ID_2 << "\n";
            }

            cout << "--------------\n";
        }

    // Access Pairs
        for (auto id_1 : asset_ids)
        {
            for (auto id_2 : asset_ids)
            {
                if (id_1 == id_2)
                    continue;

                if (pair_exists(id_1, id_2))
                {
                    // Get Pair
                        int pair_id = ( ((id_1 & 0xFFFF) << 16) | ((id_2 & 0xFFFF) << 0));
                        auto new_pair = pair_id_map[pair_id];

                    // Print
                        cout << "Access Pair :: " << new_pair->pair_ID << " -- ";
                        cout << new_pair->ID_1 << "|" << new_pair->ID_2 << "\n";
                }

            }
        }


}

Output

// ---------------------
//   :: Create Pairs ::
// ---------------------
// Creating Pair :: 65538 || 1:2
// Creating Pair :: 65539 || 1:3
// Creating Pair :: 65540 || 1:4
//   ~ ~ ~
// Creating Pair :: 327683 || 5:3
// Creating Pair :: 327684 || 5:4

// ---------------------
//   :: Access Pairs ::
// ---------------------
// Access Pair :: 327684 -- 5|4
// Access Pair :: 327684 -- 5|4
//  ~ ~ ~
// Access Pair :: 327684 -- 5|4
  • Initialize vector
    • create structs in place
  • add pair to map: [ ID, *pair ]
  • access pair: [ ID, *pair ]
auto new_pair = *(Asset_Pairs.begin() + pair_counter);
asset_pair *p_pair = &new_pair;
pair_id_map[new_pair.pair_ID] = p_pair;

will cause a pointer to the local variable new_pair to be stored in your map. You need to change new_pair to either a pointer or reference:

auto &new_pair = *(Asset_Pairs.begin() + pair_counter);

Or, since Asset_Pairs is a vector you can just use

auto &new_pair = Asset_Pairs[pair_counter];

or

auto &new_pair = Asset_Pairs.back();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM