1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | #include "stdafx.h" #include <cstdio> #include "stdafx.h" #include <cstdio> #include <unordered_set> #include <set> #include <unordered_map> #include <map> #include <iostream> #include <string> #include <algorithm> #include <vector> using namespace std; template<class T> class test { public: T a; T b; }; string func(string sam) { sam += " world"; return sam; } int main() { // pair은 대소비교 및 각 원소 접근이 가능하다. STL 원소로도 사용 가능. pair<int, int> p(100, 101); pair<int, int> p2(99, 101); pair<int, int> p3(100, 102); vector<pair<int, int>> v1; v1.push_back(p); v1.push_back(p2); v1.push_back(p3); sort(v1.begin(), v1.end()); // #include <algorithm> vector<pair<int, int>>::iterator it1; for (it1 = v1.begin(); it1 != v1.end(); it1++) { cout << it1->first << " " << (*it1).second << endl; } //map은 red black tree를 사용해서 insert delete search가 O(logN)이다. 자동정렬된다. map<string, int> data; //INSERT data.insert(map<string, int>::value_type("void knife", 400)); data.insert(map<string, int>::value_type("void knife", 10)); // 먼저온놈만 남는다!! data.insert(map<string, int>::value_type("killer sword", 330)); data.insert(map<string, int>::value_type("gorgon hammer", 220)); data.insert(map<string, int>::value_type("wand", 90)); data.insert(data.begin(), map<string, int>::value_type("zsword", 550)); // 영향 없음.. map<string, int>::iterator i1; for (i1 = data.begin(); i1 != data.end(); i1++) { cout << i1->first << " " << i1->second << endl; } //FIND i1 = data.find("wand"); if (i1 != data.end()) { cout << "found" << endl; //i1->first = s1; 키값은 바꿀 수 없다!! 다시 insert 해줘야됨. erase 하고 i1->second = 1000; } //ERASE data.erase("wand"); i1 = data.begin(); for (int i = 0; i < 3; i++) { i1++; } data.erase(i1); for (i1 = data.begin(); i1 != data.end(); i1++) { cout << i1->first << " " << i1->second << endl; } //UPPER BOUNDS map<int,string> students; for (int i = 0; i < 10; i++) { string z1 = "sam hamington"; z1.append(1, i + '0'); students.insert(map<int,string>::value_type(i*10,z1)); } map<int, string>::iterator im; im = students.upper_bound(23); for (map<int, string>::iterator im2 = im; im2 != students.end(); im2++) { cout << im2->first << " " << im2->second << endl; } //set은 red black tree를 사용해서 insert delete search가 O(logN)이다. set<string> jset; //INSERT jset.insert("lexiographically"); jset.insert("japan"); jset.insert("jaPan"); jset.insert("jApan"); jset.insert("adam"); //ERASE jset.erase(jset.begin()); jset.erase("jApan"); //FIND if (jset.find("japan") != jset.end()) { cout << "found" << endl; } //ITERATE set<string>::iterator itr; for (itr = jset.begin(); itr != jset.end(); itr++) { cout << *itr << endl; } //BOUNDS set<int> sset; for (int i = 1; i < 20; i++) sset.insert(i*10); set<int>::iterator sl, su; sl = sset.upper_bound(31); su = sset.upper_bound(122); for (set<int>::iterator sss = sl; sss != su; sss++) { cout << *sss << endl; } //unordered map이 우리가 기대하는 그 hash table이다.. //insert delete index가 모두 상수이다. space complexity는 O(n) unordered_map<string, int> characters; //INSERT characters.insert(unordered_map<string, int>::value_type("john doe", 10000)); characters.insert(unordered_map<string, int>::value_type("jinhyuk jeon", 1990000)); characters.insert(unordered_map<string, int>::value_type("sam white", 1000880)); characters.insert(unordered_map<string, int>::value_type("baeyean whang", 10887000)); characters.insert(unordered_map<string, int>::value_type("junho kim", 1000550)); characters.insert(unordered_map<string, int>::value_type("junyong kim", 1003300)); unordered_map<string, int>::iterator j1; for (j1 = characters.begin(); j1 != characters.end(); j1++) { cout << j1->first << " " << j1->second << endl; } //FIND j1 = characters.find("sam white"); if ( j1 != characters.end()) { cout << "founddd" << endl; j1->second = 111; } //DELETE j1 = characters.begin(); for (int i = 0; i < 3; i++) { j1++; } characters.erase(j1); characters.erase("john doe"); for (j1 = characters.begin(); j1 != characters.end(); j1++) { cout << j1->first << " " << j1->second << endl; } //SIZE, CLEAR cout << "size" << characters.size() << endl; characters.clear(); //unordered set은 hash table을 사용해서 average index time이 constant이다. unordered_set<string> stringset; // INSERT stringset.insert("hello world"); stringset.insert("bye"); stringset.insert("bye"); // 아무 일도 일어나지 않음 // insert나 초기화의 순서가 iterator 순서를 보장하지 않는다!! unordered_set<int> jack; for (int i = 0; i < 100; i++) { jack.insert(i); } unordered_set<int>::iterator it2; for (it2 = jack.begin(); it2 != jack.end(); it2++) { cout << *it2 << " "; } cout << endl; // FIND if (stringset.find("hello world") == stringset.end()) { cout << "not found" << endl; } else { cout << "found" << endl; } cout << stringset.size() << endl; // ITERATE unordered_set<string>::iterator it; for (it = stringset.begin(); it != stringset.end(); it++) { cout << *it << endl; } // ERASE std::unordered_set<std::string> myset = { "USA","Canada","France","UK","Japan","Germany","Italy", "jinhyuk" }; myset.erase(myset.begin()); // erasing by iterator myset.erase("France"); // erasing by key myset.erase(myset.find("Japan"), myset.end()); // erasing by range for (it = myset.begin(); it != myset.end(); it++) { cout << *it << endl; } // CLEAR myset.clear(); //multiset cout << "start here" << endl << endl; multiset<int> mms; mms.insert(1); mms.insert(2); mms.insert(1); mms.insert(3); mms.insert(1); for (multiset<int>::iterator iii = mms.begin(); iii != mms.end(); iii++) { cout << *iii << endl; } if (mms.find(1) != mms.end()) { cout << "found" << endl; } // O(logN) cout << "counts " << mms.count(1) << endl; // strings string sam = "hi im string"; char * sam3 = new char[50]; // char sam3[50] 도 똑같이 동작한다 scanf("%[^\n]", sam3); //char 배열을 string에 대입 가능 sam = sam3; //string sam4 = sam3; cout << "ss4 " << sam << endl; //scanf("%s", &sam); 스트링 입력 안된다 cout << sam[3] << endl; cout << sam.size() << endl; int i = 0; while (sam[i] != '\0') { cout << sam[i] << endl; i++; } sam = func("hello"); cout << sam.c_str() << endl; string sam2 = "what is this"; cout << sam2 << endl; // need to include <string> vector<string> v2; v2.push_back("hello"); v2.push_back("world"); cout << v1.size() << " " << v2.at(0) << " " << v2.at(1) << endl; while (1) {} return 0; } | cs |
'2019 > c++' 카테고리의 다른 글
strings(3) (0) | 2018.03.22 |
---|---|
Strings(2) (0) | 2018.03.22 |
dynamic allocation of 2-dimension array (0) | 2018.03.20 |
c++ reference variables (0) | 2018.03.18 |
Template classes, operator overloading (0) | 2018.03.18 |