ผลต่างระหว่างรุ่นของ "Psl/stl"
ไปยังการนำทาง
ไปยังการค้นหา
Jittat (คุย | มีส่วนร่วม) |
Jittat (คุย | มีส่วนร่วม) |
||
(ไม่แสดง 8 รุ่นระหว่างกลางโดยผู้ใช้คนเดียวกัน) | |||
แถว 1: | แถว 1: | ||
+ | : ''หน้านี้เป็นส่วนหนึ่งของ [[Problem solving lab]]'' | ||
== List / iterator == | == List / iterator == | ||
แถว 54: | แถว 55: | ||
lst.erase(i20); | lst.erase(i20); | ||
print_list(lst); | print_list(lst); | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | == Set == | ||
+ | |||
+ | <syntaxhighlight lang="cpp"> | ||
+ | #include <set> | ||
+ | #include <cstdio> | ||
+ | |||
+ | using namespace std; | ||
+ | |||
+ | set<int> iset; | ||
+ | |||
+ | void print_set(set<int>& s) | ||
+ | { | ||
+ | for(set<int>::iterator i = s.begin(); | ||
+ | i != s.end(); ++i) { | ||
+ | printf("%d\n", *i); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | main() | ||
+ | { | ||
+ | set<int> s; | ||
+ | s.insert(10); | ||
+ | s.insert(100); | ||
+ | s.insert(50); | ||
+ | s.insert(30); | ||
+ | |||
+ | print_set(s); | ||
+ | |||
+ | s.insert(-100); | ||
+ | printf("%d\n",*(s.begin())); | ||
+ | s.insert(-200); | ||
+ | printf("%d\n",*(s.begin())); | ||
+ | s.insert(-150); | ||
+ | printf("%d\n",*(s.begin())); | ||
+ | |||
+ | set<int>::iterator i = s.find(-200); | ||
+ | if(i != s.end()) { | ||
+ | s.erase(i); | ||
+ | } | ||
+ | printf("%d\n",*(s.begin())); | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | === pair + sort === | ||
+ | <syntaxhighlight lang="cpp"> | ||
+ | #include <iostream> | ||
+ | #include <set> | ||
+ | using namespace std; | ||
+ | main() | ||
+ | { | ||
+ | set<pair<int,int>> s; | ||
+ | int n; | ||
+ | |||
+ | cin >> n; | ||
+ | |||
+ | for(int i=0; i<n; i++) { | ||
+ | int y; | ||
+ | cin >> y; | ||
+ | s.insert(make_pair(y,i)); | ||
+ | } | ||
+ | |||
+ | for(auto i = s.begin(); i != s.end(); i++) { | ||
+ | cout << i->first << endl; | ||
+ | } | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | === pair / erase === | ||
+ | <syntaxhighlight lang="cpp"> | ||
+ | #include <set> | ||
+ | #include <cstdio> | ||
+ | |||
+ | using namespace std; | ||
+ | |||
+ | typedef pair<int,int> int_pair; | ||
+ | set<int_pair> s; | ||
+ | |||
+ | main() | ||
+ | { | ||
+ | s.insert(make_pair(0,0)); | ||
+ | |||
+ | pair<set<int_pair>::iterator,bool> res = s.insert(int_pair(10,0)); | ||
+ | |||
+ | s.insert(int_pair(15,10)); | ||
+ | s.insert(int_pair(10,7)); | ||
+ | |||
+ | s.erase(res.first); | ||
+ | |||
+ | for(set<int_pair>::iterator i = s.begin(); | ||
+ | i != s.end(); i++) { | ||
+ | printf("%d, %d\n", i->first, i->second); | ||
+ | } | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | == map == | ||
+ | <syntaxhighlight lang="cpp"> | ||
+ | #include <map> | ||
+ | #include <cstdio> | ||
+ | |||
+ | using namespace std; | ||
+ | |||
+ | map<int,int> m; | ||
+ | |||
+ | main() | ||
+ | { | ||
+ | m[100] = 20; | ||
+ | m[1000000] = 30; | ||
+ | m[100]++; | ||
+ | printf("%d\n",m[100]); | ||
+ | printf("%d\n",m[1000000]); | ||
+ | |||
+ | if(m.find(200) != m.end()) { | ||
+ | m[200] += 10; | ||
+ | } else { | ||
+ | m[200] = 1; | ||
+ | } | ||
+ | m.insert(make_pair(1000,1000000)); | ||
+ | |||
+ | for(map<int,int>::iterator i = m.begin(); | ||
+ | i != m.end(); i++) { | ||
+ | printf("%d => %d\n",i->first, i->second); | ||
+ | } | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | == lower_bound, upper_bound == | ||
+ | ใช้ได้ทั้ง set และ map | ||
+ | |||
+ | <syntaxhighlight lang="cpp"> | ||
+ | #include <map> | ||
+ | #include <cstdio> | ||
+ | |||
+ | using namespace std; | ||
+ | |||
+ | map<int,int> m; | ||
+ | |||
+ | main() | ||
+ | { | ||
+ | m[100] = 20; | ||
+ | m[1000000] = 30; | ||
+ | m[100]++; | ||
+ | m.insert(make_pair(1000,1000000)); | ||
+ | |||
+ | printf("%d\n",m.lower_bound(999)->first); | ||
+ | printf("%d\n",m.lower_bound(1000)->first); | ||
+ | printf("%d\n",m.upper_bound(999)->first); | ||
+ | printf("%d\n",m.upper_bound(1000)->first); | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> |
รุ่นแก้ไขปัจจุบันเมื่อ 03:02, 12 กุมภาพันธ์ 2561
- หน้านี้เป็นส่วนหนึ่งของ Problem solving lab
เนื้อหา
List / iterator
#include <list>
#include <cstdio>
using namespace std;
list<int> lst;
main()
{
lst.push_back(10);
lst.push_back(20);
lst.push_back(30);
list<int>::iterator i = lst.begin();
while(i != lst.end()) {
printf("%d\n", *i);
i++;
}
}
erase โดยระบุ iterator
#include <list>
#include <cstdio>
using namespace std;
list<int> lst;
void print_list(list<int>& lst)
{
list<int>::iterator i = lst.begin();
while(i != lst.end()) {
printf("%d\n", *i);
i++;
}
}
main()
{
lst.push_back(10);
lst.push_back(20);
list<int>::iterator i20 = lst.end();
i20--;
lst.push_back(30);
print_list(lst);
printf("-----\n");
lst.erase(i20);
print_list(lst);
}
Set
#include <set>
#include <cstdio>
using namespace std;
set<int> iset;
void print_set(set<int>& s)
{
for(set<int>::iterator i = s.begin();
i != s.end(); ++i) {
printf("%d\n", *i);
}
}
main()
{
set<int> s;
s.insert(10);
s.insert(100);
s.insert(50);
s.insert(30);
print_set(s);
s.insert(-100);
printf("%d\n",*(s.begin()));
s.insert(-200);
printf("%d\n",*(s.begin()));
s.insert(-150);
printf("%d\n",*(s.begin()));
set<int>::iterator i = s.find(-200);
if(i != s.end()) {
s.erase(i);
}
printf("%d\n",*(s.begin()));
}
pair + sort
#include <iostream>
#include <set>
using namespace std;
main()
{
set<pair<int,int>> s;
int n;
cin >> n;
for(int i=0; i<n; i++) {
int y;
cin >> y;
s.insert(make_pair(y,i));
}
for(auto i = s.begin(); i != s.end(); i++) {
cout << i->first << endl;
}
}
pair / erase
#include <set>
#include <cstdio>
using namespace std;
typedef pair<int,int> int_pair;
set<int_pair> s;
main()
{
s.insert(make_pair(0,0));
pair<set<int_pair>::iterator,bool> res = s.insert(int_pair(10,0));
s.insert(int_pair(15,10));
s.insert(int_pair(10,7));
s.erase(res.first);
for(set<int_pair>::iterator i = s.begin();
i != s.end(); i++) {
printf("%d, %d\n", i->first, i->second);
}
}
map
#include <map>
#include <cstdio>
using namespace std;
map<int,int> m;
main()
{
m[100] = 20;
m[1000000] = 30;
m[100]++;
printf("%d\n",m[100]);
printf("%d\n",m[1000000]);
if(m.find(200) != m.end()) {
m[200] += 10;
} else {
m[200] = 1;
}
m.insert(make_pair(1000,1000000));
for(map<int,int>::iterator i = m.begin();
i != m.end(); i++) {
printf("%d => %d\n",i->first, i->second);
}
}
lower_bound, upper_bound
ใช้ได้ทั้ง set และ map
#include <map>
#include <cstdio>
using namespace std;
map<int,int> m;
main()
{
m[100] = 20;
m[1000000] = 30;
m[100]++;
m.insert(make_pair(1000,1000000));
printf("%d\n",m.lower_bound(999)->first);
printf("%d\n",m.lower_bound(1000)->first);
printf("%d\n",m.upper_bound(999)->first);
printf("%d\n",m.upper_bound(1000)->first);
}