本文整理汇总了C++中unordered_map::erase方法的典型用法代码示例。如果您正苦于以下问题:C++ unordered_map::erase方法的具体用法?C++ unordered_map::erase怎么用?C++ unordered_map::erase使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类unordered_map
的用法示例。
在下文中一共展示了unordered_map::erase方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: topoSort
string topoSort(unordered_map<char, unordered_set<char>>& graph, unordered_map<char, int>& mp) {
// For chars without any incoming edges, we output it as the start of the string.
string res = "";
while (!mp.empty()) {
bool found = false;
for (auto& pair : mp) {
if (pair.second == 0) {
res.push_back(pair.first);
for (auto& nn : graph[pair.first]) {
mp[nn]--;
}
graph.erase(pair.first);
mp.erase(pair.first);
found = true;
break;
}
}
if (!found) {
res.clear();
return res;
}
}
return res;
}
示例2: read
shared_ptr<Event> read()
{
uint8_t buffer_in[3];
snd_rawmidi_read(handle_in, &buffer_in, 3);
shared_ptr<NoteOnEvent> note_on, note_off;
switch ( buffer_in[0] & 0xF0 )
{
case MIDI_NOTE_ON:
if ( buffer_in[2] )
{
note_on = shared_ptr<NoteOnEvent>(new NoteOnEvent { buffer_in[1], buffer_in[2] });
open_notes.emplace(buffer_in[1], note_on);
return note_on;
}
else
{
note_on = open_notes.at(buffer_in[1]);
open_notes.erase(buffer_in[1]);
return shared_ptr<Event>(new NoteOffEvent { note_on });
}
case MIDI_NOTE_OFF:
note_on = open_notes.at(buffer_in[1]);
open_notes.erase(buffer_in[1]);
return shared_ptr<Event>(new NoteOffEvent { note_on });
case MIDI_CONTROL:
return shared_ptr<Event>(new ControlEvent { static_cast<ControlEvent::Controller>(buffer_in[1]), buffer_in[2] });
default:
return shared_ptr<Event>(new Event {});
}
}
示例3: dfs
bool dfs(string& pattern, int pstart, unordered_map<char, string>& p2s, string& str, int sstart, unordered_map<string, char>& s2p) {
if (pstart == pattern.size() || sstart == str.size()) {
return pstart == pattern.size() && sstart == str.size();
}
char ch = pattern[pstart];
if (p2s.find(ch) != p2s.end()) {
int len = p2s[ch].size();
string temp = str.substr(sstart, len);
if (temp == p2s[ch] && s2p.find(temp) != s2p.end() && s2p[temp] == ch)
return dfs(pattern, pstart+1, p2s, str, sstart+len, s2p);
else
return false;
} else {
for (int i = sstart; i < str.size(); i++) {
string temp = str.substr(sstart, i - sstart + 1);
if (s2p.find(temp) != s2p.end())
continue;
p2s[ch] = temp;
s2p[temp] = ch;
if (dfs(pattern, pstart+1, p2s, str, i+1, s2p))
return true;
p2s.erase(ch);
s2p.erase(temp);
}
return false;
}
}
示例4: plain_release
static int plain_release (const char *path, struct fuse_file_info *fi) {
boost::mutex::scoped_lock lock(io_mutex);
if (tempFiles.find(string(path))!=tempFiles.end()) { // not already in temp list
TempFile* file = tempFiles.find(string(path))->second;
if (file == NULL) {
tempFiles.erase(string(path));
} else {
if (file->written) {
struct stat stats;
stats.st_mode = S_IFREG | 0444;
stats.st_nlink = 1;
stats.st_size = file->data.size();
// evil hack, use the underlying c array pointer...
store->addFile(file->name.substr(1), stats, &(file->data[0]));
delete file;
tempFiles.erase(string(path));
} else {
cout<<"release file was not written"<<endl;
}
}
} else {
return -ENOENT;
}
return 0;
};
示例5: isMatch
bool isMatch(string & pattern, string & str,
int start_pattern_idx, int start_str_idx,
unordered_map<char, string> forward_mapping,
unordered_map<string, char> backward_mapping){
if(start_pattern_idx >= pattern.length() && start_str_idx>= str.length())
return true;
if(start_pattern_idx >= pattern.length() || start_str_idx >= str.length())
return false;
char pc = pattern[start_pattern_idx];
bool inserted_forward = false, inserted_backward = false;
for(int len=1; len<=str.length()-start_str_idx; len++){
string s = str.substr(start_str_idx, len);
if(forward_mapping.find(pc)==forward_mapping.end()){
forward_mapping[pc] = s;
inserted_forward = true;
}else{
if(forward_mapping[pc]!=s) continue;
}
if(backward_mapping.find(s)==backward_mapping.end()){
backward_mapping[s] = pc;
inserted_backward = true;
}else{
if(backward_mapping[s]!=pc) return false;
}
if(isMatch(pattern, str, start_pattern_idx+1, start_str_idx+len, forward_mapping, backward_mapping)){
return true;
}
if(inserted_forward) forward_mapping.erase(pc);
if(inserted_backward) backward_mapping.erase(s);
}
return false;
}
示例6: set
void set(int key, int value) {
t++;
//already in cache, just update timestamp and value
if (key_value.count(key)) {
key_time[key] = t;
key_value[key] = value;
}
else {
if (pq.size() >= cap) {
while (pq.top().time != key_time[pq.top().key]) {
Elem x = pq.top();
x.time = key_time[x.key];
pq.pop();
pq.push(x);
}
int oldKey = pq.top().key;
key_value.erase(oldKey);
key_time.erase(oldKey);
pq.pop();
}
//insert
key_value[key] = value;
key_time[key] = t;
pq.push(Elem{t, key});
}
}
示例7: put
void put(int key, int value) {
auto it = db.find(key);
auto iter = itcache.find(key);
if(it!=db.end()) {
(it->second.second)++;
it->second.first = value;
int newfreq = it->second.second;
int curfreq = newfreq-1;
cache[curfreq].erase(iter->second);
cache[newfreq].push_front(key);
itcache[key] = cache[newfreq].begin();
return;
}
if(itcache.size() == n) {
if(cache[minfreq].size() == 0)
return;
int keytodel = cache[minfreq].back();
cache[minfreq].pop_back();
itcache.erase(keytodel);
db.erase(keytodel);
}
minfreq = 1;
db[key] = make_pair(value, 1);
cache[1].push_front(key);
itcache[key] = cache[1].begin();
}
示例8: match
bool match(string &pattern, int i, string &str, int j) {
int m = pattern.size(), n = str.size();
if (i == m && j == n) {
return true;
} else if (i == m || j == n) {
return false;
}
bool pairFound = false;
for (int k = j; k < n; k++) {
string word = str.substr(j, k-j+1);
char p = pattern[i];
if (p2w.find(p) != p2w.end()) {
if (p2w[p] != word) continue;
} else if (w2p.find(word) != w2p.end()) {
if (w2p[word] != p) continue;
} else {
p2w[p] = word;
w2p[word] = p;
pairFound = true;
}
// DFS
if (match(pattern, i+1, str, k+1)) return true;
// backtrack
if (pairFound) {
p2w.erase(p);
w2p.erase(word);
}
}
return false;
}
示例9: update_window
uint16_t update_window(const Packet &p, unordered_map<uint16_t, Packet_info> &window, uint16_t &base_num, RTO &rto)
{
uint16_t n_removed = 0;
while (base_num > p.ack_num())
{
auto found = window.find(base_num);
if (found == window.end())
{
cerr << "could not find base_num packet with base_num " << base_num << " and ack num " << p.ack_num() << " in window, update_window" << endl;
exit(1);
}
rto.update_RTO(found->second.get_time_sent());
uint16_t len = found->second.data_len();
window.erase(base_num);
n_removed += len;
base_num = (base_num + len) % MSN;
}
while (base_num < p.ack_num())
{
auto found = window.find(base_num);
if (found == window.end())
{
cerr << "could not find base_num packet with base_num " << base_num << " and ack num " << p.ack_num() << " in window, update_window" << endl;
exit(1);
}
rto.update_RTO(found->second.get_time_sent());
uint16_t len = found->second.data_len();
window.erase(base_num);
n_removed += len;
base_num = (base_num + len) % MSN;
}
return n_removed;
}
示例10: erase_last
void erase_last()
{
if(lru.size()>cap)
{
int temp = lru.back();
lru.pop_back();
dic.erase(temp);
key_loc.erase(temp);
}
}
示例11: longestConsecutivex
int longestConsecutivex(vector<int>& nums) {
int maxLength = 0;
for(int i = 0; i < nums.size(); i++){
int startRange = nums[i] + 1;
int endRange = nums[i]-1;
if(startMap[nums[i]] || endMap[nums[i]])
continue;
if(startMap[startRange] || endMap[endRange]){
Range* pSr = startMap[startRange];
Range* pEr = endMap[endRange];
if(pSr){
pSr->start = nums[i];
pSr->count++;
startMap.erase(startRange);
startMap[nums[i]] = pSr;
maxLength = max(maxLength, pSr->count);
}
if(pEr){
pEr->end = nums[i];
pEr->count++;
endMap.erase(endRange);
endMap[nums[i]] = pEr;
maxLength = max(maxLength, pEr->count);
}
if(pSr && pEr){
pEr->end = pSr->end;
pEr->count = pEr->count + pSr->count - 1;
startMap.erase(nums[i]);
endMap.erase(nums[i]);
endMap[pEr->end] = pEr;
delete pSr;
maxLength = max(maxLength, pEr->count);
}
}
else{
Range* pR = new Range;
pR->start = nums[i];
pR->end = nums[i];
pR->count = 1;
maxLength = max(maxLength, pR->count);
startMap[nums[i]] = pR;
endMap[nums[i]] = pR;
}
}
return maxLength;
}
示例12: set
void set(int key, int value) {
if (key2value.count(key) == 0 && capacity == key2value.size()){
key2value.erase(time2key.begin()->second);
key2time.erase(time2key.begin()->second);
time2key.erase(time2key.begin());
}
if (key2value.count(key) > 0){
time2key.erase(key2time[key]);
}
key2value[key] = value;
time2key[id] = key;
key2time[key] = id;
id++;
}
示例13: helper
bool helper(string &pat, int i, string &str, int j, unordered_map<char,string> &m1, unordered_map<string,char> &m2){
if(i==pat.size() && j==str.size()){
return true;
}
if(i==pat.size() || j==str.size()){
return false;
}
char c = pat[i];
if(m1.find(c)!=m1.end()){
//pattern c is already exist
string s=m1[c];
if(s!=str.substr(j,s.size())){
return false;
}
// match c, yeah , go forward
return helper(pat,i+1,str,j+s.size(),m1,m2);
}else{
// pattern c is not exist
// try to match it with str[i...end] use backtracking
for(int k=j; k<str.size(); k++){
string s = str.substr(j,k-j+1);
if(m2.find(s)!=m2.end()){
if(m2[s]!=c){
continue;
}
}
//use it as a match
m1[c]=s;
m2[s]=c;
if(helper(pat,i+1,str,k+1,m1,m2)){
return true;
}
//backtracking
m1.erase(c);
m2.erase(s);
}
}
return false;
}
示例14: put
void put(int key, int value) {
if (hash.find(key) == hash.end()) {
if (hash.size() == n) {
hash.erase(order.front());
iters.erase(order.front());
order.pop_front();
}
} else {
order.erase(iters[key]);
}
order.push_back(key);
iters[key] = --(order.end());
hash[key] = value;
}
示例15: updateBuildings
void Buildings::updateBuildings(color_ostream& out, void* ptr)
{
int32_t id = (int32_t)ptr;
auto building = df::building::find(id);
if (building)
{
// Already cached -> weird, so bail out
if (corner1.count(id))
return;
// Civzones cannot be cached because they can
// overlap each other and normal buildings.
if (!building->isSettingOccupancy())
return;
df::coord p1(min(building->x1, building->x2), min(building->y1,building->y2), building->z);
df::coord p2(max(building->x1, building->x2), max(building->y1,building->y2), building->z);
corner1[id] = p1;
corner2[id] = p2;
for ( int32_t x = p1.x; x <= p2.x; x++ ) {
for ( int32_t y = p1.y; y <= p2.y; y++ ) {
df::coord pt(x,y,building->z);
if (containsTile(building, pt, false))
locationToBuilding[pt] = id;
}
}
}
else if (corner1.count(id))
{
//existing building: destroy it
df::coord p1 = corner1[id];
df::coord p2 = corner2[id];
for ( int32_t x = p1.x; x <= p2.x; x++ ) {
for ( int32_t y = p1.y; y <= p2.y; y++ ) {
df::coord pt(x,y,p1.z);
auto cur = locationToBuilding.find(pt);
if (cur != locationToBuilding.end() && cur->second == id)
locationToBuilding.erase(cur);
}
}
corner1.erase(id);
corner2.erase(id);
}
}