C++Primer 第10章 bind与迭代器

//10.20,10.22
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<functional>
using namespace std;
using namespace std::placeholders;bool sumcount(const string &str, const string::size_type &size)
{return str.size() == size;
}int main()
{vector<string> str{ "wo","shi","ni","ba","ba!" };const string::size_type &sz = 2;cout << count_if(str.begin(), str.end(), bind(sumcount, _1, sz)) << endl;cout << count_if(str.begin(), str.end(), [&](const string &str) {return str.size() == sz; }) << endl;
}//10.24
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<functional>
using namespace ::std;
using namespace ::std::placeholders;
bool strsize(string::size_type size,const string &str)
{return size == str.size();
}
int main()
{string str{ "abcd" };vector<int> ival{ 1,2,3,0xffff,4,5,6,7,8,9,010 };auto p = find_if(ival.begin(), ival.end(), bind(strsize, _1, str));cout << *p << endl;auto q = find_if(ival.begin(), ival.end(), [str](string::size_type size) {return size == str.size(); });cout << *q << endl;
}
10.4.2思考
#include<iostream>
#include<vector>
#include<iterator>
#include<numeric>
#include<xstddef>
using namespace std;
int main()
{//位于iterator头文件中istream_iterator<int> int_it(cin);istream_iterator<int> int_eof;//位于numeric头文件中//第一次看懂仿函数哈哈//cout << accumulate(int_it, int_eof,//   0/*初值*/, // /*仿函数*/minus</*默认传入迭代器右值类型*/>()/*这里默认使用plus<>()*/) << endl;//初始化输入vector<int> vint2(int_it, int_eof);ostream_iterator<int> int_ot(cout, " ");for (auto it : vint2)*******++***++**++***int_ot++++++ = it;//*int_ot++=it;        此迭代器++,*等操作符不做任何事情,已看源码,这些操作符的重载返回只是它本身//思考的原因:io流没有<<或>>这个并没有移动迭代器,迭代器只是模拟其他的迭代器保持统一的格式//but,这个东西没有--因为这个符合io流的逻辑//    cout << it << endl;copy(vint2.begin(), vint2.end(), int_ot);元素赋值//vector<int> vint;//while (int_it != int_eof)// vint.push_back(*int_it++);//for (auto it : vint)//    cout << it << endl;
}//10.29
#include<iostream>
#include<vector>
#include<iterator>
#include<string>
#include<fstream>
using namespace std;
int main()
{ifstream filein("data");if (!filein)cerr << "无法读取文件" << endl;string str1;//必须包含头文件string,itreatoristream_iterator<string> str_it(filein); istream_iterator<string> eof;vector<string> vstr1(str_it, eof);ostream_iterator<string> str_ot(cout, "\n");for (auto t : vstr1)str_ot = t;filein.close();
}//10.30,10.31
#include<iostream>
#include<vector>
#include<iterator>
#include<string>
#include<algorithm>
using namespace std;
int main()
{istream_iterator<int> int_it(cin);istream_iterator<int> int_eof;ostream_iterator<int> int_ot(cout, ",");vector<int> vint1(int_it, int_eof);sort(vint1.begin(), vint1.end(), greater<>());unique_copy(vint1.begin(), vint1.end(), int_ot);
}//10.33
#include<iostream>
#include<vector>
#include<iterator>
#include<string>
#include<fstream>
#include<algorithm>
#include<functional>
using namespace std;
using namespace std::placeholders;
using ostit = ostream_iterator<int>;
using istit = istream_iterator<int>;
void fenjiou(const int &ot, ostit &int_ot1, ostit &int_ot2)
{if (ot & 1)int_ot1 = ot;elseint_ot2 = ot;
}
int main()
{ifstream filein1("data1");if (!filein1){cerr << "无法读取文件" << endl;exit(0);}istit int_it(filein1);istit int_eof;vector<int> vint(int_it, int_eof);ofstream fileout1("jishu");if (!fileout1){cerr << "无法读取文件" << endl;exit(0);}ofstream fileout2("oushu");if (!fileout2){cerr << "无法读取文件" << endl;exit(0);}ostit fileji_ot(fileout1," ");ostit fileou_ot(fileout2, " ");for_each(vint.begin(), vint.end(), bind(fenjiou, _1, fileji_ot, fileou_ot));filein1.close();fileout1.close();fileout2.close();
}

