C++相关闲碎记录(6)

1、使用shared_ptr

#include <iostream>
#include <memory>
#include <set>
#include <deque>
#include <algorithm>
#include <string>class Item {
private:std::string name;float price;
public:Item(const std::string& n, float p = 0) :name(n), price(p) {}std::string getName() const {return name;}void setName(const std::string& n) {name = n;}float getPrice() const {return price;}float setPrice(float p) {price = p;return p;}
};template <typename Coll>
void printItems(const std::string& msg, const Coll& coll) {std::cout << msg << std::endl;for (const auto& elem: coll) {std::cout << " " << elem->getName() << ": " << elem->getPrice() << std::endl;}
}int main() {using namespace std;typedef shared_ptr<Item> ItemPtr;set<ItemPtr> allItems;deque<ItemPtr> bestsellers;bestsellers = {ItemPtr(new Item("Kong Yize", 20.10)),ItemPtr(new Item("A Midsummer Night's Dream", 14.99)),ItemPtr(new Item("The Maltese Falcon", 9.88))};allItems = {ItemPtr(new Item("Water", 0.44)),ItemPtr(new Item("Pizza", 2.22))};allItems.insert(bestsellers.begin(), bestsellers.end());printItems("bestsellers: ", bestsellers);printItems("all: ", allItems);cout << endl;for_each(bestsellers.begin(), bestsellers.end(),[](shared_ptr<Item>& elem) {elem->setPrice(elem->getPrice() * 2);});bestsellers[1] = *(find_if(allItems.begin(), allItems.end(),[](shared_ptr<Item> elem) {return elem->getName() == "Pizza";}));bestsellers[0]->setPrice(44.88);printItems("bestsellers: ", bestsellers);printItems("all: ", allItems);return 0;
}

面的set使用find的时候,会找出拥有相等value的元素,现在却比较的是内部的指针,

allItems.find(ItemPtr(new Item("Pizza", 2.22)))     //can't be successful,所以这里必须使用find_if算法。

2、advance

#include <iterator>
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;int main()
{list<int> coll;// insert elements from 1 to 9for (int i=1; i<=9; ++i) {coll.push_back(i);}list<int>::iterator pos = coll.begin();// print actual elementcout << *pos << endl;// step three elements forwardadvance (pos, 3);// print actual elementcout << *pos << endl;// step one element backwardadvance (pos, -1);// print actual elementcout << *pos << endl;
}
输出:
1
4
3

3、iter_swap()

 交换迭代器的值,迭代器类型不必相同,所指的两个值必须可以相互赋值。

#include <iostream>
#include <list>
#include <algorithm>
#include <iterator>
#include "print.hpp"
using namespace std;int main()
{list<int> coll;// insert elements from 1 to 9for (int i=1; i<=9; ++i) {coll.push_back(i);}PRINT_ELEMENTS(coll);// swap first and second valueiter_swap (coll.begin(), next(coll.begin()));PRINT_ELEMENTS(coll);// swap first and last valueiter_swap (coll.begin(), prev(coll.end()));PRINT_ELEMENTS(coll);
}
输出:
1 2 3 4 5 6 7 8 9 
2 1 3 4 5 6 7 8 9
9 1 3 4 5 6 7 8 2
#include <iterator>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;int main()
{// create list with elements from 1 to 9vector<int> coll = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };// find position of element with value 5vector<int>::const_iterator pos;pos = find (coll.cbegin(), coll.cend(),5);// print value to which iterator pos referscout << "pos:  " << *pos << endl;// convert iterator to reverse iterator rposvector<int>::const_reverse_iterator rpos(pos);// print value to which reverse iterator rpos referscout << "rpos: " << *rpos << endl;
}
输出:
pos:  5
rpos: 4

 

#include <iterator>
#include <iostream>
#include <deque>
#include <algorithm>
using namespace std;void print (int elem)
{cout << elem << ' ';
}int main()
{// create deque with elements from 1 to 9deque<int> coll = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };// find position of element with value 2deque<int>::const_iterator pos1;pos1 = find (coll.cbegin(), coll.cend(),  // range2);                          // value// find position of element with value 7deque<int>::const_iterator pos2;pos2 = find (coll.cbegin(), coll.cend(),  // range7);                          // value// print all elements in range [pos1,pos2)for_each (pos1, pos2,     // rangeprint);         // operationcout << endl;// convert iterators to reverse iteratorsdeque<int>::const_reverse_iterator rpos1(pos1);deque<int>::const_reverse_iterator rpos2(pos2);// print all elements in range [pos1,pos2) in reverse orderfor_each (rpos2, rpos1,   // rangeprint);         // operationcout << endl;
}
输出:
2 3 4 5 6 
6 5 4 3 2

