static CSession* Find( const CSocket& sock ){ ITERATOR it = m_Sessions.find( sock.GetHandle() ); if( it != m_Sessions.end() ){ return(it->second); } return NULL; }
int main(){ #ifdef LOCAL_TEST std::ifstream in("input.txt",std::ifstream::in); #else std::istream& in=std::cin; #endif std::string line; std::vector<long> nums; std::vector<long> allnums; typedef std::tr1::unordered_map<long,int> MAP; MAP count; while(std::getline(in,line)){ lineToNumbers(line,nums); for(size_t i=0;i!=nums.size();++i){ long v=nums[i]; MAP::iterator iter=count.find(v); if(iter==count.end()){ count[v]=1; allnums.push_back(v); } else { iter->second++; } } } for(size_t i=0;i!=allnums.size();++i){ long v= allnums[i]; std::cout<<v<<" "<<count[v]<<"\n"; } #ifdef LOCAL_TEST in.close(); #endif return 0; }
CSession* Find( UINT id ){ ITERATOR it = m_map.find( id ); if( it != m_map.end() ){ return(it->second); } return NULL; }
UndirectedGraphNode *cloneGraph1(UndirectedGraphNode *node) { //BFS if (!node) return NULL; queue<UndirectedGraphNode *> q; q.push(node); MAP map; map[node] = new UndirectedGraphNode(node->label); while (!q.empty()) { UndirectedGraphNode *origNode = q.front(); q.pop(); UndirectedGraphNode *newNode = map[origNode]; for (int i = 0; i < origNode->neighbors.size(); ++i) { UndirectedGraphNode *origNb = origNode->neighbors[i]; if (map.find(origNb) != map.end()) { //already cloned newNode->neighbors.push_back(map[origNb]); //push map[origNb] continue; } UndirectedGraphNode *newNb = new UndirectedGraphNode(origNb->label); newNode->neighbors.push_back(newNb); map[origNb] = newNb; q.push(origNb); } } return map[node]; }
GraphNode *cloneGraph(GraphNode *node) { if (!node) return NULL; MAP exist; exist[node] = new GraphNode(node->data); queue<GraphNode *> q; q.push(node); while (!q.empty()) { GraphNode *orig = q.front(); GraphNode *newNode = exist[orig]; q.pop(); for (int i = 0; i < orig->neighbors.size(); ++i) { if (exist.find(orig->neighbors[i]) != exist.end()) { newNode->neighbors.push_back(exist[orig->neighbors[i]]); continue; } GraphNode *newNeighbor = new GraphNode(orig->neighbors[i]->data); newNode->neighbors.push_back(newNeighbor); exist[orig->neighbors[i]] = newNeighbor; q.push(orig->neighbors[i]); } } return exist[node]; }
long default_find(long key, long defaultValue, MAP& map){ MAP::const_iterator it = map.find(key); if (it == map.end()) { return defaultValue; } else { return it->second; } }
// TREE STUFF // Does there exist a root with variable v? bool FindTree(const Variable & v,SFSGNode *& result) const { MAP::const_iterator w = d_tree_map.find(v); bool b = w!=d_tree_map.end(); if(b) { result = (*w).second; }; return b; };
///function to search all values of the array in rb hash tree void search_random() { long int i = 0; long ret; while ( i < n ) { ret = hashmap.find ( data[i] )->second; i++; } /// }
inline typename MAP::mapped_type getValue_or_default (MAP& map, typename MAP::key_type const& key , typename MAP::mapped_type defaultVal) { typename MAP::const_iterator pos = map.find (key); if (pos != map.end()) return pos->second; else return defaultVal; }
void operator+=(simple_poly const &p1) { for(const_iterator i(p1.terms.begin());i!=p1.terms.end();++i) { iterator j = terms.find(i->first); if(j != terms.end()) { j->second += i->second; } else { terms.insert(std::make_pair(i->first,i->second)); } } }
int __map_hasKey(SEXP m, SEXP k) { MAP* list = (MAP*)R_ExternalPtrAddr(m); if (!list) return 0; if (!Rf_isString(k)) return 0; std::string keystr = as<std::string>(k); MAP::iterator it = list->find(keystr); return (it != list->end()); }
static void find_test (MAP &map, size_t iterations, KEY *keys) { // Find system generated keys. for (VALUE i = 0; i < iterations; ++i) { VALUE j; ACE_ASSERT (map.find (keys[i], j) != -1); ACE_ASSERT (i == j); } }
// [[Rcpp::export]] SEXP map_getVal(SEXP m, SEXP k) { MAP* list = (MAP*)R_ExternalPtrAddr(m); if (!list) return R_NilValue; if (!Rf_isString(k)) return R_NilValue; std::string keystr = as<std::string>(k); MAP::iterator it = list->find(keystr); if (it == list->end()) return R_NilValue; else return local_ptr_toRObject(it->second); }
bool GpXmlStateMachine::FindAttribute(MAP& attribs, const string& compstr, string& retstr) { bool retval = false; MAP::const_iterator search = attribs.find(compstr); #ifdef DEBUG cout << "Searching for attribute: " << compstr; #endif if (search != attribs.end() ) { retstr = attribs[compstr]; #ifdef DEBUG cout << " Found value: " << retstr; #endif retval = true; } #ifdef DEBUG cout << "\n"; #endif return retval; }
int main(){ MAP member; member.insert(make_pair(1000000000,1)); //使用strength做key关键字,因为题目已经给出了 实力值不可能相等 的条件 int strength,id; //也可使用id做key关键字,但这样每一次都需要完整遍历一次map,容易造成TLE int n; int low,high; cin >> n; while(n--){ cin >> id >> strength; member.insert(make_pair(strength,id)); MAP::iterator i,j,k; i = member.find(strength); if(i != member.end()){ if(i == member.begin()){ j = i; j++; cout << i->second << " " << j->second << endl; } else{ j = i; k = i; j++; k--; low = i->first - k->first; high = j->first - i->first; if(low <= high){ cout << i->second << " " << k->second << endl; } else{ cout << i->second << " " << j->second << endl; } } } else continue; } return 0; }
static void functionality_test (MAP &map, size_t iterations) { size_t counter; VALUE i; KEY *original_keys = new KEY[iterations]; KEY *modified_keys = new KEY[iterations]; // Setup the keys to have some initial data. for (i = 0; i < iterations; ++i) { original_keys[i].size (sizeof i / sizeof (KEY::TYPE)); ACE_OS::memcpy (&original_keys[i][0], &i, sizeof i); } // Make a copy of the keys so that we can compare with the original // keys later. for (i = 0; i < iterations; ++i) { modified_keys[i] = original_keys[i]; } // Add to the map, allowing keys to be modified. counter = 0; for (i = 0; i < iterations; ++i) { ACE_ASSERT (map.bind_modify_key (i, modified_keys[i]) == 0); ++counter; ACE_ASSERT (map.current_size () == counter); } // Forward iteration... { counter = 0; MAP::iterator end = map.end (); for (MAP::iterator iter = map.begin (); iter != end; ++iter, ++counter) { MAP::value_type entry = *iter; // Recover original key. KEY original_key; ACE_ASSERT (map.recover_key (entry.first (), original_key) == 0); // Make sure recovering keys work. ACE_ASSERT (original_keys[entry.second ()] == original_key); // Obtain value from key. VALUE original_value; ACE_OS::memcpy (&original_value, &original_key[0], sizeof original_value); // Debugging info. ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%d|%d|%d)"), counter, original_value, entry.second ())); } ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\n"))); ACE_ASSERT (counter == iterations); } // Reverse iteration... { counter = iterations; MAP::reverse_iterator end = map.rend (); for (MAP::reverse_iterator iter = map.rbegin (); iter != end; ++iter) { --counter; MAP::value_type entry = *iter; // Recover original key. KEY original_key; ACE_ASSERT (map.recover_key (entry.first (), original_key) == 0); // Make sure recovering keys work. ACE_ASSERT (original_keys[entry.second ()] == original_key); // Obtain value from key. VALUE original_value; ACE_OS::memcpy (&original_value, &original_key[0], sizeof original_value); // Debugging info. ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%d|%d|%d)"), counter, original_value, entry.second ())); } ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\n"))); ACE_ASSERT (counter == 0); } // Search using the modified keys. for (i = 0; i < iterations; ++i) { VALUE j; ACE_ASSERT (map.find (modified_keys[i], j) != -1); ACE_ASSERT (i == j); } // Rmoved keys from map. counter = iterations; for (i = 0; i < iterations; ++i) { ACE_ASSERT (map.unbind (modified_keys[i]) != -1); --counter; ACE_ASSERT (map.current_size () == counter); } // Cleanup. delete[] modified_keys; delete[] original_keys; }
int countQueen(GRID g, int n, int x, int y, const VVV& ava, VVB pos, MAP& hash) { if (n == 0) return 1; int count = 0; if (g[x][y] != '.') { y ++; if (y == g[0].size()) { y = 0; x ++; } if(x == g.size()) return count; count += countQueen(g, n, x, y, ava, pos, hash); count %= 1000000007; return count; } Key key; key.status = pos; key.n = n; MAP::iterator iter = hash.find(key); if(iter != hash.end()) { assert(key.n == iter->first.n); return iter->second; } if(pos[x][y]) { if(validate(g, x, y, ava)) { GRID cp(g); VVB cpb(pos); cp[x][y] = 'q'; resetPos(g, x, y, cpb); int r = countQueen(cp, n - 1, x, y, ava, cpb, hash); count += r; //countQueen(cp, n - 1, x, y, ava, cpb, hash); count %= 1000000007; } } pos[x][y] = false; bool record = (g[x][y] == '.'); y ++; if (y == g[0].size()) { y = 0; x ++; } if(x == g.size()) return count; count += countQueen(g, n, x, y, ava, pos, hash); count %= 1000000007; if (record) hash[key] = count; return count; }
INT solve(INT n, INT k, INT a, INT b, INT c, INT r) { VECT* m = new VECT(); MAP* used = new MAP(); gen(a, b, c, r, k, m, used); VECT* unused = new VECT(); for (INT x = 0; x < 2*k; ++x) { if (used->find(x) == used->end()) { unused->push_back(x); } } m->push_back((*unused)[0]); (*used)[(*unused)[0]] = 1; unused->pop_front(); INT last = m->size() - 1; INT limit = 2 * k; INT currval; printf("limit = %lu\n", limit); while (last != 2 * k) { if (last % 100 == 0) { printf("%d %lu\n", m->size(), last); } currval = (*m)[0]; if (unused->size() == 0) { printf("Big bad error (n=%lu, k=%lu)\n", n, k); return 0; } if (used->find(currval) == used->end()) { (*used)[currval] = 0; } if ((*used)[currval] == 0) { if (currval < (*unused)[0]) { m->push_back(currval); (*used)[currval] = 1; } else { m->push_back((*unused)[0]); (*used)[(*unused)[0]] = 1; unused->pop_front(); unused->push_back(currval); sort(unused->begin(), unused->end()); } } else if ((*used)[currval] > 1) { (*used)[currval] -= 1; m->push_back((*unused)[0]); unused->pop_front(); } else if ((*used)[currval] == 1) { if (currval < (*unused)[0]) { m->push_back(currval); } else { (*used)[currval] = 0; m->push_back((*unused)[0]); (*used)[(*unused)[0]] = 1; unused->pop_front(); unused->push_back(currval); sort(unused->begin(), unused->end()); /* x = (*unused)[unused->size() - 1]; while (true) { if (used->find(x) == used->end()) { unused->push_back(x); break; } ++x; }// */ } } m->pop_front(); ++last; } printf("Done...\n"); printf("m->size() = %lu\n", (INT)m->size()); return m->back(); }
static void ReadTuningMap(std::istream& iStrm, CSoundFile& csf, const size_t = 0) //------------------------------------------------------------------------------- { typedef std::map<WORD, std::string> MAP; typedef MAP::iterator MAP_ITER; MAP shortToTNameMap; ReadTuningMapTemplate<uint16, uint8>(iStrm, shortToTNameMap); //Read & set tunings for instruments std::list<std::string> notFoundTunings; for(UINT i = 1; i<=csf.GetNumInstruments(); i++) { uint16 ui; iStrm.read(reinterpret_cast<char*>(&ui), sizeof(ui)); MAP_ITER iter = shortToTNameMap.find(ui); if(csf.Instruments[i] && iter != shortToTNameMap.end()) { const std::string str = iter->second; if(str == std::string("->MPT_ORIGINAL_IT<-")) { csf.Instruments[i]->pTuning = nullptr; continue; } csf.Instruments[i]->pTuning = csf.GetTuneSpecificTunings().GetTuning(str); if(csf.Instruments[i]->pTuning) continue; #ifdef MODPLUG_TRACKER csf.Instruments[i]->pTuning = csf.GetLocalTunings().GetTuning(str); if(csf.Instruments[i]->pTuning) continue; #endif csf.Instruments[i]->pTuning = csf.GetBuiltInTunings().GetTuning(str); if(csf.Instruments[i]->pTuning) continue; if(str == "TET12" && csf.GetBuiltInTunings().GetNumTunings() > 0) csf.Instruments[i]->pTuning = &csf.GetBuiltInTunings().GetTuning(0); if(csf.Instruments[i]->pTuning) continue; //Checking if not found tuning already noticed. std::list<std::string>::iterator iter; iter = find(notFoundTunings.begin(), notFoundTunings.end(), str); if(iter == notFoundTunings.end()) { notFoundTunings.push_back(str); std::string erm = std::string("Tuning ") + str + std::string(" used by the module was not found."); csf.AddToLog(erm); #ifdef MODPLUG_TRACKER if(csf.GetpModDoc() != nullptr) { csf.GetpModDoc()->SetModified(); //The tuning is changed so the modified flag is set. } #endif // MODPLUG_TRACKER } csf.Instruments[i]->pTuning = csf.GetDefaultTuning(); } else //This 'else' happens probably only in case of corrupted file. { if(csf.Instruments[i]) csf.Instruments[i]->pTuning = csf.GetDefaultTuning(); } } //End read&set instrument tunings }
inline bool contains (MAP& map, typename MAP::key_type const& key) { return map.find(key) != map.end(); }
int main() { MAP table; string name; /****************************************************** * PHASE 0: Load the words in the text file * * into a the table * *******************************************************/ cout << "File name? "; getline(cin, name); ifstream textFile(name.c_str()); if (!textFile) { cerr << "Text file could not be opened!!" << endl; return 0; } string word; int nWords = 0; while(textFile >> word) { nWords++; //remove non-alpha chars word.erase(remove_if(word.begin(), word.end(), isNotAlpha), word.end()); //convert to lower-case letters transform(word.begin(), word.end(), word.begin(), ::tolower); if (word == "") continue; ELEMENT e = make_pair(word,1); cout << word << endl; table.BST_threaded::insert(e); } textFile.close(); /****************************************************** * PHASE 1: Display * * - number of words in the text * * - number of unique words (occurring only once) * * - frequency table * *******************************************************/ //number of words in the text cout << "The number of words in the text file is: " << nWords << endl; cout << "Number of unique words in the file: " << table.size() << endl; cout << "\n\nTable sorted alphabetically:" << endl << endl; BiIterator it = table.begin(); cout << " \tKEY" << "\tCOUNTER" << endl; cout << "==============================\n"; for( ; it != table.end(); it++) { cout << setw(15) << it->first << setw(15) << it->second << endl; } /****************************************************** * PHASE 3: remove all words with counter 1 * * and display table again * *******************************************************/ it = table.begin(); vector<string> temp; for( ; it != table.end(); it++){ if(it->second == 1){ temp.push_back(it->first); } } for(int i = 0; i != temp.size(); i ++){ table.remove(temp[i]); } cout << "Number of words after remove: " << table.size() << endl; cout << "Un-unique table sorted alphabetically:" << endl; cout << " \tKEY" << "\tCOUNTER" << endl; cout << "==============================\n"; for(it = table.begin(); it != table.end(); it++) { cout << setw(15) << it->first << setw(15) << it->second << endl; } /*********************************************************** * PHASE 4: request two words to the user w1 and w2 * * then display all words in the interval [w1,w2] * ************************************************************/ //We assume that the words entered by the user exists in the table cout << "Please enter two words from the list " << endl; string first, last; cin >> first >> last; cout << "First: " << first << " and last: " << last << endl; BiIterator itr = table.find(first); BiIterator stop = table.find(last); itr--; //a dummy to include the last node for(stop; stop!=itr; stop--){ cout << setw(15) << stop->first << setw(15) << stop->second << endl; } return 0; }
static T * s_lookup(const simpleString & s) { T * result = (T *) 0; MAP::const_iterator w = s_map.find(s); if(w!=s_map.end()) result = (*w).second; return result; };