本文是根据油管大神的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;
}

C++ STD Gems01的更多相关文章

  1. 【NX二次开发】NX内部函数,libuifw.dll文件中的内部函数

    本文分为两部分:"带参数的函数"和 "带修饰的函数". 浏览这篇博客前请先阅读: [NX二次开发]NX内部函数,查找内部函数的方法 带参数的函数: void U ...

  2. C++ std::set

    std::set template < class T, // set::key_type/value_type class Compare = less<T>, // set::k ...

  3. C++ std::priority_queue

    std::priority_queue template <class T, class Container = vector<T>, class Compare = less< ...

  4. C++ std::queue

    std::queue template <class T, class Container = deque<T> > class queue; FIFO queue queue ...

  5. C++ std::multimap

    std::multimap template < class Key, // multimap::key_type class T, // multimap::mapped_type class ...

  6. C++ std::map

    std::map template < class Key, // map::key_type class T, // map::mapped_type class Compare = less ...

  7. C++ std::list

    std::list template < class T, class Alloc = allocator > class list; List Lists are sequence co ...

  8. C++ std::forward_list

    std::forward_list template < class T, class Alloc = allocator > class forward_list; Forward li ...

  9. C++ std::deque

    std::deque template < class T, class Alloc = allocator > class deque; Double ended queue deque ...

随机推荐

  1. 2020寒假 05 ——eclipse安装scala环境

    在eclipse中安装Scala环境 1安装eclipse插件步骤,点击help,选择Eclipse Marketplace 2.输入Scala,点击go 3.选择搜索到的Scala IDE 4.7. ...

  2. Jetbrains推出了一款新的编程字体Mono

    今天看到新闻说Jetbrains推出了一款新的编程字体Mono,便到官网上下载试了一下, 在VS和VS Code中显示效果都非常不错,是并且支持连体,感兴趣的朋友可以到其官网下载试用一下.

  3. 十八、React react-router4.x中:实现路由模块化、以及嵌套路由父子组件传值

    一.路由模块化(用字典定义路由,然后循环出来) 1.官方文档参考 [官方文档]https://reacttraining.com/react-router/web/guides/quick-start ...

  4. UVA - 122 Trees on the level (二叉树的层次遍历)

    题意:给定结点值和从根结点到该结点的路径,若根到某个叶结点路径上有的结点输入中未给出或给出超过一次,则not complete,否则层次遍历输出所有结点. 分析:先建树,建树的过程中,沿途结点都申请了 ...

  5. POJ 3916:Duplicate Removal 将相近的重复元素删除

    Duplicate Removal Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1745   Accepted: 1213 ...

  6. 精简总结redis/rabbitmq/zookeeper在linux centos7上的安装

    因为本博主之前已经写过了相关的一些安装及集群,可以参考前面的记录,但是由于博最近更换了VM14和centos7,为了适应这些环境,所以后续会逐渐重新搭建相关环境,并对之前的安装思路进一步精简梳理,以期 ...

  7. JS - 使 canvas 背景透明

    canvas = document.getElementById('canvas1'); var context = canvas.getContext('2d');context.fillStyle ...

  8. 微信小程序调用用百度地图天气功能

    #小程序之调用百度地图天气功能 本篇博客主要介绍小程序在百度地图中获取天气信息,如有不全请指出.下面先上效果图 主要内容 百度地图API的个人密钥,也就是AK 请求百度地图API接口数据 获取到的信息 ...

  9. WTM框架在开发过程中如何动态迁移表和创建表

    官方迁移方法:https://wtmdoc.walkingtec.cn/#/Data/Migration 但是在实际开发过程中使用Add-Migration 方法迁移会发现,把系统内置的表也全部带出来 ...

  10. python面试题整理(一)

    python基础:1.列表生成式和生成器表达式有什么区别 我说的是首先写法不一样,列表生成式用[],生成器表达式用(),其次列表生成是一次性生成一个完整的列表,生成器表达式返回的是一个一个的值,占用内 ...