我有一个问题,每次在 main 中调用更新函数时,我都需要将地图对象插入到向量(大小为 2)中。下面是该类的代码片段。
缓存.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;
}
}
}
测试.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();
}
}
以下是上述代码的输出。
$ ./测试
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
正如您从上面的输出中看到的那样,即使我试图获取一个新的地图对象,以前的值也会被覆盖(时间戳相同),而我希望应该存在两个不同的地图对象,直到发生索引交换以便之前存在的价值。有什么方法可以让我每次都获得新的地图对象,这样以前的索引值就不会被覆盖。在这方面的任何指针都会有很大帮助。