添加STL
This commit is contained in:
parent
a46fb16beb
commit
df0a14d612
@ -87,4 +87,9 @@ add_executable(vect1 stl/vect1.cpp)
|
||||
add_executable(vect2 stl/vect2.cpp)
|
||||
add_executable(vect3 stl/vect3.cpp)
|
||||
add_executable(copyit stl/copyit.cpp)
|
||||
add_executable(inserts stl/inserts.cpp)
|
||||
add_executable(inserts stl/inserts.cpp)
|
||||
add_executable(list stl/list.cpp)
|
||||
add_executable(setops stl/setops.cpp)
|
||||
add_executable(multimap stl/multimap.cpp)
|
||||
add_executable(functor stl/functor.cpp)
|
||||
add_executable(funadap stl/funadap.cpp)
|
54
base/stl/funadap.cpp
Normal file
54
base/stl/funadap.cpp
Normal file
@ -0,0 +1,54 @@
|
||||
//
|
||||
// Created by nicemoe on 2021/9/21.
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <iterator>
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
|
||||
using namespace std;
|
||||
|
||||
void Show(double);
|
||||
|
||||
const int LIM = 6;
|
||||
|
||||
int main() {
|
||||
|
||||
double arr1[LIM]{
|
||||
28, 29, 30, 35, 38, 59
|
||||
};
|
||||
double arr2[LIM]{
|
||||
63, 65, 69, 75, 80, 99
|
||||
};
|
||||
vector<double> gr8(arr1, arr1 + LIM);
|
||||
vector<double> m8(arr2, arr2 + LIM);
|
||||
cout.setf(ios_base::fixed);
|
||||
//保留小数点以一位
|
||||
cout.precision(1);
|
||||
cout << "gr8:\t";
|
||||
for_each(gr8.begin(), gr8.end(), Show);
|
||||
cout << endl;
|
||||
cout << "m8: \t";
|
||||
for_each(m8.begin(), m8.end(), Show);
|
||||
cout << endl;
|
||||
|
||||
vector<double> sum(LIM);
|
||||
transform(gr8.begin(), gr8.end(), m8.begin(), sum.begin(), plus<double>());
|
||||
cout << "sum:\t";
|
||||
for_each(sum.begin(), sum.end(), Show);
|
||||
cout << endl;
|
||||
|
||||
vector<double> prod(LIM);
|
||||
transform(gr8.begin(), gr8.end(), prod.begin(), bind1st(multiplies<double>(), 2.5));
|
||||
cout << "prod:\t";
|
||||
for_each(prod.begin(), prod.end(), Show);
|
||||
cout << endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void Show(double v) {
|
||||
cout.width(6);
|
||||
cout << v << ' ';
|
||||
}
|
50
base/stl/functor.cpp
Normal file
50
base/stl/functor.cpp
Normal file
@ -0,0 +1,50 @@
|
||||
//
|
||||
// Created by nicemoe on 2021/9/21.
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
#include <iterator>
|
||||
#include <algorithm>
|
||||
|
||||
using namespace std;
|
||||
|
||||
template<class T>
|
||||
class TooBig {
|
||||
private:
|
||||
T cutoff;
|
||||
public:
|
||||
TooBig(const T &t) : cutoff(t) {}
|
||||
|
||||
bool operator()(const T &v) {
|
||||
return v > cutoff;
|
||||
}
|
||||
};
|
||||
|
||||
void outint(int n) {
|
||||
cout << n << " ";
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
TooBig<int> f100(100);
|
||||
int vals[10] {
|
||||
50,100,90,180,60,210,415,88,188,201
|
||||
};
|
||||
list<int> yadayada(vals,vals+10);
|
||||
list<int> etcetera(vals,vals+10);
|
||||
|
||||
cout<<"Original lists:\n";
|
||||
for_each(yadayada.begin(),yadayada.end(), outint);
|
||||
cout<<endl;
|
||||
for_each(etcetera.begin(),etcetera.end(), outint);
|
||||
cout<<endl;
|
||||
yadayada.remove_if(f100);
|
||||
etcetera.remove_if(TooBig<int>(200));
|
||||
cout<<"Trimmed lists:\n";
|
||||
for_each(yadayada.begin(),yadayada.end(), outint);
|
||||
cout<<endl;
|
||||
for_each(etcetera.begin(),etcetera.end(), outint);
|
||||
cout<<endl;
|
||||
return 0;
|
||||
}
|
56
base/stl/list.cpp
Normal file
56
base/stl/list.cpp
Normal file
@ -0,0 +1,56 @@
|
||||
//
|
||||
// Created by nicemoe on 2021/9/21.
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
#include <iterator>
|
||||
#include <algorithm>
|
||||
|
||||
using namespace std;
|
||||
|
||||
void outint(int n) {
|
||||
cout << n << " ";
|
||||
}
|
||||
|
||||
int main() {
|
||||
list<int> one(5, 2);
|
||||
int stuff[5] = {
|
||||
1, 2, 4, 8, 6
|
||||
};
|
||||
list<int> two;
|
||||
two.insert(two.begin(), stuff, stuff + 5);
|
||||
int more[6]{
|
||||
6, 4, 2, 4, 6, 5
|
||||
};
|
||||
list<int> three(two);
|
||||
three.insert(three.end(), more, more + 6);
|
||||
|
||||
cout << "List one: ";
|
||||
for_each(one.begin(), one.end(), outint);
|
||||
cout << endl << "List two: ";
|
||||
for_each(two.begin(), two.end(), outint);
|
||||
cout << endl << "List three: ";
|
||||
for_each(three.begin(), three.end(), outint);
|
||||
three.remove(2);
|
||||
cout << endl << "List three minus 2s: ";
|
||||
for_each(three.begin(), three.end(), outint);
|
||||
three.splice(three.begin(), one);
|
||||
cout << endl << "List three after splice: ";
|
||||
for_each(three.begin(), three.end(), outint);
|
||||
cout << endl << "List one: ";
|
||||
for_each(one.begin(), one.end(), outint);
|
||||
three.unique();
|
||||
cout << endl << "List three after unique: ";
|
||||
for_each(three.begin(), three.end(), outint);
|
||||
three.sort();
|
||||
three.unique();
|
||||
cout << endl << "List three after sort & unique: ";
|
||||
for_each(three.begin(), three.end(), outint);
|
||||
two.sort();
|
||||
three.merge(two);
|
||||
cout << endl << "Sorted two merged into three: ";
|
||||
for_each(three.begin(), three.end(), outint);
|
||||
cout << endl;
|
||||
return 0;
|
||||
}
|
42
base/stl/multimap.cpp
Normal file
42
base/stl/multimap.cpp
Normal file
@ -0,0 +1,42 @@
|
||||
//
|
||||
// Created by nicemoe on 2021/9/21.
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
|
||||
using namespace std;
|
||||
|
||||
typedef int KeyType;
|
||||
typedef pair<const KeyType, string> Pair;
|
||||
typedef multimap<KeyType, string> MapCode;
|
||||
|
||||
int main() {
|
||||
MapCode codes;
|
||||
|
||||
codes.insert(Pair(415, "San Francisco"));
|
||||
codes.insert(Pair(510, "Oakland"));
|
||||
codes.insert(Pair(718, "Brooklyn"));
|
||||
codes.insert(Pair(718, "Staten Island"));
|
||||
codes.insert(Pair(415, "San Rafael"));
|
||||
codes.insert(Pair(510, "Berkeley"));
|
||||
|
||||
cout << "Number of cities with area code 415: " << codes.count(415) << endl;
|
||||
cout << "Number of cities with area code 718: " << codes.count(718) << endl;
|
||||
cout << "Number of cities with area code 510: " << codes.count(510) << endl;
|
||||
cout << "Area Code City" << endl;
|
||||
|
||||
MapCode::iterator it;
|
||||
for (it = codes.begin(); it != codes.end(); ++it) {
|
||||
cout << " " << (*it).first << " " << it->second << endl;
|
||||
}
|
||||
pair<MapCode::iterator, MapCode::iterator> range = codes.equal_range(718);
|
||||
cout << "Cities with area code 718:\n";
|
||||
for (it = range.first; it != range.second; ++it) {
|
||||
cout << it->second << endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
58
base/stl/setops.cpp
Normal file
58
base/stl/setops.cpp
Normal file
@ -0,0 +1,58 @@
|
||||
//
|
||||
// Created by nicemoe on 2021/9/21.
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
#include<string>
|
||||
#include <set>
|
||||
#include <algorithm>
|
||||
#include <iterator>
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
const int N =6;
|
||||
string s1[N]{
|
||||
"buffon","thinkers","for","heavy","can","for"
|
||||
};
|
||||
string s2[N]{
|
||||
"metal","any","food","elegant","deliver","for"
|
||||
};
|
||||
set<string> A(s1,s1+N);
|
||||
set<string> B(s2,s2+N);
|
||||
|
||||
ostream_iterator<string,char> out(cout," ");
|
||||
cout<<"Set A: ";
|
||||
copy(A.begin(),A.end(),out);
|
||||
cout<<endl;
|
||||
cout<<"Set B: ";
|
||||
copy(B.begin(),B.end(),out);
|
||||
cout<<endl;
|
||||
//并集
|
||||
cout<<"Union of A and B:\n";
|
||||
set_union(A.begin(),A.end(),B.begin(),B.end(),out);
|
||||
cout<<endl;
|
||||
//交集
|
||||
cout<<"Intersection of A and B:\n";
|
||||
set_intersection(A.begin(),A.end(),B.begin(),B.end(),out);
|
||||
cout<< endl;
|
||||
//差集
|
||||
cout<<"Difference of A and B:\n";
|
||||
set_difference(A.begin(),A.end(),B.begin(),B.end(),out);
|
||||
cout<< endl;
|
||||
set<string> C;
|
||||
cout<<"Set C: "<<endl;
|
||||
set_union(A.begin(),A.end(),B.begin(),B.end(),insert_iterator<set<string> >(C,C.begin()));
|
||||
copy(C.begin(),C.end(),out);
|
||||
cout<< endl;
|
||||
|
||||
string s3("grungy");
|
||||
C.insert(s3);
|
||||
cout<<"Set C after insertion:\n";
|
||||
copy(C.begin(),C.end(),out);
|
||||
cout<<endl;
|
||||
|
||||
cout<<"Showing a range: "<<endl;
|
||||
copy(C.lower_bound("ghost"),C.upper_bound("spook"),out);
|
||||
cout<<endl;
|
||||
return 0;
|
||||
}
|
15
base/stl/strgst1.cpp
Normal file
15
base/stl/strgst1.cpp
Normal file
@ -0,0 +1,15 @@
|
||||
//
|
||||
// Created by nicemoe on 2021/9/21.
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user