c++primer学習ノート-第9章
19126 ワード
練習問題9.4:
練習問題9.5:
練習問題9.13:
練習問題9.14:
練習問題9.15:
練習問題9.16:
練習問題9.18:
練習問題9.19:
練習問題9.22:
練習問題9.24:
練習問題9.26:
#include
#include
using std::cin; using std::cout; using std::endl;
using std::vector;
using Iter = vector::const_iterator;
bool find(Iter beg, Iter end, int num)
{
while (beg != end)
{
if (*beg == num)
return true;
++beg;
}
return false;
}
int main()
{
vector ivec{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
cout << std::boolalpha << find(ivec.cbegin(), ivec.cend(), 22) << endl;
getchar();
getchar();
return 0;
}
練習問題9.5:
#include
#include
#include
using std::cin; using std::cout; using std::endl;
using std::vector;
using std::runtime_error;
using Iter = vector::const_iterator;
Iter find(Iter beg, Iter end, int num)
{
try{
for (auto iter = beg; iter != end; ++iter)
if (*iter == num)
return iter;
throw runtime_error("Cannot find the input number and iterator is out of range.");
}
catch(runtime_error err){
cout << err.what()< ivec{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
cout << *find(ivec.cbegin(), ivec.cend(), 22) << endl;
getchar();
getchar();
return 0;
}
練習問題9.13:
#include
#include
#include
using std::cin; using std::cout; using std::endl;
using std::vector; using std::list;
int main()
{
list lvec{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
vector ivec{ 11, 22, 33, 44, 55, 66, 77, 88, 99 };
vector dvec(lvec.cbegin(), lvec.cend());
//vector dvec(ivec.cbegin(), ivec.cend());
for (auto d : dvec)
cout << d << " ";
cout << endl;
getchar();
getchar();
return 0;
}
練習問題9.14:
#include
#include
#include
#include
using std::cin; using std::cout; using std::endl;
using std::string; using std::vector; using std::list;
int main()
{
list clst{ "aaa", "bbb", "ccc" };
vector svec;
svec.assign(clst.cbegin(), clst.cend());
for (const auto s : svec)// const ,
cout << s << " ";
cout << endl;
getchar();
getchar();
return 0;
}
練習問題9.15:
#include
#include
#include
#include
using std::cin; using std::cout; using std::endl;
using std::string; using std::vector; using std::list;
bool compare_vec_int(const vector &ivec1, const vector &ivec2)
{
if (ivec1.size() == ivec2.size())
{
for (auto i = 0; i != ivec1.size(); ++i)
if (ivec1[i] != ivec2[i])
return false;
return true;
}
else
return false;
}
int main()
{
vector ivec1{ 1, 2, 3, 4, 5 }, ivec2{ 1, 2, 3, 4, 5 };
cout << std::boolalpha << compare_vec_int(ivec1, ivec2) << endl;
cout << (ivec1 == ivec2 ? "true" : "false") << endl;// <
練習問題9.16:
#include
#include
#include
#include
using std::cin; using std::cout; using std::endl;
using std::string; using std::vector; using std::list;
int main()
{
const vector ivec{ 1, 2, 3, 4, 5};
const list ilst{ 1, 2, 3, 4, 5 };
cout << (vector(ilst.cbegin(), ilst.cend()) == ivec ?
"true" : "false") << endl;
getchar();
getchar();
return 0;
}
練習問題9.18:
#include
#include
#include
#include
#include
using std::cin; using std::cout; using std::endl;
using std::string; using std::vector; using std::list; using std::deque;
int main()
{
deque sdeq;
for (string seq; cin >> seq; sdeq.emplace_front(seq));
for (auto iter = sdeq.cbegin(); iter != sdeq.cend(); ++iter)
cout << *iter << " ";
cout << endl;
getchar();
getchar();
return 0;
}
練習問題9.19:
#include
#include
#include
#include
#include
using std::cin; using std::cout; using std::endl;
using std::string; using std::vector; using std::list; using std::deque;
int main()
{
list sdeq;
for (string seq; cin >> seq; sdeq.emplace_back(seq));
for (auto iter = sdeq.cbegin(); iter != sdeq.cend(); ++iter)
cout << *iter << " ";
cout << endl;
getchar();
getchar();
return 0;
}
練習問題9.22:
#include
#include
#include
#include
#include
using std::cin; using std::cout; using std::endl;
using std::string; using std::vector; using std::list; using std::deque;
int main()
{
vector iv{ 1, 2, 3, 4, 5 };
vector::iterator mid = iv.begin() + iv.size() / 2;
for (auto iter = iv.begin(); iter != mid; ++iter)
if (*iter == 2)
{
iter = iv.insert(iter, 2 * 4);
mid = iter + iv.size() / 2;
++iter;
}
for (auto i : iv)
cout << i << " ";
cout << endl;
getchar();
getchar();
return 0;
}
練習問題9.24:
#include
#include
#include
#include
#include
#include
using std::cin; using std::cout; using std::endl;
using std::string; using std::vector; using std::list; using std::deque;
using std::out_of_range;
int main()
{
vector ivec;
try{
cout << ivec.at(0);
}
catch (out_of_range err){
cout << err.what() << endl;
}
getchar();
getchar();
return 0;
}
練習問題9.26:
#include
#include
#include
#include
#include
#include
#include
using std::cin; using std::cout; using std::endl;
using std::string; using std::vector; using std::list; using std::deque;
using std::begin; using std::end;#include
#include
#include
#include
#include
#include
#include
#include
using std::cin; using std::cout; using std::endl;
using std::string; using std::vector; using std::list;
using std::deque; using std::forward_list;
using std::begin; using std::end;
int main()
{
forward_list iflst = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 55, 89 };
auto prev = iflst.before_begin();
auto curr = iflst.begin();
while (curr != iflst.end())
{
if (*curr & 0x1)
{
curr = iflst.erase_after(prev);
}
else
{
prev = curr;
++curr;
}
}
for (auto i : iflst)
cout << i << " ";
cout << endl;
getchar();
getchar();
return 0;
}
int main(){int a[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 55, 89 };vector ivec(begin(a), end(a));list ilst(begin(a), end(a));for (auto iter = ivec.begin(); iter != ivec.end();)if (!(*iter & 0x1))iter = ivec.erase(iter);else++iter;for (auto i : ivec)cout << i << "";cout << endl;for (auto iter = ilst.begin(); iter != ilst.end();)if (*iter & 0x1)iter = ilst.erase(iter);else++iter;for (auto i : ilst)cout << i << "";cout << endl;getchar();getchar();return 0;} 9.27:
#include
#include
#include
#include
#include
#include
#include
#include
using std::cin; using std::cout; using std::endl;
using std::string; using std::vector; using std::list;
using std::deque; using std::forward_list;
using std::begin; using std::end;
void insert_string(forward_list &sflst, const string &s1, const string &s2)
{
auto prev = sflst.before_begin();
auto curr = sflst.begin();
bool flag = true;
while (curr != sflst.end())
if (*curr == s1)
{
flag = false;
prev = sflst.insert_after(curr, s2);
curr = ++prev;
}
else
{
++curr;
++prev;
}
if (flag)
{
sflst.insert_after(prev, s2);
}
}
int main()
{
forward_list sflst{ "a", "b", "c", "d", "b", "e" };
string s1("b"), s2(" ");
insert_string(sflst, s1, s2);
for (auto s : sflst)
cout << s << " ";
cout << endl;
getchar();
getchar();
return 0;
}
9.31:#include
#include
#include
#include
#include
#include
#include
#include
using std::cin; using std::cout; using std::endl;
using std::string; using std::vector; using std::list;
using std::deque; using std::forward_list;
using std::begin; using std::end;
int main()
{
list ilst{ 1, 2, 3, 4, 5, 6, 7, 8, 9 };
auto iter = ilst.begin();
while (iter != ilst.end())
if (*iter & 0x1){
iter = ilst.insert(iter, *iter);
++(++iter);
}
else
{
iter=ilst.erase(iter);
}
for (auto i : ilst)
cout << i << " ";
cout << endl;
getchar();
getchar();
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
using std::cin; using std::cout; using std::endl;
using std::string; using std::vector; using std::list;
using std::deque; using std::forward_list;
using std::begin; using std::end;
int main()
{
forward_list iflst{ 1, 2, 3, 4, 5, 6, 7, 8, 9 };
auto prev = iflst.before_begin();
for (auto curr = iflst.begin(); curr != iflst.end();)
{
if (*curr & 0x1)
{
curr = ++(iflst.insert_after(curr, *curr));
++(++prev);
}
else
curr = iflst.erase_after(prev);
}
for (auto i : iflst)
cout << i << " ";
cout << endl;
cout << endl;
getchar();
getchar();
return 0;
}
9.38:#include
#include
#include
#include
#include
#include
#include
#include
using std::cin; using std::cout; using std::endl;
using std::string; using std::vector; using std::list;
using std::deque; using std::forward_list;
using std::begin; using std::end;
int main()
{
vector ivec;
for (auto i = 0; i < 100; ++i)
{
cout << i << ": siez-" << ivec.size() << " capacity-" << ivec.capacity() << endl;
ivec.push_back(0);
}
getchar();
getchar();
return 0;
}
9.41:#include
#include
#include
#include
#include
#include
#include
#include
using std::cin; using std::cout; using std::endl;
using std::string; using std::vector; using std::list;
using std::deque; using std::forward_list;
using std::begin; using std::end;
int main()
{
vector cvec{ 'a', 'b', 'c' };
string str(cvec.begin(),cvec.end());
cout << str << endl;
getchar();
getchar();
return 0;
}
9.43:#include
#include
#include
#include
#include
#include
#include
#include
using std::cin; using std::cout; using std::endl;
using std::string; using std::vector; using std::list;
using std::deque; using std::forward_list;
using std::begin; using std::end;
void replace_s(string &s, const string &oldVal, const string &newVal)
{
for (auto iter = s.begin(); s.end() - iter >= oldVal.size();)
{
if (oldVal == string(iter, iter + oldVal.size()))
{ // out of range( git for ), string
iter=s.erase(iter, iter + oldVal.size());
iter=s.insert(iter, newVal.begin(), newVal.end());
iter += newVal.size();
}
else
++iter;// ++iter else ,
} // oldVal
}
int main()
{
string s("Hello,everybabybaby!");
replace_s(s, "baby", "body");
cout << s << endl;
getchar();
getchar();
return 0;
}
9.44:#include
#include
#include
#include
#include
#include
#include
#include
using std::cin; using std::cout; using std::endl;
using std::string; using std::vector; using std::list;
using std::deque; using std::forward_list;
using std::begin; using std::end;
void replace_s(string &s, const string &oldVal, const string &newVal)
{
for (auto i = 0; i <= s.size() - oldVal.size();)// , i
{
if (string(s, i, oldVal.size()) == oldVal)//github substr,
{
s.replace(i, oldVal.size(), newVal);
i += oldVal.size();
}
else
++i;
}
}
int main()
{
string s("Hello,everybabybaby!");
replace_s(s, "baby", "body");
cout << s << endl;
getchar();
getchar();
return 0;
}
9.45:#include
#include
#include
#include
#include
#include
#include
#include
using std::cin; using std::cout; using std::endl;
using std::string; using std::vector; using std::list;
using std::deque; using std::forward_list;
using std::begin; using std::end;
string &pre_post(string &name, const string &pre, const string &post)
{
name.insert(name.begin(), pre.begin(), pre.end());// , pre , p305
name.append(post.begin(), post.end());
return name;
}
int main()
{
string name = "Ge";
cout << pre_post(name, "Mr.", "III") << endl;
getchar();
getchar();
return 0;
}
9.46:#include
#include
#include
#include
#include
#include
#include
#include
using std::cin; using std::cout; using std::endl;
using std::string; using std::vector; using std::list;
using std::deque; using std::forward_list;
using std::begin; using std::end;
string pre_post(const string &name, const string &pre, const string &post)
{
string full(name);
full.insert(0,pre);
return full;
}
int main()
{
cout << pre_post("Ge", "Mr.", "III") << endl;
getchar();
getchar();
return 0;
}
9.47:#include
#include
#include
#include
#include
#include
#include
#include
using std::cin; using std::cout; using std::endl;
using std::string; using std::vector; using std::list;
using std::deque; using std::forward_list;
using std::begin; using std::end;
int main()
{
string number("0123456789"),
alphabet("qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM");
string name("ab2c3d7R4E6");
string::size_type pos=0;
while ((pos = name.find_first_not_of(alphabet, pos)) != string::npos)
{
cout << "find number at index: " << pos
<< " element is " << name[pos] << endl;
++pos;
}
cout << endl;
pos = 0;
while ((pos = name.find_first_not_of(number, pos)) != string::npos)
{
cout << "find char at index: " << pos
<< " element is " << name[pos] << endl;
++pos;
}
getchar();
getchar();
return 0;
}
9.49:#include
#include
#include
#include
#include
#include
#include
#include
#include
using std::cin; using std::cout; using std::endl;
using std::istream; using std::ifstream;
using std::string; using std::vector; using std::list;
using std::deque; using std::forward_list;
using std::begin; using std::end;
bool is_not_cender(string &word)
{
if (word.find_first_not_of("aceimnorsuvwxz") == string::npos)
return true;
else
return false;
}
bool longest(istream &is, string &long_word)
{
bool exist = false;
for (string word; is >> word;)
if (is_not_cender(word))
if (long_word.empty())
{
long_word = word;
exist = true;
}
else
if (word.size() > long_word.size())
long_word = word;
return exist;
}
int main()
{
ifstream infile("words.txt");
string long_word;
if (longest(infile, long_word))
cout << long_word << ": " << long_word.size() << endl;
else
cout << "all cenders" << endl;
getchar();
getchar();
return 0;
}
9.50:#include
#include
#include
#include
#include
#include
#include
#include
using std::cin; using std::cout; using std::endl;
using std::string; using std::vector; using std::list;
using std::deque; using std::forward_list;
using std::begin; using std::end;
int main()
{
vector svec1{ "1", "2", "3", "4", "5" };
int sum1 = 0;
for (auto s : svec1)
sum1 += stoi(s);
cout << sum1 << endl;
vector svec2{ "1.1", "2.2", "0.1e1", "4.6", "-5" };
double sum2 = 0;
for (auto s : svec2)
sum2 += stod(s);
cout << sum2 << endl;
getchar();
getchar();
return 0;
}