C ++ std :: map每次调用类对象时如何获取新的地图对象

时间:2012-12-01 21:25:56

标签: c++ stdvector stdmap

我有一个问题,每当我在main中调用更新函数时,我需要将一个map对象插入一个vector(大小为2)。以下是该类的代码段。

Cache.hxx

#ifndef CACHECOS_H
#define CACHECOS_H

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

using namespace std;

class Cache
{
    public:
        static int index;
        static vector<map<string, map<string, string> > > cacheVector;
    protected:
        typedef map<string, string> innerMap;
        typedef map<string, innerMap> cacheMap;
        cacheMap cache_Map;
};
#endif

CacheReader.hxx

#ifndef CACHEREADER_H
#define CACHEREADER_H

#include "Cache.hxx"

class CacheReader : public Cache
{
    public:
        CacheReader();
        ~CacheReader();
        void readCache();
};
#endif

CacheWriter.hxx

#ifndef CACHEWRITER_H
#define CACHEWRITER_H

#include "Cache.hxx"

class CacheWriter : public Cache{
    public:
        CacheWriter();
        ~CacheWriter();
        void updateCosCache();

    private:
        time_t lastUpdated;
};

#endif

CacheWriter.cxx

#include "CacheWriter.hxx"
#include <sstream>

using namespace std;

int Cache::index = -1;
vector<map<string, map<string, string> > >Cache::cacheVector;

CacheWriter::CacheWriter()
{}

CacheWriter::~CacheWriter()
{}

void CacheWriter::updateCosCache()
{
    cache_Map.clear(); //Is this required
    //First element in the outer map
    cache_Map.insert(make_pair("test1", innerMap()));
    //Write elment in inner map
    cache_Map["test1"].insert(make_pair("mailstore", "host1"));
    cache_Map["test1"].insert(make_pair("mailboxid", "100"));
    cache_Map["test1"].insert(make_pair("emailId", "test@test.com"));
    cache_Map["test1"].insert(make_pair("poplogin", "test"));
    lastUpdated = time(NULL);
    cache_Map.insert(make_pair("lastUpdated", innerMap()));
    stringstream ss;
    ss << lastUpdated;
    cache_Map["lastUpdated"].insert(make_pair("timeStamp", ss.str()));
    if(index == -1) {
        cout<<"Cuurent index is "<<index<<endl;
        cacheVector.push_back(cache_Map);
        //cacheVector.insert(cacheVector.begin() + index +1, cache_Map);
        index = 0;
    } else if(index == 0) {
        cout<<"Cuurent index is "<<index<<endl;
        cacheVector.insert(cacheVector.begin() + 1, cache_Map); // insert new map in the vector.
        index = 1;
    } else {
        cout<<"Cuurent index is "<<index<<endl;
        cacheVector.insert(cacheVector.begin() + 0, cache_Map );
        index = 0;
    }
}

CacheReader.cxx

#include "CacheReader.hxx"

CacheReader::CacheReader()
{}

CacheReader::~CacheReader()
{}

void CacheReader::readCache()
{
    cache_Map = cacheVector[index];
    map<string, innerMap>::iterator cache_Mapit;
    map<string, string>::iterator innerIter;
    for(cache_Mapit = cache_Map.begin(); cache_Mapit != cache_Map.end(); ++cache_Mapit) {
        cout << "\n\nNew element\n" <<(*cache_Mapit).first<<endl;
        for(innerIter = (*cache_Mapit).second.begin(); innerIter != (*cache_Mapit).second.end(); ++innerIter) {
            cout << (*innerIter).first << " => " << (*innerIter).second << endl;
        }
    }
}

test.cxx

#include "Cache.hxx"
#include "CacheReader.hxx"
#include "CacheWriter.hxx"

int main()
{
    CacheWriter cacheWriter;
    CacheReader cacheReader;

    for(int i = 0; i < 5; i++) {
        cacheWriter.updateCosCache();
        cacheReader.readCache();
    }
}

以下是上述代码的输出。

$ ./test

Cuurent index is -1


New element
lastUpdated
timeStamp => 1354396680


New element
test1
emailId => test@test.com
mailboxid => 100
mailstore => host1
poplogin => test
Cuurent index is 0


New element
lastUpdated
timeStamp => 1354396680


New element
test1
emailId => test@test.com
mailboxid => 100
mailstore => host1
poplogin => test
Cuurent index is 1


New element
lastUpdated
timeStamp => 1354396680


New element
test1
emailId => test@test.com
mailboxid => 100
mailstore => host1
poplogin => test
Cuurent index is 0


New element
lastUpdated
timeStamp => 1354396680


New element
test1
emailId => test@test.com
mailboxid => 100
mailstore => host1
poplogin => test
Cuurent index is 1


New element
lastUpdated
timeStamp => 1354396680


New element
test1
emailId => test@test.com
mailboxid => 100
mailstore => host1
poplogin => test

正如你从上面的输出中看到的那样,即使我试图获得一个新的地图对象,之前的值也会被覆盖(时间戳是相同的),而我希望应该有两个不同的地图对象存在直到索引的交换发生,以便存在先前的值。我有没有办法每次都能获得新的地图对象,以便以前的索引值不会被覆盖。这方面的任何指针都会有很大的帮助。

1 个答案:

答案 0 :(得分:0)

我认为问题是你使用cacheVector.insert而不是cacheVector []。 每次调用insert方法都会将矢量大小增加一。这可能不是你所期待的。