#include "stdlib.h"
#include "stdio.h"
#include <sys/shm.h>
#include <sys/stat.h>
#include <string>
#include <tr1/unordered_map>
using namespace std::tr1;
using namespace std;
struct zhongguoren{
int iAge;
string szName;
zhongguoren(const int i, const string &sss)
{
iAge = i;
szName = sss;
}
void print()
{
printf("AGE = %d, NAME = %s\n", iAge, szName.c_str());
}
};
typedef std::tr1::unordered_multimap<string, zhongguoren> MAP_ZHONGGUOREN;
int main(void)
{
zhongguoren tmp1(11111, "chaassdenddjun");
zhongguoren tmp2(11112, "lifdfedsdng");
zhongguoren tmp3(11113, "chensecjiaeewshuo");
zhongguoren tmp4(11114, "wangsdfdcadfihong");
zhongguoren tmp5(11115, "jiansjcddierong");
MAP_ZHONGGUOREN hhhh;
hhhh.insert(pair<string, zhongguoren>("cj", tmp1));
hhhh.insert(pair<string, zhongguoren>("lf", tmp2));
hhhh.insert(pair<string, zhongguoren>("cjs", tmp3));
hhhh.insert(pair<string, zhongguoren>("wch", tmp4));
hhhh.insert(pair<string, zhongguoren>("jjr", tmp5));
printf("开始查询\n");
auto range = hhhh.equal_range("cj");
auto target = range.first;
if (target != hhhh.end())
{
for (auto i = target; i != range.second; i++)
{
(*i).second.print();
}
}
printf("结束查询\n");
MAP_ZHONGGUOREN *ccccc;
int segment_id;
struct shmid_ds shmbuffer;
int segment_size;
const int shared_segment_size = sizeof(hhhh); /* 分配一个共享内存块 */
segment_id = shmget(IPC_PRIVATE, shared_segment_size, IPC_CREAT|IPC_EXCL|S_IRUSR|S_IWUSR ); /* 绑定到共享内存块 */
ccccc = (MAP_ZHONGGUOREN*)shmat(segment_id, 0, 0);
shmctl(segment_id, IPC_STAT, &shmbuffer);
segment_size = shmbuffer.shm_segsz;
printf("segment size: %d\n", segment_size);
ccccc->swap(hhhh);
printf("存入共享内存中结束\n");
MAP_ZHONGGUOREN *tmp;
tmp = (MAP_ZHONGGUOREN*)shmat(segment_id, 0, 0);
printf("开始读取共享内存中的数据\n");
auto range1 = tmp->equal_range("cj");
auto target1 = range1.first;
if (target1 != tmp->end())
{
for (auto i = target1; i != range1.second; i++)
{
(*i).second.print();
}
}
printf("读取共享内存中的数据完成\n");
shmdt(tmp);
shmctl(segment_id, IPC_RMID, 0);/* 释放这个共享内存块 */
return 0;
}
#include "stdio.h"
#include <sys/shm.h>
#include <sys/stat.h>
#include <string>
#include <tr1/unordered_map>
using namespace std::tr1;
using namespace std;
struct zhongguoren{
int iAge;
string szName;
zhongguoren(const int i, const string &sss)
{
iAge = i;
szName = sss;
}
void print()
{
printf("AGE = %d, NAME = %s\n", iAge, szName.c_str());
}
};
typedef std::tr1::unordered_multimap<string, zhongguoren> MAP_ZHONGGUOREN;
int main(void)
{
zhongguoren tmp1(11111, "chaassdenddjun");
zhongguoren tmp2(11112, "lifdfedsdng");
zhongguoren tmp3(11113, "chensecjiaeewshuo");
zhongguoren tmp4(11114, "wangsdfdcadfihong");
zhongguoren tmp5(11115, "jiansjcddierong");
MAP_ZHONGGUOREN hhhh;
hhhh.insert(pair<string, zhongguoren>("cj", tmp1));
hhhh.insert(pair<string, zhongguoren>("lf", tmp2));
hhhh.insert(pair<string, zhongguoren>("cjs", tmp3));
hhhh.insert(pair<string, zhongguoren>("wch", tmp4));
hhhh.insert(pair<string, zhongguoren>("jjr", tmp5));
printf("开始查询\n");
auto range = hhhh.equal_range("cj");
auto target = range.first;
if (target != hhhh.end())
{
for (auto i = target; i != range.second; i++)
{
(*i).second.print();
}
}
printf("结束查询\n");
MAP_ZHONGGUOREN *ccccc;
int segment_id;
struct shmid_ds shmbuffer;
int segment_size;
const int shared_segment_size = sizeof(hhhh); /* 分配一个共享内存块 */
segment_id = shmget(IPC_PRIVATE, shared_segment_size, IPC_CREAT|IPC_EXCL|S_IRUSR|S_IWUSR ); /* 绑定到共享内存块 */
ccccc = (MAP_ZHONGGUOREN*)shmat(segment_id, 0, 0);
shmctl(segment_id, IPC_STAT, &shmbuffer);
segment_size = shmbuffer.shm_segsz;
printf("segment size: %d\n", segment_size);
ccccc->swap(hhhh);
printf("存入共享内存中结束\n");
MAP_ZHONGGUOREN *tmp;
tmp = (MAP_ZHONGGUOREN*)shmat(segment_id, 0, 0);
printf("开始读取共享内存中的数据\n");
auto range1 = tmp->equal_range("cj");
auto target1 = range1.first;
if (target1 != tmp->end())
{
for (auto i = target1; i != range1.second; i++)
{
(*i).second.print();
}
}
printf("读取共享内存中的数据完成\n");
shmdt(tmp);
shmctl(segment_id, IPC_RMID, 0);/* 释放这个共享内存块 */
return 0;
}