本文整理汇总了C++中unordered_set::begin方法的典型用法代码示例。如果您正苦于以下问题:C++ unordered_set::begin方法的具体用法?C++ unordered_set::begin怎么用?C++ unordered_set::begin使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类unordered_set
的用法示例。
在下文中一共展示了unordered_set::begin方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: init
void init(const unordered_set<string> & wordlist, const string& beginWord,
const string& endWord) {
//init endWords
for (unordered_set<string>::const_iterator iter = wordlist.begin();
iter != wordlist.end(); ++iter) {
if (isOneStep(endWord, *iter))
this->endWords.insert(*iter);
}
//init beginWords
for (unordered_set<string>::const_iterator iter = wordlist.begin();
iter != wordlist.end(); ++iter) {
if (isOneStep(beginWord, *iter))
this->beginWords.insert(*iter);
}
//init oneStep
for (unordered_set<string>::const_iterator i = wordlist.begin();
i != wordlist.end(); ++i) {
vector<string> tmp;
for (unordered_set<string>::const_iterator j = wordlist.begin();
j != wordlist.end(); ++j)
if (isOneStep(*i, *j)) {
tmp.push_back(*j);
}
this->oneStep[*i] = tmp;
}
}
示例2: wordBreak
bool wordBreak(string s, unordered_set<string> &dict) {
for(int i = 0; i < s.size(); i++){
bool found = false;
for(auto it = dict.begin(); it != dict.end(); it++){
if(it->find(s.substr(i, 1)) != string::npos){
found = true;
break;
}
}
if(found == false){
return false;
}
}
for(auto it = dict.begin(); it != dict.end(); it++){
if(s.find(*it) == 0){
if(it->size() == s.size()){
return true;
}
if(wordBreak(s.substr(it->size(), string::npos), dict)){
return true;
};
}
}
return false;
}
示例3: topSort
void topSort(unordered_set<char> &letters, vector<pair<char, char>> &partialOrders, string &order) {
if (partialOrders.empty()) {
for (auto it = letters.begin(); it != letters.end(); it++) {
order.push_back(*it);
}
return;
}
// build adjacency list
unordered_map<char, unordered_set<char>> adj;
for (int i = 0; i < partialOrders.size(); i++) {
pair<char, char> e = partialOrders[i];
adj[e.first].insert(e.second);
}
unordered_set<char> visited;
unordered_set<char> inStack;
stack<char> s;
for (auto it = letters.begin(); it != letters.end(); it++) {
if (visited.find(*it) == visited.end()) {
if (dfs(adj, *it, visited, inStack, s)) { // cycle found
return;
}
}
}
// no cycle found
while (!s.empty()) {
order.push_back(s.top());
s.pop();
}
return;
}
示例4: bfs
// looking for connection from both ends
bool bfs(unordered_set<string> &forward, unordered_set<string> &backward, bool isForward,
unordered_set<string> &wordList, unordered_map<string, vector<string>> &transMap) {
if (forward.empty() || backward.empty())
{
return false;
}
// always do bfs with less nodes
if (forward.size() > backward.size())
{
return bfs(backward, forward, !isForward, wordList, transMap);
}
// remove added node in the wordList to avoid duplication
unordered_set<string>::iterator it;
for (it = forward.begin(); it != forward.end(); it++)
{
wordList.erase(*it);
}
for (it = backward.begin(); it != backward.end(); it++)
{
wordList.erase(*it);
}
unordered_set<string> nextlevel;
bool isConnected = false;
for (it = forward.begin(); it != forward.end(); it++)
{
string word = *it;
for (int i = 0; i < word.size(); i++)
{
for (char ch = 'a'; ch <= 'z'; ch++)
{
if (word[i] != ch)
{
// word is in forward list, str is in backward list
string str(word);
str[i] = ch;
if (backward.find(str) != backward.end())
{
isConnected = true;
connect(word, str, isForward, transMap);
}
else if (!isConnected && wordList.find(str) != wordList.end())
{
nextlevel.insert(str);
connect(word, str, isForward, transMap);
}
}
}
}
}
return isConnected || bfs(nextlevel, backward, isForward, wordList, transMap);
}
示例5: tripleSetToString
string tripleSetToString(const unordered_set<Triple>& st)
{
string rval;
for (auto it=st.begin(); it!=st.end(); it++)
{
if(it!=st.begin())
rval += kInternalSetDelim;
rval += tripleToString(*it);
}
return rval;
}
示例6: unionSet
uint64_t unionSet(unordered_set<minimizer>& set1,unordered_set<minimizer>& set2){
uint64_t res(0);
for (auto it=set2.begin(); it!=set2.end(); ++it){
++res;
}
for (auto it=set1.begin(); it!=set1.end(); ++it){
if(set2.count(*it)==0){
++res;
}
}
return res;
}
示例7: wordBreakII
vector<string> wordBreakII(string s, unordered_set<string> &dict) {
vector<string> res;
vector<vector<unordered_set<string>::iterator> > record;
/*
printf("%s\n", s.c_str());
for (unordered_set<string>::iterator usit = dict.begin(); usit != dict.end(); ++usit)
{
printf("%s\n", usit->c_str());
}
*/
int len = (int)s.size();
int *st = new int[len+1];
memset(st, 0, sizeof(int) * (len+1));
st[0] = 1;
for (int i = 0; i <= len; ++i) record.push_back(vector<unordered_set<string>::iterator>());
unordered_set<string>::iterator usit = dict.begin();
for (int i = 1; i <= len; ++i)
{
for (usit = dict.begin(); usit != dict.end(); ++usit)
{
int cur_len = (int)usit->size();
int dis = i - cur_len;
if (dis < 0) continue; //too short, impossible
const char *start = s.c_str() + dis;
if (st[dis] == 1 && strncmp(start, usit->c_str(), cur_len) == 0)
{
st[i] = 1;
record[i].push_back(usit);
}
}
}
//debug
/*
for (size_t i = 1; i < record.size(); ++i)
{
printf("%lu ==> ", i);
for (size_t j = 0; j < record[i].size(); j++) printf("%s ", record[i][j]->c_str());
printf("\n");
}
printf("\n");
*/
//construct paths
if (st[len] == 1) constructSolutions(record, res, (int)record.size()-1, "");
//if (st[len] == 1) printf("YES!\n");
return res;
}
示例8: values
void values(unordered_set<int> &s,int a,int b)
{
s.insert(a);
s.insert(b);
if(s.size()==1) return;
int n;
std::vector<int> src,dst;
std::vector<int>::iterator iti,itj;
std::unordered_set<int> v;
size_t lastsz=0;
while(true){
copy(s.begin(),s.end(),std::back_inserter(src));
for(iti=src.begin();iti!=src.end();++iti){
for(itj=src.begin();itj!=src.end();++itj){
n = *iti-*itj;
if(n>0)
v.insert(n);
}
}
/*
cout<<"差值[原始]:";
std::for_each(v.begin(),v.end(),[](int n){cout<<n<<'\t';});
cout<<endl;
*/
src.clear();
copy(v.begin(),v.end(),std::back_inserter(dst));
copy(s.begin(),s.end(),std::back_inserter(src));
std::sort(dst.begin(),dst.end());
std::sort(src.begin(),src.end());
//unordered_set difference
std::vector<int> tmp(a/(a-b)+1);
iti = std::set_difference(dst.begin(),dst.end(),src.begin(),src.end(),tmp.begin());
tmp.resize(iti-tmp.begin());
/*
cout<<"差值:";
std::for_each(tmp.begin(),tmp.end(),[](int n){cout<<n<<'\t';});
cout<<endl;
*/
if(tmp.empty()) break;
s.insert(*tmp.begin());
if(s.size()==lastsz) break;
lastsz = s.size();
//std::for_each(v.begin(),v.end(),[&s](int n){cout<<n<<'\t';s.insert(n);});
v.clear();
src.clear();
dst.clear();
}
}
示例9:
void
PerceptronModel::ComputeFeaturesToUpdate(const CandidateSet &example,
unordered_set<int> &
gold_features_to_update,
unordered_set<int> &
best_scoring_features_to_update)
const {
// Collect gold features that are not in best-scoring candidate.
const FeatureVector<int,double> &gold_features =
example.GetGold().features();
const FeatureVector<int,double> &best_scoring_features =
example.GetBestScoring().features();
if (DEBUG >= 2) {
cerr << "Gold index: " << example.gold_index()
<< "; best scoring index: " << example.best_scoring_index()
<< endl;
cerr << "Original gold features: " << gold_features << endl
<< "Original best scoring features: " << best_scoring_features << endl;
}
gold_features.GetNonZeroFeatures(gold_features_to_update);
best_scoring_features.RemoveEqualFeatures(gold_features,
gold_features_to_update);
if (DEBUG >= 2) {
cerr << "Time:" << time_.to_string() << ": new gold features: [";
for (unordered_set<int>::const_iterator it =
gold_features_to_update.begin();
it != gold_features_to_update.end();
++it) {
cerr << " " << *it;
}
cerr << "]" << endl;
}
best_scoring_features.GetNonZeroFeatures(best_scoring_features_to_update);
gold_features.RemoveEqualFeatures(best_scoring_features,
best_scoring_features_to_update);
if (DEBUG >= 2) {
cerr << "Time:" << time_.to_string() << ": new best scoring features: [";
for (unordered_set<int>::const_iterator it =
best_scoring_features_to_update.begin();
it != best_scoring_features_to_update.end();
++it) {
cerr << " " << *it;
}
cerr << "]" << endl;
}
}
示例10: wordBreak
bool wordBreak(string s, unordered_set<string> &dict) {
int n = s.size();
int q[n + 1];
memset(q, 0, sizeof(q));
q[0] = 1;
unordered_set<string>::iterator it;
string t;
for (int i = 1; i <= n; i++) {
for (it = dict.begin(); it != dict.end(); ++it) {
t = *it;
int j = t.size();
if (i < j || !q[i - j]) continue;
bool flag = true;
for (int k = j; k >= 1; k--) {
if (s[i - j + k - 1] != t[k - 1]) {
flag = false;
break;
}
}
if (flag) {
q[i] = 1;
}
}
}
return (q[n] == 1);
}
示例11: dfs
void dfs(string start, string &end, unordered_set<string> &dict,
int curL, int minL,
vector<string> &vis, vector<string> &ret){
if(curL > minL)
return;
int len = dict.begin()->size();
for(int i = 0; i < len; i++){
string v = start;
char oldc = start[i];
for(char c = 'a'; c <= 'z'; c++){
if(c == oldc)
continue;
v[i] = c;
if(v == end){
vis.push_back(v);
ret.append(vis);
vis.pop_back();
return;
}
if(dict.find(v) == dict.end())
continue;
vis.push_back(v);
dfs(v, end, dict, curL+1, minL, vis, ret);
vis.pop_back();
}
}
}
示例12: manageConstructionEvent
static void manageConstructionEvent(color_ostream& out) {
if ( handlers[EventType::CONSTRUCTION].empty() )
return;
unordered_set<df::construction*> constructionsNow(df::global::world->constructions.begin(), df::global::world->constructions.end());
multimap<Plugin*,EventHandler> copy(handlers[EventType::CONSTRUCTION].begin(), handlers[EventType::CONSTRUCTION].end());
for ( auto a = constructions.begin(); a != constructions.end(); a++ ) {
df::construction* construction = *a;
if ( constructionsNow.find(construction) != constructionsNow.end() )
continue;
for ( auto b = copy.begin(); b != copy.end(); b++ ) {
EventHandler handle = (*b).second;
handle.eventHandler(out, (void*)construction);
}
}
for ( auto a = constructionsNow.begin(); a != constructionsNow.end(); a++ ) {
df::construction* construction = *a;
if ( constructions.find(construction) != constructions.end() )
continue;
for ( auto b = copy.begin(); b != copy.end(); b++ ) {
EventHandler handle = (*b).second;
handle.eventHandler(out, (void*)construction);
}
}
constructions.clear();
constructions.insert(constructionsNow.begin(), constructionsNow.end());
}
示例13: wordBreak
int WordBreak::wordBreak(string s, unordered_set<string> &dict) {
static vector<string> words;
int combination = 0;
unordered_set<string>::iterator iter = dict.begin();
if (s.empty())
return false;
while (iter != dict.end()) {
string w = *iter;
if (!s.compare(0, w.length(), w)) {
words.push_back(w);
if (s.length() == w.length()) {
dumpWords(words);
combination++;
}
combination += wordBreak(s.substr(w.length(), s.length()-w.length()), dict);
words.pop_back();
}
iter++;
}
return combination;
}
示例14: searchwithDoubleBFS
// 双向BFS搜索
// -------- ----------
// | | | |
// |words1 | <---------------> | words2 |
// | | | |
// --------- ----------
// 不断地从两端向中心搜索,直至搜索到中间单词temp在另一个词袋中
int searchwithDoubleBFS(unordered_set<string>& words1, unordered_set<string>& words2,
unordered_set<string>& dict, int level) {
if (words1.empty()) return 0;
if (words1.size() > words2.size())
return searchwithDoubleBFS(words2, words1, dict, level);
unordered_set<string> words3;
for (auto it = words1.begin(); it != words1.end(); ++it) {
string word = *it;
for (size_t i = 0; i < word.size(); ++i) {
int ch = word[i];
for (int k = 0; k < 26; ++k) {
int ch2 = 'a' + k;
if (ch2 != ch) {
string temp = word;
temp[i] = ch2;
if (words2.find(temp) != words2.end()) {
return level+1;
} else {
if (dict.find(temp) != dict.end()) {
dict.erase(temp);
words3.insert(temp);
}
}
}
}
}
}
return searchwithDoubleBFS(words3, words2, dict, level+1);
}
示例15: ladderLength
int ladderLength(string start, string end, unordered_set<string> &dict) {
int cur_steps = 0, cur_num = 1, res = -1, next_num = 0;
queue<string> Q;
unordered_set<string>::iterator it;
unordered_set<string> visited;
Q.push(start);
while(!Q.empty()) {
string tmp = Q.front();
Q.pop();
if(judge_string(tmp, end)) {
res = (cur_num == 0 ? cur_steps + 1 : cur_steps) + 2;
break;
}
for(it = dict.begin(); it != dict.end(); it++) {
if(visited.find(*it) == dict.end() && judge_string(tmp, (string)(*it))) {
Q.push(*it);
visited.insert(*it);
next_num++;
}
}
cur_num--;
if(cur_num == 0) {
cur_num = next_num;
next_num = 0;
cur_steps++;
}
}
return res;
}