stl

Why I don't get an exception when using operator [] with index out of range in std::vector?

北战南征 提交于 2019-12-30 04:53:05
问题 Why when I use below code I don't get an out of range exception ? std::vector<int> v; v.resize(12); int t; try { t = v[12]; } catch(std::exception e){ std::cout<<"error:"<<e.what()<<"\n"; } 回答1: By using operator[] you are essentially telling the compiler "I know what I'm doing. Trust me." If you access some element that is outside of the array it's your fault. You violated that trust; you didn't know what you were doing. The alternative is to use the at() method. Here you are asking the

C++ 学习书目

不想你离开。 提交于 2019-12-30 04:51:35
C++书单 第一篇 一些同学问我,如何学好C++,我没有别的办法给你们,唯一的办法就是读书,读大量的书,就可以解决。要把C++作为日常语言,而不是一种程序语言,这样就好办了。 有人又要问我,那么我应该读什么书才好?没有时间怎么办?我只能对你们说,没时间的话,就别学C++了,做你们喜欢做的事。生活中没有C++,也同样美好。如果你准备学,一定要学好,那么我开个书单,应该问题不是甚大。 首先肯定要读一读Bjarne Stroustrup的The Design and Evolution of C++,了解 一下这个语言的历史。接下来就可以看别的书了,但要不停地回头看这本书,看到你不断地学到的新技术是怎么样一点点地被接纳到这个语言中去的。 第一本书因人而异,基础好一些的,可以看Stanley B. Lippman的C++ Primer,这本书非常地巨大,你打星号的部分可以不要看。基础不太好的,可以看Stanley B.Lippman的Essential C++,这本书份量要轻得多,不过四个C++的范型都讲了,而且讲得非常清楚。 第二本应该停止技术层面的东西,静下心来看看Pike和Kernighan的The Practice of Programming,好好地整理一下,在程序设计中应该有哪些注意的事项。这本非常薄的booklet,可以说是程序员必读的指南。 第三本书,就应该是Bruce

iostream and large file support

倖福魔咒の 提交于 2019-12-30 04:38:06
问题 I'm trying to find a definitive answer and can't, so I'm hoping someone might know. I'm developing a C++ app using GCC 4.x on Linux (32-bit OS). This app needs to be able to read files > 2GB in size. I would really like to use iostream stuff vs. FILE pointers, but I can't find if the large file #defines (_LARGEFILE_SOURCE, _LARGEFILE64_SOURCE, _FILE_OFFSET_BITS=64) have any effect on the iostream headers. I'm compiling on a 32-bit system. Any pointers would be helpful. 回答1: This has already

c++ stl容器set成员函数介绍及set集合插入,遍历等用法举例

纵饮孤独 提交于 2019-12-30 04:26:26
c++ stl集合set介绍 c++ stl集合(Set)是一种包含已排序对象的关联容器。set/multiset会根据待定的排序准则,自动将元素排序。两者不同在于前者不允许元素重复,而后者允许。 1) 不能直接改变元素值,因为那样会打乱原本正确的顺序,要改变元素值必须先删除旧元素,则插入新元素 2) 不提供直接存取元素的任何操作函数,只能通过迭代器进行间接存取,而且从迭代器角度来看,元素值是常数 3) 元素比较动作只能用于型别相同的容器(即元素和排序准则必须相同) set模板原型://Key为元素(键值)类型 1 template < class Key, class Compare=less<Key>, class Alloc=STL_DEFAULT_ALLOCATOR(Key) > 从原型可以看出,可以看出比较函数对象及内存分配器采用的是默认参数,因此如果未指定,它们将采用系统默认方式。 set的各成员函数列表如下: c++ stl容器set成员函数:begin()--返回指向第一个元素的迭代器 c++ stl容器set成员函数:clear()--清除所有元素 c++ stl容器set成员函数:count()--返回某个值元素的个数 c++ stl容器set成员函数:empty()--如果集合为空,返回true c++ stl容器set成员函数:end()-

C++ set容器简单用法

半城伤御伤魂 提交于 2019-12-30 04:23:39
set是关联容器,类似于集合,里面的元素不会重复,而且呈现为有序性 常用操作: using namespace std; set<int>:s; 1.元素插入:s.insert() 2.中序遍历:类似vector遍历(用迭代器) set<int>::reverse_iterator rit=s.begin(); while(rit!=s.end()) {   printf("%d",*rit++); } 3.反向遍历:利用反向迭代器reverse_iterator。 set<int>::reverse_iterator rit; for(rit=s.rbegin();rit!=s.rend();rit++) 4.元素删除:与插入一样,可以高效的删除,并自动调整使红黑树平衡。 set<int> s; s.erase(2); //删除键值为2的元素 s.clear(); 5.元素检索:find(),若找到,返回该键值迭代器的位置,否则,返回最后一个元素后面一个位置。 set<int> s; set<int>::iterator it; it=s.find(5); //查找键值为5的元素 if(it!=s.end()) //找到 cout<<*it<<endl; else //未找到 cout<<"未找到"; C++成员函数: c++ stl容器set成员函数:begin()-

