1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Erasing a vector element in a map one by one

Discussion in 'C' started by baskarvj, May 17, 2010.

  1. baskarvj

    baskarvj New Member

    Joined:
    May 17, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    hi,

    I have a need to store some data like this in a table. this table is just an example but the values can increase in numbers.

    key 1 Key2 Value

    100 1 1
    100 1 2
    100 1 3
    100 2 1
    100 2 2
    100 2 3
    100 2 4

    So i decided to have a map with a key pair and a vector to store the value.


    int main()
    {


    typedef std::vector<int> ValueVec_t;

    typedef std:pair<int, int > key_type_K1_K2_t;
    typedef std::map<key_type_K1_K2_t, ValueVec_t> finalMap_t;

    key_type_K1_K2_t keypair;
    ValueVec_t valueVec;
    ValueVec_t::iterator vec_iter;
    finalMap_t finalMap;

    // store the value into the vector

    for (size_t i=1; i<10; ++i)
    {
    cout << "Storing into the vector " << i << endl;
    valueVec.push_back(i);
    }

    // Store the vector into the map

    keypair.first = 100;
    keypair.second = 1;
    finalMap[keypair] = valueVec;

    keypair.first = 100;
    keypair.second = 8;
    finalMap[keypair] = valueVec;

    // print the value stored in a map

    for( finalMap_t::iterator iter = finalMap.begin(); iter != finalMap.end(); ++iter )
    {
    for (vec_iter = valueVec.begin();vec_iter!=valueVec.end();++vec_iter)
    {
    cout << "KEY 1 " << (*iter).first.first << " " << "KEY 2 " << (*iter).first.second << " " << "Value" << *vec_iter << endl;
    }
    }

    // Erase the element stored in a vector


    for( finalMap_t::iterator iter = finalMap.begin(); iter != finalMap.end(); ++iter )
    {
    for (vec_iter = valueVec.begin();vec_iter!=valueVec.end();++vec_iter)
    {
    valueVec.erase(vec_iter);
    }
    }

    }

    Problem is with the erase, actually i want to erase the vector element one by one stored in the map, and all the vector element stored for a specify key, i want to remove that map key. and iterate through the next map entry.

    Please someone have a check. Thanks in advance.
     

Share This Page