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