stl

Why is is it not possible to pass a const set<Derived*> as const set<Base*> to a function?

别说谁变了你拦得住时间么 提交于 2020-02-22 12:20:07
问题 Before this is marked as duplicate, I'm aware of this question, but in my case we are talking about const containers. I have 2 classes: class Base { }; class Derived : public Base { }; And a function: void register_objects(const std::set<Base*> &objects) {} I would like to invoke this function as: std::set<Derived*> objs; register_objects(objs); The compiler does not accept this. Why not? The set is not modifiable so there is no risk of non-Derived objects being inserted into it. How can I do

STL

徘徊边缘 提交于 2020-02-22 05:28:53
常用STL及其函数 list 链表:双链表 不常用 assign ( ) 给list赋值 back ( ) 返回最后一个元素 begin ( ) 返回指向第一个元素的迭代器 clear ( ) 删除所有元素 empty ( ) 如果list是空的则返回 true end ( ) 返回末尾的迭代器 erase ( ) 删除一个元素 front ( ) 返回第一个元素 get_allocator ( ) 返回list的配置器 insert ( ) 插入一个元素到list中 max_size ( ) 返回list能容纳的最大元素数量 merge ( ) 合并两个list pop_back ( ) 删除最后一个元素 pop_front ( ) 删除第一个元素 push_back ( ) 在list的末尾添加一个元素 push_front ( ) 在list的头部添加一个元素 rbegin ( ) 返回指向第一个元素的逆向迭代器 remove ( ) 从list删除元素 remove_if ( ) 按指定条件删除元素 rend ( ) 指向list末尾的逆向迭代器 resize ( ) 改变list的大小 reverse ( ) 把list的元素倒转 size ( ) 返回list中的元素个数 sort ( ) 给list排序 splice ( ) 合并两个list swap ( )

[C++]STL-函数对象基础知识

故事扮演 提交于 2020-02-21 18:59:00
函数对象(仿函数) 重载函数调用操作符的类,其对象称为函数对象(function object),即他们是行为类似函数的对象,也叫仿函数(functor),其实就是重载"()"操作符,使得类对象可以像函数那样被调用 注意: 函数对象(仿函数)是一个类,不是一个函数 假定某个类有一个重载的operator(),而且重载的operator()要求获取一个参数,就称其为“一元仿函数(unary functor)”;诸如此类,两个参数即称为二元仿函数等等 # include <iostream> # include <vector> # include <algorithm> using namespace std ; struct MyPrint { MyPrint ( ) : num ( 0 ) { } void operator ( ) ( int val ) { num ++ ; //记录函数调用次数 cout << val << endl ; } int num ; } ; void FunctorTest1 ( ) { MyPrint print ; print ( 45 ) ; //函数对象可以像普通函数那样调用 //函数对象也可以像普通函数那样接入参数 //函数对象超出了函数的概念,函数对象可以保存函数调用的状态 } int num = 0 ; //真正开发中

STL总结之vector

醉酒当歌 提交于 2020-02-21 17:01:47
STL中vector是通常作为数组使用,不过它更像一个动态数组,在实际项目开发中大量使用. 优点:存储空间连续,可以使用下标访问,时间复杂度O(1). 缺点:不适合从中间删除和添加元素. C++标准规定的vector模板声明: template < class T, class Allocator = allocator<T> > class vector; T : 存储的数据类型 Allocator : 存储空间分配器(默认为std::allocator<T>) 1)首先vector可以作为数组使用,因此我们可以在初始化vector时,指定元素个数和初始值. vector<int> v; //v中含有0个元素 vector<int> v(5); //v中含有5个元素 vector<int> v(5, 1); //v中含有5个元素,赋予初始值1 另外vector还可以通过其他方式构造: vector<int> third (v.begin(),v.end()); //通过v构造third vector<int> fourth (third); //通过copy构造 //通过数组进行初始化 int myints[] = {16,2,77,29}; vector<int> fifth (myints, myints + sizeof(myints) / sizeof(int) );

[置顶] Effective STL 学习笔记

