stl

C++ STL vector容器 简单实用整理

半城伤御伤魂 提交于 2020-01-14 23:31:01
一、什么是vector? 1.1 介绍: vector 是c++中stl库里的一个容器,实质就是一个 长度未定的数组 ,而且他还有一些常用操作“封装”在了vector类型内部。 1.2 举例说明: vector是一个模板类,所以需要用vector< int > a或者vector< double > b来声明一个vector,并且要引用#include< vector >头文件。 vector< int > a是声明了一个长度可变的int型数组a,类似于int a[]的整数数组,同理vector< string > a类似于string a[]的字符串数组。 二、如何使用vector? 2.1 头文件: #include< vector > 2.2 声明数组: # include <iostream> # include <vector> using namespace std ; int main ( ) { vector < int > a ; //声明了一个长度未知的int型数组a vector < double > b ; //声明了一个长度未知的double型数组b return 0 ; } 2.3 vector中封装的便捷操作: 假如a是一个vector,可以用a.size()读取他的大小;a.resize()改变大小;a.p ush_back()向尾部添加元素;a

GDB 7.6 STL pretty print with gcc-4.8 and mac os 10.9

你。 提交于 2020-01-14 19:42:06
问题 I'm struggling to get the pretty prints as described here in gdb working on my mac. I downloaded the latest gdb through macports and using gcc-4.8 . I loaded the ~/.gdbinit file and the printers are registered, but whenever I call print myVector it gives me the raw output. Any suggestions what I could do? Thanks a lot guys! 回答1: To have pretty printer with libc++ (new library used in Clang++/LLVM) use this new pretty printer: https://github.com/koutheir/libcxx-pretty-printers The .gdbinit is

is std::vector same as array[number]? [duplicate]

∥☆過路亽.° 提交于 2020-01-14 18:50:08
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: Are std::vector elements guaranteed to be contiguous? does std::vector always contain the data in sequential memory addresses as array[number]? 回答1: For all types except bool, the standard requires the elements are contiguous in memory: 23.2.4/1 ... The elements of a vector are stored contiguously, meaning that if v is a vector where T is some type other than bool, then it obeys the identity &v[n] == &v[0] + n

How to provide const interface with iterators to a collection?

匆匆过客 提交于 2020-01-14 13:42:11
问题 I would like to create a function with a signature like this: // Set found to be an iterator to the location of key in map or end() // if not found. bool lookup(const Key &key, const std::map<Key, Value> &map, std::map<Key, Value>::const_iterator &found); But I would like to also call it in cases where the map and iterator are not const so that I can modify the found value: const Key key; std::map<Key, Value> map; std::map<Key, Value>::iterator found; if (lookup(key, map, found)) { found-

C++ STL sort 函数的用法

99封情书 提交于 2020-01-14 13:39:07
sort 在 STL 库中是排序函数,有时冒泡、选择等 $\mathcal O(n^2)$ 算法会超时时,我们可以使用 STL 中的快速排序函数 $\mathcal O(n \ log \ n)$ 完成排序 sort 在 algorithm 库里面,原型如下: template <class RandomAccessIterator> void sort ( RandomAccessIterator first, RandomAccessIterator last ); template <class RandomAccessIterator, class Compare> void sort ( RandomAccessIterator first, RandomAccessIterator last, Compare comp ); 我们会发现 sort 有两种形式一个有三个参数,一个有两个参数,我们先讲讲两个参数的吧! sort 的前两个参数是起始地址和中止地址 如:sort(a,a+n) 表示对 a[0] ... a[n-1] 排序 代码如下: #include <algorithm> #include <cstdio> using namespace std; int main() { int n,a[1001]; scanf("%d",&n); for (int i =

STL set 使用小结

浪子不回头ぞ 提交于 2020-01-14 13:24:41
这是微软帮助文档中对集合(set)的解释: “描述了一个控制变长元素序列的对象(注:set中的key和value是Key类型的,而map中的key和value是一个pair结构中的两个分 量)的模板类, 每一个元素包含了一个排序键(sort key)和一个值(value)。对这个序列可以进行查找、插入、删除序列中的任意一个元素,而完成这些操作的时间同这个序列中元素个数的对数成比例关 系, 并且当游标指向一个已删除的元素时,删除操作无效。” 而一个经过更正的和更加实际的定义应该是:一个集合(set)是一个容器,它其中所包含的元素的值是唯一的。这在收集一个数据的具体值的时候是有用的。集 合中的元素按一定的顺序排列,并被作为集合中的实例。如果你需要一个键/值对(pair)来存储数据,map是一个更好的选择。一个集合通过一个链表来组 织,在插入操作和删除操作上比向量(vector)快,但查找或添加末尾的元素时会有些慢。 #include<iostream> #include<string> #include<set> using namespace std; int main() { set<string> strset; set<string>::iterator iter; strset.insert("apple"); strset.insert("orange"); strset

std::vector::assign - reallocates the data?

China☆狼群 提交于 2020-01-14 10:16:07
问题 I am working with STL library and my goal is to minimize the data reallocation cases. I was wndering, does std::vector::assign(size_type n, const value_type& val) reallocated the data if the size is not changed or does is actually just assign the new values (for example, using operator=) ? The STL documentation at http://www.cplusplus.com/ sais the following (C++98): In the fill version (2), the new contents are n elements, each initialized to a copy of val. If a reallocation happens,the

C++ does begin/end/rbegin/rend execute in constant time for std::set, std::map, etc?

雨燕双飞 提交于 2020-01-14 10:09:47
问题 For data types such as std::set and std::map where lookup occurs in logarithmic time, is the implementation required to maintain the begin and end iterators? Does accessing begin and end imply a lookup that could occur in logarithmic time? I have always assumed that begin and end always occur in constant time, however I can't find any confirmation of this in Josuttis. Now that I'm working on something where I need to be anal about performance, I want to make sure to cover my bases. Thanks 回答1

C++ does begin/end/rbegin/rend execute in constant time for std::set, std::map, etc?

喜夏-厌秋 提交于 2020-01-14 10:09:31
问题 For data types such as std::set and std::map where lookup occurs in logarithmic time, is the implementation required to maintain the begin and end iterators? Does accessing begin and end imply a lookup that could occur in logarithmic time? I have always assumed that begin and end always occur in constant time, however I can't find any confirmation of this in Josuttis. Now that I'm working on something where I need to be anal about performance, I want to make sure to cover my bases. Thanks 回答1

Why do STL-Datastructures need fully defined types

我只是一个虾纸丫 提交于 2020-01-14 09:05:30
问题 When looking for a solution to this question, I found the this thread on another forum, which says that the standard requires all template parameters to STL-Datastructure to be fully defined. This means producing a structure which stores elements of the it's own type within itself invokes undefined behavior. However as far as I can tell this is not caught for most pre-C++11 datastructure (i.e. std::vector , std::map etc.). What actually could be the problem of using incomplete types within