4、使用base()将reverse迭代器转回正常

#include <iterator>
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;int main()
{// create list with elements from 1 to 9list<int> coll = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };// find position of element with value 5list<int>::const_iterator pos;pos = find (coll.cbegin(), coll.cend(),  // range5);                          // value// print value of the elementcout << "pos:   " << *pos << endl;// convert iterator to reverse iteratorlist<int>::const_reverse_iterator rpos(pos);// print value of the element to which the reverse iterator referscout << "rpos:  " << *rpos << endl;// convert reverse iterator back to normal iteratorlist<int>::const_iterator rrpos;rrpos = rpos.base();// print value of the element to which the normal iterator referscout << "rrpos: " << *rrpos << endl;
}
输出:
pos:   5
rpos:  4
rrpos: 5

5、back_inserter

#include <vector>
#include <algorithm>
#include <iterator>
#include "print.hpp"using namespace std;int main() {vector<int> coll;back_insert_iterator<vector<int>> iter(coll);*iter = 1;*iter++;*iter = 2;iter++;*iter = 3;PRINT_ELEMENTS(coll);// convenient wayback_inserter(coll) = 44;back_inserter(coll) = 55;PRINT_ELEMENTS(coll);// use back inserter to append all elements again// - reserve enough memory to avoid reallocationcoll.reserve(2*coll.size());copy(coll.begin(), coll.end(), back_inserter(coll));PRINT_ELEMENTS(coll);return 0;
}
输出:
1 2 3 
1 2 3 44 55
1 2 3 44 55 1 2 3 44 55

6、front_inserter

#include <list>
#include <algorithm>
#include <iterator>
#include "print.hpp"using namespace std;int main() {list<int> coll;front_insert_iterator<list<int>> iter(coll);*iter = 1;iter++;*iter = 2;iter++;*iter = 3;PRINT_ELEMENTS(coll);front_inserter(coll) = 44;front_inserter(coll) = 55;PRINT_ELEMENTS(coll);copy(coll.begin(), coll.end(), front_inserter(coll));PRINT_ELEMENTS(coll);return 0;
}
输出:
3 2 1 
55 44 3 2 1
1 2 3 44 55 55 44 3 2 1

7、inserter

#include <set>
#include <list>
#include <algorithm>
#include <iterator>
#include "print.hpp"using namespace std;int main() {set<int> coll;insert_iterator<set<int>> iter(coll, coll.begin());*iter = 1;iter++;*iter = 2;iter++;*iter = 3;PRINT_ELEMENTS(coll,"set: ");inserter(coll, coll.end()) = 44;inserter(coll, coll.end()) = 55;PRINT_ELEMENTS(coll, "set: ");list<int> coll2;copy(coll.begin(), coll.end(), inserter(coll2, coll2.begin()));PRINT_ELEMENTS(coll2, "list: ");copy(coll.begin(), coll.end(), inserter(coll2, ++coll2.begin()));PRINT_ELEMENTS(coll2, "list: ");return 0;
}
输出:
set: 1 2 3 
set: 1 2 3 44 55
list: 1 2 3 44 55
list: 1 1 2 3 44 55 2 3 44 55

 8、ostream迭代器

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>using namespace std;int main() {ostream_iterator<int> intWriter(cout, "\n");*intWriter = 43;intWriter++;*intWriter = 77;intWriter++;*intWriter = -5;vector<int> coll = {1, 2, 3, 4, 5, 6, 7, 8, 9};copy(coll.begin(), coll.end(), ostream_iterator<int>(cout));cout << endl;copy(coll.begin(), coll.end(), ostream_iterator<int>(cout, " < "));cout << endl;return 0;
}
输出:
43
77
-5
123456789
1 < 2 < 3 < 4 < 5 < 6 < 7 < 8 < 9 <

 9、istream迭代器

#include <iterator>
#include <iostream>
#include <string>
#include <algorithm>using namespace std;int main() {istream_iterator<string> cinPos(cin);ostream_iterator<string> coutPos(cout, " ");while (cinPos != istream_iterator<string>()) {advance(cinPos, 2);if (cinPos != istream_iterator<string>()) {*coutPos++ = *cinPos++;}}cout << endl;return 0;
}
输入:
No one objects if you are doing
a good programming job for 
someone whom you respect
输出:
objects are good for you

10、move迭代器

std::vector<string> v2(make_move_iterator(s.begin()),make_move_iterator(s.end()));

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/265572.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

