// Ex2_01.cpp
//2019年9月28日17:11:02
/*
Using array<T,N> to create a Body Mass Index (BMI) table
BMI = weight/(height*height)
weight is in kilograms, height is in meters
*/
#include <iostream> // For standard streams
#include <iomanip> // For stream manipulators
#include <array> // For array<T,N>
using namespace std;
int main()
{
const unsigned int min_wt {100U}; // Minimum weight in table in lbs
const unsigned int max_wt {250U}; // Maximum weight in table in lbs
const unsigned int wt_step {10U}; // Weight increment
const size_t wt_count {1 + (max_wt - min_wt) / wt_step};
const unsigned int min_ht {48U}; // Minimum height in table in inches
const unsigned int max_ht {84U}; // Maximum height in table in inches
const unsigned int ht_step {2U}; // Height increment
const size_t ht_count {1 + (max_ht - min_ht) / ht_step};
const double lbs_per_kg {2.20462}; // Conversion factor lbs to kg
const double ins_per_m {39.3701}; // Conversion factor ins to m
std::array<unsigned int, wt_count> weight_lbs;
std::array<unsigned int, ht_count> height_ins;
// Create weights from 100lbs in steps of 10lbs
for (size_t i {}, w {min_wt}; i < wt_count; w += wt_step, ++i)
{
weight_lbs.at(i) = w;
}
// Create heights from 48 inches in steps of 2 inches
unsigned int h {min_ht};
for (auto& height : height_ins)
{
height = h;
h += ht_step;
}
// Output table headings
std::cout << std::setw(7) << " |";
for (const auto& w : weight_lbs)
std::cout << std::setw(5) << w << " |";
std::cout << std::endl;
// Output line below headings
for (size_t i {1}; i < wt_count; ++i)
std::cout << "---------";
std::cout << std::endl;
double bmi {}; // Stores BMI
unsigned int feet {}; // Whole feet for output
unsigned int inches {}; // Whole inches for output
const unsigned int inches_per_foot {12U};
std::cout<< inches_per_foot<<std::endl;
for(const auto& h:height_ins)
{
feet = h/inches_per_foot;
inches = h%inches_per_foot;
cout<<setw(2)<<feet<<"\'"<<setw(2)<<inches<<"\""<<"|";
cout<<fixed<<setprecision(1);
for(const auto& w:weight_lbs)
{
bmi = h/ins_per_m;
bmi = (w/lbs_per_kg)/(bmi*bmi);
cout<<setw(2)<<" "<<bmi<<" ";
}
cout<<endl;
}
for (size_t i {1}; i < wt_count; ++i)
std::cout << "---------";
cout<<"BMI from 18.5 to 24.9 is normal"<<endl;
return 0;
}
//ex2_02
//sorting strings in a vector container
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
int main()
{
vector<string> words;
words.reserve(10);
cout<<"Enter words separated by spaces. Enter Ctrl + Z on a separate line to end";
copy(istream_iterator<string>{cin},istream_iterator<string>{},back_inserter(words));
cout<<"Starting sort."<<endl;
bool out_of_order{false};
auto last = end(words);
while(true)
{
for(auto first = begin(words)+1; first != last; ++first)
{
if(*(first-1)>*first)
{
swap(*first,*(first-1));
out_of_order = true;
}
}
if(!out_of_order)
break;
out_of_order = false;
}
//output the sorted array
cout<<"Your array in an ascending order"<<endl;
//copy()中,前两个参数,确定了复制元素的范围。第三个元素,指向这些元素的存放位置
copy(begin(words),end(words),ostream_iterator<string> {cout," "});
cout<<endl;
//creating a new vector by moving elements from words vector
vector<string> words_copy{make_move_iterator(begin(words)), make_move_iterator(end(words))};
cout<<"after moving elements from words, words_copy contains:"<<endl;
copy(begin(words_copy),end(words_copy),ostream_iterator<string> {cout," "});
cout<<endl;
// Now,we reflect upon the words
cout<<"words has "<<words.size()<<" elements"<<endl;
if(words.front().empty())
cout<<"First empty is empty string object"<<endl;
cout<<"First element is " << words.front()<<" "<<endl;
return 0;
}
// Ex2_03.cpp
// Understanding how capacity is increased in a vector container
#include <iostream> // For standard streams
#include <vector> // For vector container
int main()
{
std::vector <size_t> sizes; // Record numbers of elements
std::vector <size_t> capacities; // and corresponding capacities
size_t el_incr {10}; // Increment to initial element count
size_t incr_count {4 * el_incr}; // Number of increments to element count
for (size_t n_elements {}; n_elements < incr_count; n_elements += el_incr)
{
std::vector<int> values(n_elements);
std::cout << "\nAppending to a vector with " << n_elements << " initial elements:\n";
sizes.push_back(values.size());
size_t space {values.capacity()};
capacities.push_back(space);
// Append elements to obtain capacity increases
size_t count {}; // Counts capacity increases
size_t n_increases {10};
while (count < n_increases)
{
values.push_back(22); // Append a new element
if (space < values.capacity()) // Capacity increased...
{ // ...so record size and capacity
space = values.capacity();
capacities.push_back(space);
sizes.push_back(values.size());
++count;
}
}
// Show sizes & capacities when increments occur
std::cout << "Size/Capacity: ";
for (size_t i {}; i < sizes.size(); ++i)
std::cout << sizes.at(i) << "/" << capacities.at(i) << " ";
std::cout << std::endl;
sizes.clear(); // Remove all elements
capacities.clear(); // Remove all elements
}
}
//ex2_04
//using a deque container
//2019年9月28日21:47:07
#include <algorithm>
#include <iostream>
#include <deque>
#include <string>
#include <iterator>
using namespace std;
int main()
{
deque<string> names;
cout<<"Enter first names separated by spaces and enter Ctrl + z in a new line to end this."<<endl;
copy(istream_iterator<string>{cin},istream_iterator<string>{},front_inserter(names));
cout<<endl<<"In reverse order"<<endl;
copy(begin(names),end(names),ostream_iterator<string>{cout," "});
cout << endl;
return 0;
}
//ex2_05
//working with a list
#include <iostream>
#include <list>
#include <string>
#include <functional>
using namespace std;
template<typename Iter>
void list_elements(Iter begin, Iter end)
{
while(begin!=end)
cout<< *begin++ <<endl;
}
int main()
{
list<string>proverbs;
cout<<"Enter a few verbs and enter an empty line to end"<<endl;
string proverb;
while(getline(cin,proverb,'\n'), !proverb.empty())
proverbs.push_back(proverb);
cout<<"Go on, just one more line:"<<endl;
getline(cin,proverb,'\n');
proverbs.emplace_back(proverb);
cout<<"The elements in the list in reverse order are:"<<endl;
list_elements(rbegin(proverbs),rend(proverbs));
proverbs.sort();
cout<<"The ascending sequence proverbs are:"<<endl;
list_elements(begin(proverbs),end(proverbs));
proverbs.sort(greater<>());
cout<<"The descending sequence proverbs are:"<<endl;
list_elements(begin(proverbs),end(proverbs));
return 0;
}