添加stl

This commit is contained in:
zhuyijun 2021-09-20 23:39:19 +08:00
parent d776d8839a
commit b648bb02e9
6 changed files with 243 additions and 2 deletions

View File

@ -59,7 +59,7 @@ add_executable(studentc reusing/studentc.cpp)
add_executable(uses_stuc reusing/uses_stuc.cpp)
add_executable(stacktp stl/stacktp.cpp)
add_executable(stacktp1 stl/stacktp1.cpp)
add_executable(arraytp stl/arraytp.cpp)
add_executable(arraytp stl/arraytp.cpp stl/inserts.cpp)
add_executable(tv friend/tv.cpp friend/use_tv.cpp friend/tvfm.h)
add_executable(queuetp friend/queuetp.cpp)
add_executable(error1 exception/error1.cpp)
@ -81,4 +81,9 @@ add_executable(rtti2 exception/rtti2.cpp)
add_executable(constcast exception/constcast.cpp)
add_executable(str1 string/str1.cpp)
add_executable(strfile string/strfile.cpp)
add_executable(hangman string/hangman.cpp)
add_executable(hangman string/hangman.cpp)
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)

31
base/stl/copyit.cpp Normal file
View File

@ -0,0 +1,31 @@
//
// Created by zhuyi on 2021/9/20.
//
#include <iostream>
#include <iterator>
#include <vector>
int main() {
using namespace std;
int casts[10]{
6, 7, 2, 9, 4,
11, 8, 7, 10, 5
};
vector<int> dice(10);
copy(casts, casts + 10, dice.begin());
cout << "Let the dice be cast!\n";
ostream_iterator<int, char> out_iter(cout, " ");
copy(dice.begin(), dice.end(), out_iter);
cout << endl;
cout << "Implicit use of reverse iterator.\n";
copy(dice.rbegin(), dice.rend(), out_iter);
cout << endl;
cout << "Explict use of reverse iterator.\n";
vector<int>::reverse_iterator ri;
for (ri = dice.rbegin(); ri != dice.rend(); ++ri) {
cout << *ri << ' ';
}
cout << endl;
return 0;
}

39
base/stl/inserts.cpp Normal file
View File

@ -0,0 +1,39 @@
//
// Created by zhuyi on 2021/9/20.
//
#include <iostream>
#include <string>
#include <iterator>
#include <vector>
#include <algorithm>
using namespace std;
void output(const string &s) {
cout << s << " ";
}
int main() {
string s1[4] = {
"fine", "fish", "fashion", "fate"
};
string s2[2] = {
"busy", "bats"
};
string s3[2] = {
"silly", "singers"
};
vector<string> words(4);
copy(s1, s1 + 4, words.begin());
for_each(words.begin(), words.end(), output);
cout << endl;
copy(s2, s2 + 2, back_insert_iterator<vector<string> >(words));
for_each(words.begin(), words.end(), output);
cout << endl;
copy(s3, s3 + 2, insert_iterator<vector<string> >(words, words.begin()));
for_each(words.begin(), words.end(), output);
cout << endl;
return 0;
}

29
base/stl/vect1.cpp Normal file
View File

@ -0,0 +1,29 @@
//
// Created by zhuyi on 2021/9/19.
//
#include <iostream>
#include <string>
#include <vector>
using namespace std;
const int NUM = 5;
int main(){
vector<int> ratings(NUM);
vector<string> titles(NUM);
cout <<"You will do exactly as told.You will enter\n"
<< NUM <<" book titles and your ratings (0-10).\n";
int i;
for (i = 0; i < NUM; i++) {
cout <<"Enter title #" << i+1 << " : ";
getline(cin,titles[i]);
cout <<"Enter your rating(0-10): ";
cin >>ratings[i];
cin.get();
}
cout<<"Thank you. You entered the following:\n"<<"Rating\tBoot\n";
for (int j = 0; j < NUM; ++j) {
cout << ratings[j] <<"\t" <<titles[j] << endl;
}
return 0;
}

62
base/stl/vect2.cpp Normal file
View File

@ -0,0 +1,62 @@
//
// Created by zhuyi on 2021/9/19.
//
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct Review{
string title;
int rating;
};
bool FillReview(Review & rr);
void ShowReview(const Review & rr);
int main(){
vector<Review> books;
Review temp;
while (FillReview(temp)){
books.push_back(temp);
}
int num = books.size();
if (num > 0){
cout << "Thank you .You entered the following:\n"<<"Rating\tBook\n";
for (auto pr = books.begin(); pr !=books.end() ; pr++) {
ShowReview(*pr);
}
vector<Review> oldlist(books);
if (num > 3){
books.erase(books.begin() +1,books.begin() + 3);
cout <<"After erasure:\n";
for (auto pr = books.begin();pr != books.end();pr++) {
ShowReview(*pr);
}
}
books.swap(oldlist);
cout <<"Swapping oldlist with books:\n";
for (auto pr = books.begin();pr != books.end();pr++) {
ShowReview(*pr);
}
} else{
cout <<"Nothing entered, nothing gained.\n";
}
return 0;
}
bool FillReview(Review & rr){
cout <<"Enter book title (quit to quit): ";
getline(cin,rr.title);
if (rr.title == "quit") return false;
cout <<"Enter book rating: ";
cin >> rr.rating;
if (!cin){
return false;
}
while (cin.get() != '\n'){
continue;
}
return true;
}
void ShowReview(const Review & rr){
cout << rr.rating <<"\t"<<rr.title << endl;
}

75
base/stl/vect3.cpp Normal file
View File

@ -0,0 +1,75 @@
//
// Created by zhuyi on 2021/9/19.
//
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct Review{
string title;
int rating;
};
bool operator<(const Review & r1,const Review & r2);
bool worseThan(const Review & r1,const Review & r2);
bool FillReview(Review & rr);
void ShowReview(const Review & rr);
int main(){
vector<Review> books;
Review temp;
while (FillReview(temp)){
books.push_back(temp);
}
if (books.size() > 0){
cout << "Thank you .You entered the following: "<< books.size()<<"raings:\n"<<"Rating\tBook\n";
for_each(books.begin(),books.end(), ShowReview);
sort(books.begin(),books.end());
cout <<"Sorted by title:\n"<<"Rating\tBook\n";
for_each(books.begin(),books.end(), ShowReview);
sort(books.begin(),books.end(), worseThan);
cout <<"Sorted by rating:\n"<<"Rating\tBook\n";
for_each(books.begin(),books.end(), ShowReview);
random_shuffle(books.begin(),books.end());
cout <<"After shuffling:\n"<<"Rating\tBook\n";
for_each(books.begin(),books.end(), ShowReview);
} else{
cout <<"No entries.";
}
cout <<"Bye.\n";
return 0;
}
bool operator<(const Review & r1,const Review & r2){
if (r1.title < r2.title){
return false;
} else if (r1.title == r2.title && r1.rating < r2.rating){
return true;
}
return false;
}
bool worseThan(const Review & r1,const Review & r2){
if (r1.rating < r2.rating){
return true;
}
return false;
}
bool FillReview(Review & rr){
cout <<"Enter book title (quit to quit): ";
getline(cin,rr.title);
if (rr.title == "quit") return false;
cout <<"Enter book rating: ";
cin >> rr.rating;
if (!cin){
return false;
}
while (cin.get() != '\n'){
continue;
}
return true;
}
void ShowReview(const Review & rr){
cout << rr.rating <<"\t"<<rr.title << endl;
}