班级管理的重要性

班级管理&#xff0c;就像是一座桥&#xff0c;连接着学生和老师&#xff0c;它的重要性不言而喻。 营造良好的学习氛围 班级管理不仅仅是维护秩序&#xff0c;更是营造一个积极向上的学习氛围。一个好的班级管理&#xff0c;能让学生更加专注于学习&#xff0c;提高学习效率。…

【JAVA杂货铺】一文带你走进面向对象编程的构造方法 | Java| 面向对象编程 | (中)

&#x1f308;个人主页: Aileen_0v0&#x1f525;系列专栏:Java学习系列专栏&#x1f4ab;个人格言:"没有罗马,那就自己创造罗马~" 目录 回顾 构造方法 this关键字 面试题 构造方法的类型 下节预告 代码块 &#x1f352;回顾 之前我们学习了什么是类 什么是…

观海微电子----LVDS接口

LVDS&#xff08;Low Voltage Differential Signaling&#xff0c;即低电压差分信号&#xff09; 常见于高清分辨率的屏幕&#xff0c;是TTL接口的升级版&#xff0c;LVDS接口是在TTL的技术上编码而成&#xff0c;使用低压差分信号来进行传输。 这种技术的核心是采用极…

Java面试遇到的一些常见题

目录 1. Java语言有几种基本类型&#xff0c;分别是什么&#xff1f; 整数类型&#xff08;Integer Types&#xff09;&#xff1a; 浮点类型&#xff08;Floating-Point Types&#xff09;&#xff1a; 字符类型&#xff08;Character Type&#xff09;&#xff1a; 布尔类…

java学习part42反射

187-反射机制-反射的理解与使用举例_哔哩哔哩_bilibili

大数据技术1:大数据发展简史

前言&#xff1a;学习大数据技术&#xff0c;知道会用已经够了&#xff0c;但是要想走得更远&#xff0c;应该了解它发展的来龙去脉&#xff0c;为何会有新的技术/工具的出现&#xff0c;相比老的技术有什么样的进步。 1、传统数据处理系统存在的问题 随着信息时代互联网技术爆…

LeetCode 每日一题 Day 9 ||简单dp

70. 爬楼梯 假设你正在爬楼梯。需要 n 阶你才能到达楼顶。 每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢&#xff1f; 示例 1&#xff1a; 输入&#xff1a;n 2 输出&#xff1a;2 解释&#xff1a;有两种方法可以爬到楼顶。 1 阶 1 阶2 阶 示例 2&am…

Nacos源码解读08——基于JRaft实现AP模式

什么是JRaft算法 详情参考 https://www.cnblogs.com/luozhiyun/p/13150808.html http://www.zhenchao.io/2020/06/01/sofa/sofa-jraft-node-startup/ Nacos对JRaft算法的应用 当Nacos使用嵌入数据源&#xff08; -DembeddedStoragetrue&#xff0c;每个节点有一个数据源&…

【FPGA】Quartus18.1打包封装网表文件(.qxp)详细教程

当我们在做项目的过程中&#xff0c;编写的底层Verilog代码不想交给甲方时怎么办呢&#xff1f;此时可以将源代码打包封装成网表文件&#xff08;.qxp&#xff09;进行加密&#xff0c;并且在工程中进行调用。 Quartus II的.qxp文件为QuartusII Exported Partition&#xff0c;…

ardupilot开发 --- git 篇

一些概念 工作区&#xff1a;就是你在电脑里能看到的目录&#xff1b;暂存区&#xff1a;stage区 或 index区。存放在 &#xff1a;工作区 / .git / index 文件中&#xff1b;版本库&#xff1a;本地仓库&#xff0c;存放在 &#xff1a;工作区 / .git 中 关于 HEAD 是所有本地…

深入理解ARP协议:网络通信中的地址解析协议

目录 引言 什么是ARP协议&#xff1f; ARP协议的工作原理 1. ARP请求 2. ARP应答 3. ARP缓存 ARP协议的作用 结语 引言 在计算机网络中&#xff0c;地址解析协议&#xff08;ARP&#xff0c;Address Resolution Protocol&#xff09;扮演着重要的角色。ARP协议负责将网…

盘点面试大热门之区间问题

关卡名 理解与贪心有关的高频问题 我会了✔️ 内容 1.理解区间问题如何解决 ✔️ 2.理解字符串分割问题 ✔️ 3.理解加油站问题如何解决 ✔️ 1. 区间问题 区间问题也是面试中经常遇到的情况&#xff0c;这类面试题目还挺讨巧的&#xff0c;很容易考察出应聘者到底会不会…