Drkcore

03 09 2008 cpp topcoder Tweet

c++でset (SRM-DIV2-250)

ユーザー登録する際にIDがかぶったら数字のナンバリングをする

setの使い方を覚えた。

class UserName {
public:
  string newMember(vector <string> existingNames, string newName) {
    int n=1;
    set <string> s;
    for(int i=0;i<existingNames.size();++i){
      s.insert(existingNames[i]);
    }

    if(s.count(newName) == 0) return newName; 

    while(1){
      stringstream ss;
      ss << newName << n;
      if(s.count(ss.str()) == 0) return ss.str();
      n++;
    }
};

set便利

About

  • もう5年目(wishlistありマス♡)
  • 最近はPythonとDeepLearning
  • 日本酒自粛中
  • ドラムンベースからミニマルまで
  • ポケモンGOゆるめ

Tag

Python Deep Learning javascript chemoinformatics Emacs sake and more...

Ad

© kzfm 2003-2021