This commit is contained in:
朱毅骏 2021-09-22 15:40:16 +08:00
parent 503686d9ef
commit 532b93dc95
7 changed files with 223 additions and 18 deletions

View File

@ -92,4 +92,29 @@ 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)
add_executable(funadap stl/funadap.cpp)
add_executable(strgst1 stl/strgst1.cpp)
add_executable(listrmv stl/listrmv.cpp)
add_executable(usealgo stl/usealgo.cpp)
add_executable(valvect stl/valvect.cpp)
add_executable(vslice stl/vslice.cpp)
add_executable(ilist stl/ilist.cpp)
add_executable(write io/write.cpp)
add_executable(defaults io/defaults.cpp)
add_executable(manip io/manip.cpp)
add_executable(width io/width.cpp)
add_executable(fill io/fill.cpp)
add_executable(precise io/precise.cpp)
add_executable(showpt io/showpt.cpp)
add_executable(setf io/setf.cpp)
add_executable(iomanip io/iomanip.cpp)
add_executable(check_it io/check_it.cpp)
add_executable(cinexcp io/cinexcp.cpp)
add_executable(get_gun io/get_gun.cpp)
add_executable(peeker io/peeker.cpp)
add_executable(truncate io/truncate.cpp)
add_executable(filleio io/filleio.cpp)
add_executable(count io/count.cpp)
add_executable(append io/append.cpp)
add_executable(strout io/strout.cpp)
add_executable(strin io/strin.cpp)

View File

