[英]c++ : convert std::map<std::string, double> to std::map<std::string_view, double>
[英]Sending an std::map<int, std::map<std::string, double> > over C++ socket (linux)
我試圖在linux中通過TCP套接字發送由int和另一個映射組成的映射。
地圖是形式
map<int, map<string, double>>
按照這個SO鏈接我試着做
unsigned char* mybytemap = reinterpret_cast<unsigned char*>(&my_map);
然后發送緩沖區我用write()函數如下:
int size = sizeof(mybytemap);
char temp[10];
sprintf(temp, "%d", size);
write(sockfd, temp, strlen(temp)); //send the size for the client
write(sockfd, mybytemap, sizeof(mybytemap));
在客戶端:
char temp[10];
n = read(sockfd, temp, 10);
size = stoi(temp); //Got Size Of buffer
unsigned char * buf;
if(size != 0)
{
buf = new unsigned char[size];
int current=0;
while(current<size)
{
n = read(sockfd,(unsigned char*)(buf)+current, min(1024,size-current));
if (n <= 0)
{
cout<<"ERROR reading from socket when receiving"<<endl;
break;
}
current+=n;
}
}
map<int, map<string, double>> *test = reinterpret_cast< map<int, map<string, double>>* > (buf);
vector<int> ks;
for(map<int, map<string, double>>::iterator it = test->begin(); it != test->end(); ++it)
{
ks.push_back(it->first);
cout<<"id: "<<it->first<<endl;
}
但是在嘗試訪問數據時未正確接收Map並且代碼崩潰。 怎么解決這個問題?
我需要XML地圖嗎? 若有,有人可以指導我如何做到這一點?
您的問題中的鏈接答案不適用於地圖(地圖不是pod)
一般的想法如何編碼\\ decode map:編碼map的大小然后為每個鍵/值編碼密鑰的大小,然后編碼密鑰,然后編碼值的大小,然后編碼值。 以下代碼假定您將size_t編碼為sizeof(size_t)字節
template<class T>
std::string encode(T value){
// data to bytes
}
template<class T>
T decode(std::string bytes){
// bytes to data
}
template<class K, class V>
std::string encode_map(std::map<K, V> data){
std::string result;
result.append(encode<size_t>(data.size()));
for(auto iter: data){
std::string first = encode<K>(iter.first);
result.append(encode<size_t>( first.size() ));
result.append(first);
std::string second = encode<V>(iter.second);
result.append(encode<size_t>( second.size() ));
result.append(encode<V>(iter.second));
}
return result;
}
template<class K, class V>
std::map<K, V> decode_map(std::string bytes){
size_t index = 0;
size_t size = decode<size_t>(std::string(bytes.begin()+index, bytes.begin()+index+sizeof(size_t) ) );
index += sizeof(size_t);
std::map<K, V> result;
for(size_t i = 0; i<size; i++){
size_t next_size = decode<size_t>(std::string(bytes.begin()+index, bytes.begin()+index+sizeof(size_t) ) );
index += sizeof(size_t);
K key = decode<K>(std::string(bytes.begin()+index, bytes.begin()+index+next_size ) );
index += next_size;
next_size = decode<size_t>(std::string(bytes.begin()+index, bytes.begin()+index+sizeof(size_t) ) );
index += sizeof(size_t);
V value = decode<V>(std::string(bytes.begin()+index, bytes.begin()+index+next_size ) );
index += next_size;
result[key] = value;
}
return result;
}
按照建議,您需要執行所謂的序列化 。 您可以將Boost.Serialization用作序列化庫。 它可以處理所有STL類型。 一個說明性示例:
std::map<int, std::map<std::string, double>> m1;
// ... (fill m1)
// serialize into a buffer (string):
std::string buffer;
boost::iostreams::back_insert_device<std::string> inserter(buffer);
boost::iostreams::stream<boost::iostreams::back_insert_device<std::string>> ostr(inserter);
boost::archive::binary_oarchive oa(ostr);
oa << m1;
ostr.flush();
// ... (here you can send the contents of buffer as plain bytes-chars via socket)
// deserialize into new map:
boost::iostreams::basic_array_source<char> device(buffer.data(), buffer.size());
boost::iostreams::stream<boost::iostreams::basic_array_source<char>> istr(device);
boost::archive::binary_iarchive ia(istr);
std::map<int, std::map<std::string, double>> m2;
ia >> m2;
完整的現場演示(包括所有標題): https : //wandbox.org/permlink/NyZeVTrFI0p8RcmY
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.