C++ STD Gems01
本文是根据油管大神的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的更多相关文章
- 【NX二次开发】NX内部函数,libuifw.dll文件中的内部函数
本文分为两部分:"带参数的函数"和 "带修饰的函数". 浏览这篇博客前请先阅读: [NX二次开发]NX内部函数,查找内部函数的方法 带参数的函数: void U ...
- C++ std::set
std::set template < class T, // set::key_type/value_type class Compare = less<T>, // set::k ...
- C++ std::priority_queue
std::priority_queue template <class T, class Container = vector<T>, class Compare = less< ...
- C++ std::queue
std::queue template <class T, class Container = deque<T> > class queue; FIFO queue queue ...
- C++ std::multimap
std::multimap template < class Key, // multimap::key_type class T, // multimap::mapped_type class ...
- C++ std::map
std::map template < class Key, // map::key_type class T, // map::mapped_type class Compare = less ...
- C++ std::list
std::list template < class T, class Alloc = allocator > class list; List Lists are sequence co ...
- C++ std::forward_list
std::forward_list template < class T, class Alloc = allocator > class forward_list; Forward li ...
- C++ std::deque
std::deque template < class T, class Alloc = allocator > class deque; Double ended queue deque ...
随机推荐
- uboot前传
1.uboot到底是干嘛的 uboot主要作用是用来启动操作系统内核. uboot还要负责部署整个计算机系统. uboot中还有操作Flash等板子上硬盘的驱动. uboot还得提供一个命令行界面供人 ...
- eshop-环境配置
1. iptables # Generated by iptables-save v1. :: #*filter #:INPUT ACCEPT [:] #:FORWARD ACCEPT [:] #:O ...
- JS 选择电脑中的文件目录
按钮调用方法function CarryOut(){ var inputObj=document.createElement('input') inputObj.setAttribute('id',' ...
- UVA 1601 双向BFS
但是我们还不是很清楚每一次的状态怎么储存?我们可以用一个结构体,将每次的位置存起来,但是这个程序中用了一个更好的储存方法:我们知道最大的格数是16*16个,也就是256个,那么我们转换为二进制表示就是 ...
- JuJu团队12月1号工作汇报
JuJu团队12月1号工作汇报 JuJu Scrum 团队成员 今日工作 剩余任务 困难 于达 修改generator函数 优化代码 不熟悉julia 婷婷 和金华一起调试main.jl 继 ...
- C++输入问题探究
突发奇想对C++输入输出做一点研究,主要是做笔试题自己写输入老是花很多时间,所以做一个总结. 对于输入多行字符串,代码如下: #include<iostream> #include< ...
- esxi 主机用户功能说明
1. root 用户 默认情况下,每个 ESXi 主机都有一个具有管理员角色的 root 用户帐户.该 root 用户帐户可用于本地管理,并可用于将主机连接到 vCenter Server. 此公共 ...
- ssh: connect to host 120.79.26.164 port 22: Connection timed out报错问题
要是使用阿里云服务器,出现这种错误,一般是端口没有打开.需要在阿里云控制台中设置端口后,即可使用ssh连接.
- spring源码 AutowireCapableBeanFactory接口
对于想要拥有自动装配能力,并且想把这种能力暴露给外部引用的BeanFactory类需要实现此接口.正常情况下,不要使用此接口应该更倾向于使用BeanFactory或者ListableBeanFacto ...
- Atom :奥特曼的使用
最近在使用atom的编译器,很不爽,什么快捷键,还有识别vue的页面,还有注释这种快捷下载下来的都没有 必须到setting里面的install里下载,我能大声的说我很不爽吗............ ...