set--常见成员函数及基本用法

左心房为你撑大大i 提交于 2019-12-30 04:21:55
c++ stl集合set介绍 c++ stl集合(Set)是一种包含已排序对象的关联容器。 set/multiset会根据待定的排序准则,自动将元素排序。两者不同在于前者不允许元素重复,而后者允许。 c++ stl集合set介绍 c++ stl集合(Set)是一种包含已排序对象的关联容器。set/multiset会根据待定的排序准则,自动将元素排序。两者不同在于前者不允许元素重复,而后者允许。 1) 不能直接改变元素值,因为那样会打乱原本正确的顺序,要改变元素值必须先删除旧元素,则插入新元素 2) 不提供直接存取元素的任何操作函数,只能通过迭代器进行间接存取,而且从迭代器角度来看,元素值是常数 3) 元素比较动作只能用于型别相同的容器(即元素和排序准则必须相同) set模板原型://Key为元素(键值)类型 1 template < class Key, class Compare=less<Key>, class Alloc=STL_DEFAULT_ALLOCATOR(Key) > 从原型可以看出,可以看出比较函数对象及内存分配器采用的是默认参数,因此如果未指定,它们将采用系统默认方式。 set的各成员函数列表如下: c++ stl容器set成员函数:begin()--返回指向第一个元素的迭代器 c++ stl容器set成员函数:clear()--清除所有元素 c++

STL之set&multiset使用简介

风格不统一 提交于 2019-12-30 04:16:45
关于set,必须说明的是set关联式容器。set作为一个容器也是用来存储同一数据类型的数据类型,并且能从一个数据集合中取出数据,在set中每个元素的值都唯一,而且系统能根据元素的值自动进行排序。应该注意的是set中数元素的值不能直接被改变。C++ STL中标准关联容器set, multiset, map, multimap内部采用的就是一种非常高效的平衡检索二叉树:红黑树,也成为RB树(Red-Black Tree)。RB树的统计性能要好于一般平衡二叉树,所以被STL选择作为了关联容器的内部结构。 为何每次insert之后,以前保存的iterator不会失效? iterator这里就相当于指向节点的指针,内存没有变,指向内存的指针怎么会失效呢(当然被删除的那个元素本身已经失效了)。相对于vector来说,每一次删除和插入,指针都有可能失效,调用push_back在尾部插入也是如此。因为为了保证内部数据的连续存放,iterator指向的那块内存在删除和插入过程中可能已经被其他内存覆盖或者内存已经被释放了。即使时push_back的时候,容器内部空间可能不够,需要一块新的更大的内存,只有把以前的内存释放,申请新的更大的内存,复制已有的数据元素到新的内存,最后把需要插入的元素放到最后,那么以前的内存指针自然就不可用了。特别时在和find等算法在一起使用的时候,牢记这个原则:

Has C++11 changed requirements for elements of STL containers, and how?

一世执手 提交于 2019-12-30 03:41:09
问题 Recently I'm surprised at the fact std::unique_ptr is acceptable for elements of STL containers, because I thought these elements are required to provide functions below(this page says the same): public default constructor with no arguments public copy constructor public copy assignment operator function public destructor But std::unique_ptr is not copyable to make the pointer it holds owned by a single object, that contradicts the requirements above. Has the standard changed the requirements

How sets, multisets, maps and multimaps work internally

纵饮孤独 提交于 2019-12-30 03:21:05
问题 How do multisets work? If a set can't have a value mapped to a key, does it only hold keys? Also, how do associative containers work? I mean vector and deque in the memory is located sequentially it means that deleting/removing (except beginning [deque] and end [vector, deque]) are slow if they are large. And list is a set of pointers which are not sequentially located in the memory which causes longer search but faster delete/remove. How are sets, maps, multisets and multimaps stored and how

Why can't for_each modify its functor argument?

风格不统一 提交于 2019-12-30 03:15:09
问题 http://www.cplusplus.com/reference/algorithm/for_each/ Unary function taking an element in the range as argument. This can either be a pointer to a function or an object whose class overloads operator(). Its return value, if any, is ignored. According to this article, I expected that for_each actually modifies the object given as its third argument, but it seems like for_each operates on a temporary object, and doesn't even modify the object given to it. So, why is it implemented in that way?