本文整理汇总了C++中unordered_set::insert方法的典型用法代码示例。如果您正苦于以下问题:C++ unordered_set::insert方法的具体用法?C++ unordered_set::insert怎么用?C++ unordered_set::insert使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类unordered_set
的用法示例。
在下文中一共展示了unordered_set::insert方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: make_pair
pair<char, char> firstFirstUnMatchChars(string s, string t, unordered_set<char> &letters) {
int len = min(s.size(), t.size());
int i = 0;
while (i < len && s[i] == t[i]) {
letters.insert(s[i++]);
}
if (i == len) { // s is a prefix of t, or vice versa
if (s.size() > len) {
while (i < s.size()) {
letters.insert(s[i++]);
}
} else if (t.size() > len) {
while (i < t.size()) {
letters.insert(t[i++]);
}
}
return make_pair('A', 'A');
}
// neither s nor t is fully traversed
int iCopy = i;
if (s.size() > i) {
while (i < s.size()) {
letters.insert(s[i++]);
}
}
i = iCopy;
if (t.size() > i) {
while (i < t.size()) {
letters.insert(t[i++]);
}
}
i = iCopy;
return make_pair(s[i], t[i]);
}
示例2: findWords
vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
for(int i = 0; i < words.size(); i++) {
dict.insert(words[i]);
for(int j = 1; j <= words[i].length(); j++) {
prefix.insert(words[i].substr(0, j));
}
}
vector<string> ans;
m = board.size();
if(m == 0)
return ans;
n = board.front().size();
vector<vector<bool> > visited(m, vector<bool>(n, false));
for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++) {
string candidate = "";
candidate += board[i][j];
visited[i][j] = true;
dfs(board, i, j, candidate, visited, ans);
visited[i][j] = false;
}
}
sort(ans.begin(), ans.end());
ans.resize(unique(ans.begin(), ans.end()) - ans.begin());
return ans;
}
示例3: hasCycleUtil
bool hasCycleUtil(int parent,Vertex *curr,unordered_set<int> &visited,
unordered_set<int> &recurStack)
{
visited.insert(curr->key);
recurStack.insert(curr->key);
cout<<"visiting "<<curr->key<<endl;
for(auto cn:curr->_neighbours)
{
if(cn==parent)
continue;
if(recurStack.find(cn) != recurStack.end())
{
cout<<"already visited "<<cn<<endl;
return true;
}
if(visited.find(cn) == visited.end())
{
Vertex *neighbour = _graph[cn];
if(hasCycleUtil(curr->key,neighbour,visited,recurStack))
{
return true;
}
}
}
recurStack.erase(curr->key);
cout<<"removing from stack"<<curr->key<<endl;
return false;
}
示例4: ladderLength
int ladderLength(string beginWord, string endWord, unordered_set<string>& wordDict) {
wordDict.insert(beginWord);
wordDict.insert(endWord);
unordered_map<string, int> depth;
unordered_map<string, bool> accessed;
for (string w: wordDict) {
depth[w] = INT_MAX;
accessed[w] = false;
}
depth[beginWord] = 1;
queue<string> q({beginWord});
while (!q.empty()) {
string cur = q.front();
q.pop();
if (accessed[cur]) {
continue;
}
auto next_names = nextNames(cur, wordDict);
for (string name: next_names) {
if (!accessed[name]) {
depth[name] = min(depth[name], depth[cur] + 1);
q.push(name);
}
}
accessed[cur] = true;
}
return depth[endWord]%INT_MAX;
}
示例5: SampleMinibatch
void MMSBModel::SampleMinibatch(unordered_set<VIndex>& vertex_batch,
set<pair<VIndex, VIndex> >& link_batch, const Count batch_size) {
vertex_batch.clear();
for (int i = 0; i < batch_size; ++i) {
while (true) {
// randomly sample a vertex from the map<.,.> vertices_
auto it = vertices_.begin();
uint32 nstep = Context::randUInt64() % vertices_.size();
std::advance(it, nstep);
VIndex v = it->first;
if (vertex_batch.find(v) == vertex_batch.end()) { // to avoid duplicate
vertex_batch.insert(v);
break;
}
}
}
CollectLinks(link_batch, vertex_batch);
for (const auto& link : link_batch) {
vertex_batch.insert(link.first);
vertex_batch.insert(link.second);
}
//LOG(INFO)
// << "iter " << iter_ << ", "
// << vertex_batch.size() << " vertexes, "
// << link_batch.size() << " links";
}
示例6: findOrderIHelper
bool findOrderIHelper(int node,unordered_map<int,vector<int> >& graph, unordered_set<int>& visited,unordered_set<int>& tmpVisited,vector<int>& sortedOrder)
{
if(tmpVisited.find(node) != tmpVisited.end())
return false;
if(visited.find(node) == visited.end())
{
tmpVisited.insert(node);
for(int i = 0; i < graph[node].size(); i++)
{
if(findOrderIHelper(graph[node][i],graph,visited,tmpVisited,sortedOrder) == false)
{
return false;
}
}
visited.insert(node);
tmpVisited.erase(node);
sortedOrder.push_back(node);
}
return true;
}
示例7: process_incoming
void process_incoming(multiset<order, IN> &incoming, multiset<order, WAIT> &waiting, int seq)
{
int size, price; cin >> size >> price;
auto top_waiting = waiting.begin();
for ( ; size && top_waiting != waiting.end(); ++top_waiting)
{
if (can_trade(price, top_waiting, waiting))
{
int trade_price = top_waiting->price;
int trade_size = min(top_waiting->size, size);
cout << "TRADE " << trade_size << " " << trade_price << '\n';
size -= trade_size;
top_waiting->size -= trade_size;
price2size[trade_price] -= trade_size;
if (!top_waiting->size)
gone.insert(top_waiting->seq);
else
break;
}
else break;
}
waiting.erase(waiting.begin(), top_waiting);
if (size)
{
auto p2s_pit = price2size.insert({price, size});
if (!p2s_pit.second)
p2s_pit.first->second += size;
auto it = incoming.insert(incoming.begin(), {price, seq, size});
add_seq2iter_map(seq, it, &incoming);
}
else
gone.insert(seq);
}
示例8: getVarTripleMap
// _____________________________________________________________________________
void QueryPlanner::getVarTripleMap(
const ParsedQuery& pq,
unordered_map<string, vector<SparqlTriple>>& varToTrip,
unordered_set<string>& contextVars) const {
for (auto& t: pq._whereClauseTriples) {
if (isVariable(t._s)) {
varToTrip[t._s].push_back(t);
}
if (isVariable(t._p)) {
varToTrip[t._p].push_back(t);
}
if (isVariable(t._o)) {
varToTrip[t._o].push_back(t);
}
if (t._p == IN_CONTEXT_RELATION) {
if (isVariable(t._s) || isWords(t._o)) {
contextVars.insert(t._s);
}
if (isVariable(t._o) || isWords(t._s)) {
contextVars.insert(t._o);
}
}
}
}
示例9: findPrimefactors
// Utility function to store prime factors of a number
void findPrimefactors(unordered_set<int> &s, int n)
{
// Print the number of 2s that divide n
while (n%2 == 0)
{
s.insert(2);
n = n/2;
}
// n must be odd at this point. So we can skip
// one element (Note i = i +2)
for (int i = 3; i <= sqrt(n); i = i+2)
{
// While i divides n, print i and divide n
while (n%i == 0)
{
s.insert(i);
n = n/i;
}
}
// This condition is to handle the case when
// n is a prime number greater than 2
if (n > 2)
s.insert(n);
}
示例10: insertEdge
void insertEdge(char start, char end, int weight){
vertices.insert(start);
vertices.insert(end);
string string1, string2;
if (edges.find(string1.append(1, start).append(1, end)) == edges.end() &&
edges.find(string2.append(1, end).append(1, start)) == edges.end())
edges.insert(make_pair(string1, weight));
}
示例11: gen_primes
void gen_primes(long long n, unordered_set<long long> &primes){
if(n>= 2){
primes.insert(2);
for(long long i = 3; i <=n; i += 2){
if(is_prime(i)) primes.insert(i);
}
}
}
示例12: bfs
// create a queue to track the distance of each word in wordList from beginWord
void bfs(unordered_map<string, vector<string>> &transMap, unordered_map<string, int> &distMap,
string &beginWord, string &endWord, unordered_set<string> &wordList) {
queue<string> q;
q.push(beginWord);
// make sure beginWord and endWord is in wordList
wordList.insert(beginWord);
wordList.insert(endWord);
// start from the beginWord
distMap[beginWord] = 0;
while (!q.empty())
{
int count = q.size();
bool isEndFound = false; // flag if found the endWord
for (int i = 0; i < count; i++) // bfs by level
{
string word = q.front();
q.pop();
vector<string> transList = transform(word, wordList);
vector<string> *curList = &transMap[word];
for (int j = 0; j < transList.size(); j++)
{
string str(transList[j]);
// update possible transformations for each word in transList
curList->push_back(str);
// update distance for each word in transList if it is not in distMap
if (distMap.find(str) == distMap.end())
{
distMap[str] = distMap[word] + 1;
if (str == endWord)
{
// set flag
isEndFound = true;
}
else
{
q.push(str);
}
}
}
if (isEndFound)
{
// quit current level, go to next level
break;
}
}
}
return;
}
示例13: findLadders
vector<vector<string>> findLadders(string start, string end, unordered_set<string> &dict) {
vector<vector<string>> ret;
dict.insert(start);
dict.insert(end);
build(dict);
bfs(ID[start] , ID[end]);
vector<int> temp;
gen(ID[start] , ID[end] , ret , temp);
return ret;
}
示例14: splitdata
/**** start = "hit"
**** end = "cog"
******dict = ["hot", "dot", "dog", "lot", "log"]
*/
void splitdata(string temp, unordered_set<string>& dict)
{
int pos = -1;
while ((pos = temp.find(",")) != -1)
{
string tempsub = temp.substr(0,pos);
temp = temp.substr(pos + 1, temp.length() - pos - 1);
dict.insert(tempsub);
}
dict.insert(temp);
}
示例15: findLadders
/**
* @param start, a string
* @param end, a string
* @param dict, a set of string
* @return a list of lists of string
*/
vector<vector<string> > findLadders(string start, string end, unordered_set<string> &dict) {
ans.clear();
bt.clear();
dict.insert(start);
dict.insert(end);
vector<vector<string> > a(2);
string s1, s2;
int f, nf;
a[0].push_back(start);
f = 1;
nf = !f;
int i, j, k, n, len;
char ch;
while (true) {
a[f].clear();
n = a[nf].size();
for (i = 0; i < n; ++i) {
dict.erase(a[nf][i]);
}
for (i = 0; i < n; ++i) {
s1 = s2 = a[nf][i];
len = s1.length();
for (j = 0; j < len; ++j) {
ch = s1[j];
for (k = 0; k < 26; ++k) {
if (ch == 'a' + k) {
continue;
}
s1[j] = 'a' + k;
if (dict.find(s1) == dict.end()) {
continue;
}
a[f].push_back(s1);
bt[s1].insert(s2);
}
s1[j] = ch;
}
}
if (a[f].empty() || bt.find(end) != bt.end()) {
break;
}
f = !f;
nf = !f;
}
if (bt.find(end) == bt.end()) {
return ans;
}
res.clear();
backTrace(end);
return ans;
}