久未见 提交于 2020-02-21 11:39:33
看Effective STL 作的一些笔记,希望对各位有帮助。 以下是50条条款及相关解释。 容器 1. 慎重选择容器类型,根据需要选择高效的容器类型。 2. 不要试图编写独立于容器类型的代码。 3. 确定容器中的对象拷贝正确而高效。也就是防止在存在继承关系时发生剥离。 4. 调用empty而不是检查size()是否为0来判断容器是否为空。 原因是调用empty比检查size()更加高效。 5. 尽量使用区间成员,而不是多次使用与之对应的单元素成员函数,原因是这样更加高效。 如尽量使用vector的 assign 或 insert 成员函数,而不是一直使用 push_back 。 6. 小心C++编译器最烦人的分析机制 。如 下面的代码中的第二句被C++解释成为了函数声明,这很奇怪但又符合标准。 ifstream dataFile ("ints.dat") list <int> data( istream_iterator<int>(dataFile), istream_iterator<int>() ); // 被解释成为函数声明 正确的写法是这样的,注意第一参数两边的括号: list <int> data( ( istream_iterator<int>(dataFile) ) , istream_iterator<int>() ); 这是因为C+

Ok to use std::getline() with a moved-from std::string?

前提是你 提交于 2020-02-21 10:35:52
问题 Is it safe and well-defined for the second argument to std::getline(std::istream&, std::string&) to be an lvalue referring to a moved-from std::string, and, if so, is that string restored from its moved-from state, so methods such as pop_back() can be safely invoked? Put more simply, does writing to a string with getline() have equivalent semantics to assigning to that string? Or more concretely, is the following (somewhat contrived) snippet well-defined and correct? std::ifstream f("foo.txt"

Ok to use std::getline() with a moved-from std::string?

别来无恙 提交于 2020-02-21 10:31:44
问题 Is it safe and well-defined for the second argument to std::getline(std::istream&, std::string&) to be an lvalue referring to a moved-from std::string, and, if so, is that string restored from its moved-from state, so methods such as pop_back() can be safely invoked? Put more simply, does writing to a string with getline() have equivalent semantics to assigning to that string? Or more concretely, is the following (somewhat contrived) snippet well-defined and correct? std::ifstream f("foo.txt"

Ok to use std::getline() with a moved-from std::string?

烂漫一生 提交于 2020-02-21 10:28:07
问题 Is it safe and well-defined for the second argument to std::getline(std::istream&, std::string&) to be an lvalue referring to a moved-from std::string, and, if so, is that string restored from its moved-from state, so methods such as pop_back() can be safely invoked? Put more simply, does writing to a string with getline() have equivalent semantics to assigning to that string? Or more concretely, is the following (somewhat contrived) snippet well-defined and correct? std::ifstream f("foo.txt"

Ok to use std::getline() with a moved-from std::string?

天大地大妈咪最大 提交于 2020-02-21 10:27:07
问题 Is it safe and well-defined for the second argument to std::getline(std::istream&, std::string&) to be an lvalue referring to a moved-from std::string, and, if so, is that string restored from its moved-from state, so methods such as pop_back() can be safely invoked? Put more simply, does writing to a string with getline() have equivalent semantics to assigning to that string? Or more concretely, is the following (somewhat contrived) snippet well-defined and correct? std::ifstream f("foo.txt"

STL List

冷暖自知 提交于 2020-02-21 10:10:01
View Code 1 #include <iostream> 2 #include<list> 3 4 using namespace std; 5 6 void PrintListContent(const list<int>& listInput); 7 8 int main() 9 {10 11 12 13 list<int> a;14 list<int> b;15 list<int>::iterator iter;16 17 b.push_back(100);18 b.push_back(200);19 b.push_back(300);20 b.push_back(400);21 b.push_back(500);22 23 24 PrintListContent(b);25 26 cout<< endl;27 28 a.push_front(4);29 a.push_front(3);30 a.push_front(2);31 a.push_front(1);32 33 a.push_back(5);34 35 36 37 iter = a.begin();38 ++iter;39 40 41 a.insert(iter,10);//开头的前面插入了1042 ++iter;43 44 a.insert(iter,4,20);//在后端插入4个2045 46 a