@ -3,12 +3,41 @@
//
#include <iostream>
#include "ilist.h"
#include <initializer_list>
using namespace std;
int main(){
inline double sum(initializer_list<double> il);
inline double average(const initializer_list<double> &ril);
int main() {
cout << "List 1: sum = " << sum({2, 3, 4}) << ", ave = " << average({2, 3, 4}) << endl;
initializer_list<double> dl = {1.1, 2.2, 3.3, 4.4, 5.5};
cout << "List 2: sum = " << sum(dl) << ", ave = " << average(dl) << endl;
dl = {16.0, 25.0, 36.0, 40.0, 64.0};
cout << "List 3: sum = " << sum(dl) << ", ave = " << average(dl) << endl;
return 0;
}
inline double sum(initializer_list<double> il) {
double tot = 0;
for (auto p = il.begin(); p != il.end(); p++) {
tot += *p;
}
return tot;
}
inline double average(const initializer_list<double> &ril) {
double tot = 0;
int n = ril.size();
double ave = 0.0;
if (n > 0) {
for (auto p = ril.begin(); p != ril.end(); p++) {
tot += *p;
}
ave = tot / n;
}
return ave;
}

View File

@ -3,12 +3,43 @@
//
#include <iostream>
#include "listrmv.h"
#include <list>
#include <algorithm>
using namespace std;
int main(){
void Show(int);
const int LIM = 10;
int main() {
int ar[LIM]{
4, 5, 4, 2, 2, 3, 4, 8, 1, 4
};
list<int> la(ar, ar + LIM);
list<int> lb(la);
cout << "Original list contents:\n\t";
for_each(la.begin(), la.end(), Show);
cout << endl;
la.remove(4);
cout << "After using the remove() methond:\n";
cout << "la:\t";
for_each(la.begin(), la.end(), Show);
cout << endl;
auto last = remove(lb.begin(), lb.end(), 4);
cout << "After using the remove() function:\n";
cout << "lb:\t";
for_each(lb.begin(), lb.end(), Show);
cout << endl;
lb.erase(last, lb.end());
cout << "After using the earse() methond:\n";
cout << "lb:\t";
for_each(lb.begin(), lb.end(), Show);
cout << endl;
return 0;
}
void Show(int v) {
cout << v << ' ';
}

View File

@ -10,6 +10,16 @@ using namespace std;
int main() {
string letters;
cout << "Enter the letter grouping (quit to quit): ";
while (cin >> letters && letters != "quit") {
cout << "Permutations of " << letters << endl;
sort(letters.begin(), letters.end());
cout << letters << endl;
while (next_permutation(letters.begin(), letters.end()))
cout << letters << endl;
cout << "Enter next sequence (quit to quit): ";
}
cout << "Done.\n";
return 0;
}

View File

@ -3,12 +3,60 @@
//
#include <iostream>
#include "usealgo.h"
#include <string>
#include <vector>
#include <set>
#include <map>
#include <iterator>
#include <algorithm>
#include <cctype>
using namespace std;
int main(){
char toLower(char ch) {
return tolower(ch);
}
string &ToLower(string &st);
void display(const string &s);
int main() {
vector<string> words;
cout << "Enter words(enter quit to quit) :\n";
string input;
while (cin >> input && input != "quit")
words.push_back(input);
cout << "You entered the following words:\n";
for_each(words.begin(), words.end(), display);
cout << endl;
set<string> wordset;
transform(words.begin(), words.end(), insert_iterator<set<string> >(wordset, wordset.begin()), ToLower);
cout << "\nAlphabetic list of words:\n";
for_each(wordset.begin(), wordset.end(), display);
cout << endl;
map<string, int> wordmap;
set<string>::iterator si;
for (si = wordset.begin(); si != wordset.end(); si++) {
wordmap[*si] = count(words.begin(), words.end(), *si);
}
cout << "\nWord frequency:\n";
for (si = wordset.begin(); si != wordset.end(); si++) {
cout << *si << " : " << wordmap[*si] << endl;
}
return 0;
}
string &ToLower(string &st) {
transform(st.begin(), st.end(), st.begin(), toLower);
return st;
}
void display(const string &s) {
cout << s << " ";
}

View File

@ -3,12 +3,37 @@
//
#include <iostream>
#include "valvect.h"
#include <valarray>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
int main() {
vector<double> data;
double temp;
cout<<"Enter numbers (<=0 to quit):\n";
while (cin>>temp && temp >0)
data.push_back(temp);
sort(data.begin(), data.end());
int size =data.size();
valarray<double> numbers(size);
for (int i = 0; i < size; ++i) {
numbers[i] = data[i];
}
valarray<double> sq_rts(size);
sq_rts = sqrt(numbers);
valarray<double> result(size);
result = numbers + 2.0 * sq_rts;
cout.setf(ios_base::fixed);
cout.precision(4);
for (int i = 0; i < size; ++i) {
cout.width(8);
cout<<numbers[i]<<": ";
cout.width(8);
cout<<result[i]<<endl;
}
cout << "done\n";
return 0;
}

View File

@ -3,12 +3,49 @@
//
#include <iostream>
#include "vslice.h"
#include <valarray>
#include <cstdlib>
using namespace std;
int main(){
const int SIZE = 12;
typedef valarray<int> vint;
void show(const vint &v, int cols);
int main() {
vint valint(SIZE);
for (int i = 0; i < SIZE; ++i) {
valint[i] = rand() % 10;
}
cout << "Original array:\n";
show(valint, 3);
vint vcol(valint[slice(1, 4, 3)]);
cout << "Second column:\n";
show(vcol, 1);
vint vrow(valint[slice(3, 3, 1)]);
cout << "Second row:\n";
show(vrow, 3);
valint[slice(2, 4, 3)] = 10;
cout << "Set last column to 10:\n";
show(valint, 3);
cout << "Set first column to sum of next two:\n";
valint[slice(0, 4, 3)] = vint(valint[slice(1, 4, 3)]) + vint(valint[slice(2, 4, 3)]);
show(valint, 3);
return 0;
}
void show(const vint &v, int cols) {
int lim = v.size();
for (int i = 0; i < lim; ++i) {
cout.width(3);
cout << v[i];
if (i % cols == cols - 1)
cout << endl;
else
cout << ' ';
}
if (lim % cols != 0)
cout << endl;
}