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 ...
随机推荐
- R 《回归分析与线性统计模型》page119,4.2
rm(list = ls()) library(openxlsx) library(MASS) data = read.xlsx("xiti_4.xlsx",sheet = 2) ...
- PHP: isset与empty的区别
PHP的isset()函数 一般用来检测变量是否设置 功能:检测变量是否设置 返回值: 若变量不存在则返回 FALSE 若变量存在且其值为NULL,也返回 FALSE 若变量存在且值不为NULL,则返 ...
- git参考
https://github.com/NewLifeX (redis.mq.数据海量查询.分布式任务调度)
- 吴裕雄--天生自然java开发常用类库学习笔记:国际化程序
import java.util.ResourceBundle ; public class InterDemo01{ public static void main(String args[]){ ...
- (四)Flex 布局教程和例子
布局的传统解决方案,基于盒状模型,依赖 display 属性 + position属性 + float属性.它对于那些特殊布局非常不方便,比如,垂直居中就不容易实现. 1.flex-direction ...
- redis报错MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist
解决方法:通过redis-cli连接到服务器后执行以下命令: config set stop-writes-on-bgsave-error no 注意:这种方法只是忽略了问题,并没有解决问题,具体问题 ...
- javascript中window.open()与window.location.href
1.window.location是window对象的属性,而window.open是window对象的方法 window.location是你对当前浏览器窗口的URL地址对象的参考! ...
- C#中类的字段或属性不被序列化成JSON或XML
将一个类序列化成JSON或XML时,如果某个字段或属性不想被序列化,则可以使用以下Attribute: 1.[Newtonsoft.Json.JsonIgnore]特性:使用Newtonsoft.Js ...
- SCA-CNN: Spatial and Channel-wise Attention in Convolutional Networks for Image Captioning
题目:SCA-CNN: Spatial and Channel-wise Attention in Convolutional Networks for Image Captioning 作者: Lo ...
- springboot+thymeleaf项目中使用th:replace访问templates子目录下的模板,会报错找不到模板路径
解决方法: 先将模板路径放置templates目录下,发现可以访问,说明th:replace是可以用的. 那可能是出现在路径问题上面. 于是我开始调错,改路径. 后来在网上查找资料.说了很多种方法. ...