C++Primer 第10章 bind与迭代器相关推荐

  1. C++Primer 第10章lambda表达式

    C++Primer 第10章lambda表达式 泛型编程 咱们继续高歌猛进,下面我给大家继续讲关于算法的故事. 我们知道标准库容器中定义的算法大多数独立在算法之外,通常这些算法是通用的.它们可以处理不 ...

  2. C++ primer 第10章 泛型算法

    文章目录 概述 find count 初识泛型算法 只读算法 只读算法accumulate 只读算法equal 写容器元素的算法 算法fill 算法fill_n back_inserter 算法cop ...

  3. 2021-07-26c++primer 第10章的习题

    10.1 #include<iostream> #include<vector> #include<algorithm> #include<fstream&g ...

  4. C++ Primer 第10章 习题10.23

    //10.23.cpp //编写程序将被排除的单词存储在vector对象中,而不是存储在set对象中 //函数restricted_wc,根据形参指定文件建立单词排除集 //将被排除的单词存储在vec ...

  5. C++ Primer 第10章 pair类型

    //10.2.cpp //至少使用三种方法创建pair对象. //编写三个版本的程序,分别采用不同的方法来创建pair对象 //方法1:在定义pair对象时提供初始化式来创建pair对象#includ ...

  6. C++ Primer 第10章 习题10.24

    //10.24.cpp //建立一个单词排除集 //用于识别以's'借位.但这个结尾的's',又不能删除的单词 //使用这个排除集删除输入单词尾部的's',生成该单词的非复数版本 //如果输入的是排除 ...

  7. C++ Primer 第10章 习题 10.18

    //10-18.cpp //定义一个map对象,其元素的键是家族姓氏, //而值则是存储该家族孩子名字的vector对象. //进行基于家族姓氏的查询,输出该家族的所有孩子的名字 #include&l ...

  8. C++primer第十章 泛型算法 10.4 再探迭代器 10.5 泛型算法结构

    除了为每个容器定义的迭代器之外,标准库在头文件iterator中还定义了额外几种迭代器.这些迭代器包括以下几种. 插入迭代器(insert iterator):这些迭代器被绑定到一个容器上,可用来向容 ...

  9. c++ primer 第14章 习题解答

    14.1节 14.1答 不同点: 重载操作符必须具有至少一个class或枚举类型的操作数. 重载操作符不保证操作数的求值顺序,例如对&&和| | 的重载版本不再具有"短路求值 ...

最新文章

  1. session过期跳出irame
  2. SAP FI/CO模块调研问卷
  3. 1880: wjw的火车站(栈)
  4. hdu 1806线段树 区间合并
  5. DynamipsGUI下CISCO SDM的安装配置
  6. 同步,异步,多线程,你怎么看?
  7. java实现对大文件切割下载_Java实现大文件的切割与合并操作示例
  8. 知识小结(浮点数问题)
  9. (06)System Verilog 数组类型示例
  10. 年底各类大型年会晚会必用的高清丝绸海报设计背景来喽!
  11. TechOnTheNet的Oracle专题
  12. Unity编辑器扩展-基本界面编写
  13. 一次学会b站视频下载_合并_剪辑,亲测有效~
  14. 利用div实现遮罩层效果
  15. 10个小窍门,让你轻松准确搜索(转)
  16. 设计模式——七大原则
  17. Spark:reduceByKey与groupByKey进行对比
  18. Android 应用安装过程分析
  19. 【Akka】Akka容错处理
  20. 【Python笔记】获取星期几在指定年份的所有日期

热门文章

  1. .输入一行字符串,含有数字和非数字字符以及空格等,如: df23adfd56 2343?23dgjop535 如果将其中所有连续出现的数字视为一个整数,要求统计在该字符串中共有多少个整数,并将这些数依
  2. 腾讯帝国的下坡路 | 畅言
  3. ArcGIS配图/地图符号化的一些技巧与相关资料
  4. 转载:联想小新进入BIOS方法
  5. 打开excel表格会自动打开一个空表格
  6. mysql bit类型_MySQL bit类型
  7. PHP常说的SAPI是什么
  8. bootmgr快速修复win7_「科普」UEFI+GPT、Legacy+MBR引导模式介绍 引导修复
  9. 字节跳动Android开发大牛:90% 成功率的 BATZ Offer 收割机是怎样练成的!
  10. 饿了么交易系统设计思路