本文是根据油管大神的C++标准库课程的一个学习笔记,该课程主要介绍c++标准库中一些非常有用并且代码经常用到的工具。

copy 、copy_backward 、copy_n 、copy_if、swap_ranges

#include <iostream>
#include <iterator>
#include <string>
#include <algorithm>
#include <vector>
#include <cctype> template<typename Container>
void write_to_cout(const Container& container, const char* delimiter = " ")
{
std::copy( container.begin(), container.end(),
std::ostream_iterator<typename Container::value_type>( std::cout, delimiter) );
} // 测试函数copy
void test0()
{
// init test constainer
std::vector<std::string> a = {"zero", "one", "two", "three", "four", "five", "six"};
std::vector<std::string> b = {"0", "1", "2", "4", "5", "6", "7", "8", "9"}; // wtire initial statements
write_to_cout(a);
std::cout << std::endl;
write_to_cout(b);
std::cout << std::endl; //test algorithm
std::copy(a.begin(), a.begin() + 3, b.begin() + 4);
write_to_cout(b);
std::cout << std::endl;
std::cout << std::endl;
} void test1()
{
std::vector<std::string> a = {"zero", "one", "two", "three", "four", "five", "six"};
std::vector<std::string> b = {"0", "1", "2", "4", "5", "6", "7", "8", "9"}; write_to_cout(a);
std::cout << std::endl;
write_to_cout(b);
std::cout << std::endl; std::copy(a.begin(), a.end(), std::back_inserter(b));
write_to_cout(b);
std::cout << std::endl;
std::cout << std::endl;
} void test2()
{
// std::vector<std::string> a = {"zero", "one", "two", "three", "four", "five", "six"};
std::vector<std::string> b = {"0", "1", "2", "4", "5", "6", "7", "8", "9"}; // write_to_cout(a);
// std::cout << std::endl;
write_to_cout(b);
std::cout << std::endl; // test algorithm
std::copy(b.begin(), b.begin() + 4, b.begin() +3); // 元素从前往后挨个复制过去
write_to_cout(b);
std::cout << std::endl;
std::cout << std::endl;
} void test3()
{
std::vector<std::string> a = {"zero", "one", "two", "three", "four", "five", "six"};
std::vector<std::string> b = {"0", "1", "2", "4", "5", "6", "7", "8", "9"}; write_to_cout(a);
std::cout << std::endl;
write_to_cout(b);
std::cout << std::endl; //test algorithm
std::copy_backward(a.begin(), a.begin() + 2, b.begin() + 3); //从后往前挨个复制过去
write_to_cout(b);
std::cout << std::endl;
std::cout << std::endl;
} void test4()
{
std::vector<int> a;
std::copy_n(std::istream_iterator<int>(std::cin), 5, std::back_inserter(a)); // 输入指定的数量的元素
write_to_cout(a);
std::cout << std::endl;
std::cout << std::endl;
} void test5()
{
std::string a = "HellO, WOrlD";
std::string b = "wHat are yoU dOinG"; write_to_cout(a);
std::cout << std::endl;
write_to_cout(b);
std::cout << std::endl; //test algorithm
std::string uppers;
// 有条件地复制
std::copy_if(a.begin(), a.end(), std::back_inserter(uppers), [](unsigned char c){return std::isupper(c);} ); // 为什么直接写std::isupper不行呢
std::copy_if(b.begin(), b.end(), std::back_inserter(uppers), [](unsigned char c){return std::isupper(c);} ); write_to_cout(uppers);
std::cout << std::endl << std::endl;
} void test6()
{
std::vector<std::string> a = {"zero", "one", "two", "three", "four", "five", "six"};
std::vector<std::string> b = {"0", "1", "2", "4", "5", "6", "7", "8", "9"}; write_to_cout(a);
std::cout << std::endl;
write_to_cout(b);
std::cout << std::endl; //test algorithm
std::swap_ranges(a.begin(), a.begin() + 3, b.begin()); //作用于copy等效
write_to_cout(b);
std::cout << std::endl << std::endl; } int main()
{
test0();
test1();
test2();
test3();
test4();
test5();
test6();
return 0;
}

最新文章

  1. 如何 判断 设备 是否 连接 上 了 wifi
  2. 在Ubuntu下进行MongoDB安装步骤
  3. 斑点检测(LoG,DoG)(下)
  4. JiaThis WordPress分享插件安装全攻略
  5. 拿搬东西来解释udp tcpip bio nio aio aio异步
  6. HDU3591找零,背包
  7. iOS开发QQ空间半透明效果的实现
  8. 安卓OpenGL入门
  9. Python实现删除目录下相同文件
  10. hdoj 1175 (bfs)
  11. jQuery获得页面绝对和相对的位置
  12. Git常用指令和GitHub操作总结
  13. Linux Install geoip
  14. 【UOJ】#49.铀仓库
  15. Cobbler自动装机--2
  16. ssh问题:ssh_exchange_identification: Connection closed by remote host
  17. PyQt5--QColorDiaglog
  18. 阿里云流计算BLINK
  19. Codeforces Beta Round #94 div2 D 优先队列
  20. 提高C++程序运行效率的10个简单方法

热门文章

  1. 文本情感分析(二):基于word2vec、glove和fasttext词向量的文本表示
  2. 12.redis的AOF持久化深入讲解各种操作和相关实验
  3. SpringBoot-配置Java方式
  4. 安卓Recycleview简单的网格布局-初学者的关键点
  5. MapReduce On Yarn的执行流程
  6. ACM-小偷的背包
  7. Charles抓包(HTTP)
  8. javaboot+es
  9. 适配器之SimpleAdapter
  10. CAN分帧发送程序说明