C++ C++;映射如何在每次调用类对象时获取新的映射对象
我有一个问题,每次在主函数中调用更新函数时,我都需要将贴图对象插入向量(大小为2)。下面是该类的代码段 Cache.hxx CacheWriter.hxx CacheWriter.cxxC++ C++;映射如何在每次调用类对象时获取新的映射对象,c++,stdvector,stdmap,C++,Stdvector,Stdmap,我有一个问题,每次在主函数中调用更新函数时,我都需要将贴图对象插入向量(大小为2)。下面是该类的代码段 Cache.hxx CacheWriter.hxx CacheWriter.cxx 从上面的输出可以看出,即使我试图获取一个新的映射对象,以前的值也会被覆盖(timestamp是相同的),而我希望在交换索引之前,应该存在两个不同的映射对象,以使以前的值存在。是否有任何方法可以让我每次都获得新的贴图对象,这样以前的索引值就不会被覆盖。这方面的任何指针都会有很大帮助。我认为问题在于您使用了cach
从上面的输出可以看出,即使我试图获取一个新的映射对象,以前的值也会被覆盖(timestamp是相同的),而我希望在交换索引之前,应该存在两个不同的映射对象,以使以前的值存在。是否有任何方法可以让我每次都获得新的贴图对象,这样以前的索引值就不会被覆盖。这方面的任何指针都会有很大帮助。我认为问题在于您使用了cacheVector.insert而不是cacheVector[]。
插入方法的每次调用都会将向量大小增加1。这可能不是您所期望的。您确定这不起作用吗
time()
只有1秒的粒度,并且这些代码都应该在1秒内运行,因此它们可能都具有相同的时间戳是有意义的。
#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
#ifndef CACHEREADER_H
#define CACHEREADER_H
#include "Cache.hxx"
class CacheReader : public Cache
{
public:
CacheReader();
~CacheReader();
void readCache();
};
#endif
#ifndef CACHEWRITER_H
#define CACHEWRITER_H
#include "Cache.hxx"
class CacheWriter : public Cache{
public:
CacheWriter();
~CacheWriter();
void updateCosCache();
private:
time_t lastUpdated;
};
#endif
#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;
}
}
#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;
}
}
}
#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