私のコメントが示唆したように、このようなServerInfo *remote;
として、彼らは必要じゃないポインタを、使用しないように努力し、単にstd::vector
使用する必要があります。subscriptionId
を取るコンストラクタを追加するために、私はSubscriptionInfo
を更新
#include <vector>
#include <map>
#include <string>
#include <cstdint>
struct ServerInfo {
uint32_t ip;
uint16_t port;
ServerInfo(uint32_t io_ = 0, uint16_t port_= 0) :
ip(ip_), port(_port) {}
};
struct SubscriptionInfo {
uint64_t subscriptionId;
std::vector<ServerInfo> remote;
SubscriptionInfo(uint64_t val = 0) : subscriptionId(val) {}
};
。
あなたがこれをしたら、既存のキーおよびサブスクリプション情報がstd::map::insertを使用して行うことができます追加:
int main()
{
std::map<std::string, SubscriptionInfo> sub_list;
// insert an item into the map. If already existing, return
// the iterator to the existing item
auto iter = sub_list.insert(std::make_pair("1", // string
SubscriptionInfo(1))). // subscription info
first; // item that was inserted, or
// existing item if already in map.
// push back a new ip and port into the vector.
(*iter).second.remote.push_back({ 100,18 }); // add IP and port 18
}
だから、基本的に私たちがマップに新しい項目を挿入するためにstd::map::insert
を使用。 std::map::insert
の戻り値は、std::pair
を返します。このペアのfirst
は、挿入されたアイテムのイテレータまたは既に存在する場合は、既存のマップエントリのイテレータです。
そのため、アイテムが存在するかどうかを確認する必要はありません。remote
メンバーのpush_back
に電話する必要があります。これは、新しいエントリまたは既存のエントリへのイテレータを取得するためです。 insert
(マップ自体はstd::pair<key_type, value_type>
というエントリを格納するので、このペアのsecond
にSubscriptionInfo
インスタンスを取得します)。
remote.size()
はベクトルのエントリ数を返すので、サーバーの数を追跡するために別のメンバ変数は必要ありません。 no_of_servers
のような無関係の変数を持つと、アイテムがベクトルに追加または削除されるたびにこの変数を手動で更新しないためにバグが発生する可能性が高くなります。代わりに、正しい金額を得るために常にstd::vector::size()
を使用してください。
エントリを削除するには、あなたが必要とするすべてはkey
値であり、エントリはstd::map::erase()
機能を呼び出すことによって削除されます。
sub_list.erase("1"); // removes key "1" and the data associated with it
編集を:あなたはC++ 11に準拠していないコンパイラを使用している場合、次の変更がinsert
への呼び出しになされるべきである。
typedef std::map<std::string, SubscriptionInfo> MapStringToSub;
//...
// insert an item into the map. If already existing, return
// the iterator to the existing item
MapStringToSub::iterator iter = sub_list.insert(std::make_pair("1", // string
SubscriptionInfo(1))). // subscription info
first; // item that was inserted, or
// existing item if already in map.
// push back a new ip and port into the vector.
(*iter).second.remote.push_back(ServerInfo(100,18)); // add IP and port 18
ドロップ 'ServerInfo *'と 'のstd ::ベクトルを'使用しています。そして、 'no_of_servers'も離れます。ベクトルは独自のサイズを知っているからです。 –
PaulMcKenzie
あなたの 'SubscriptionInfo'の中で' std :: vector 'を使います。他の方法が見つからない限り、ポインタを使用しないことを常にお勧めします。 –