stl

C++ STL 介绍

自闭症网瘾萝莉.ら 提交于 2020-01-16 05:12:13
STL是C/C++开发中一个非常重要的模板,而其中定义的各种容器也是非常方便我们大家使用。下面,我们就浅谈某些常用的容器。这里我们不涉及容器的基本操作之类,只是要讨论一下各个容器其各自的特点。STL中的常用容器包括:顺序性容器(vector、deque、list)、关联容器(map、set)、容器适配器(queue、stac)。 1、顺序性容器 (1)vector   vector是一种动态数组,在内存中具有连续的存储空间,支持快速随机访问。由于具有连续的存储空间,所以在插入和删除操作方面,效率比较慢。vector有多个构造函数,默认的构造函数是构造一个初始长度为0的内存空间,且分配的内存空间是以2的倍数动态增长的,即内存空间增长是按照2 0 ,2 1 ,2 2 ,2 3 .....增长的,在push_back的过程中,若发现分配的内存空间不足,则重新分配一段连续的内存空间,其大小是现在连续空间的2倍,再将原先空间中的元素复制到新的空间中,性能消耗比较大,尤其是当元素是非内部数据时(非内部数据往往构造及拷贝构造函数相当复杂)。 vector的另一个常见的问题就是clear操作。clear函数只是把vector的size清为零,但vector中的元素在内存中并没有消除,所以在使用vector的过程中会发现内存消耗会越来越多,导致内存泄露,现在经常用的方法是swap函数来进行解决:

stl

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-15 23:40:02
容器 vector 特点:动态数组 元素类型相同 只能从尾部快速插入或者删除 随机访问快 中间插入删除慢(需要移动) capcity是给的块的大小 定义vector vector<int> A(10, 1); //声明一个初始大小为10且值都是1的向量 vector<int> A(tmp); //声明并用tmp向量初始化vec向量 vector<int> tmp(A.begin(), A.begin() + 3); //用向量vec的第0个到第2个值初始化tmp int arr[5] = {1, 2, 3, 4, 5}; vector<int> A(arr, arr + 5); //将arr数组的元素用于初始化A向量 //说明:当然不包括arr[4]元素,末尾指针都是指结束元素的下一个元素, //这个主要是为了和vec.end()指针统一 vector<int> A(&arr[1], &arr[4]); //将arr[1]~arr[4]范围内的元素作为A的初始值 vector操作 向量大小: A.size(); 向量最大容量: A.max_size();与机器有关 与程序无关 更改向量大小: A.resize(n); n是新的大小 例证capacity、resize、shrink to fit 的关系 vector<int> myvector(100);#myvector

C++: which constructors are called in vector<int> vn{MyAllocator<int>(a)}?

青春壹個敷衍的年華 提交于 2020-01-15 12:16:15
问题 I have a trivial allocator: // alloc.h #include <cstdlib> #include <new> #include <iostream> template <class T> struct Mallocator { typedef T value_type; Mallocator() { std::cout << "default ctor is called" << std::endl; } template <class U> Mallocator(const Mallocator<U>&) { std::cout << "copy ctor is called" << std::endl; } T* allocate(std::size_t n) { std::cout << "Mallocator::allocate(size_t n) is called, n = " << n << " "; if(n > std::size_t(-1) / sizeof(T)) throw std::bad_alloc(); if(T

2D vector vs 1D vector

你说的曾经没有我的故事 提交于 2020-01-15 10:00:49
问题 In C++11, how does a 2D vector against 1D vector in terms of time? In the 2D vector given, all the inner vectors are of the same size. Ex: std::vector<std::vector<int>> X{10, std::vector<int>(4)}; vs std::vector<int> Y(40); Which avatar of the vector would perform better when the elements are randomly accessed? 回答1: A single std::vector is inherently simpler, it's just a contiguous block of memory stored somewhere. A std::vector of std::vector has more overhead but it's also more powerful

C++ Templates - Specifying a container type and that containers element type that it holds

不羁岁月 提交于 2020-01-15 09:32:30
问题 I want to be able to create a function where I specify a parameter to have both a templated container and a templated element type for that container. Is this possible? I get "error C2988: unrecongnizable template declaration/definition" among others. Here is the function in question. template<class Iter, class Elem> void readIntoP(Iter<Elem> aCont){ ifstream ifss("data.dat"); string aString; int counter = 0; item tempItem; while(ifss >> aString){ istringstream iss(aString); if(counter == 0){

Set Visual Studio (conditional) breakpoint on local variable value

允我心安 提交于 2020-01-15 08:32:11
问题 I'm trying to debug a method which among other things, adds items to a list which is local to the method. However, every so often the list size gets set to zero "midstream". I would like to set the debugger to break when the list size becomes zero, but I don't know how to, and would appreciate any pointers on how to do this. Thanks. 回答1: Why not use conditional breakpoints? http://blogs.msdn.com/saraford/archive/2008/06/17/did-you-know-you-can-set-conditional-breakpoints-239.aspx 回答2: in C#

Codeforces-356A(STL运用)

我只是一个虾纸丫 提交于 2020-01-15 07:40:27
Hooray! Berl II, the king of Berland is making a knight tournament. The king has already sent the message to all knights in the kingdom and they in turn agreed to participate in this grand event. As for you, you’re just a simple peasant. There’s no surprise that you slept in this morning and were late for the tournament (it was a weekend, after all). Now you are really curious about the results of the tournament. This time the tournament in Berland went as follows: There are n knights participating in the tournament. Each knight was assigned his unique number — an integer from 1 to n. The

Why does remove_if( …, lambda ) expression require the assignment operator?

橙三吉。 提交于 2020-01-15 07:17:25
问题 I have this code (simplified) : std::vector<Session> sessions; // ... std::remove_if( sessions.begin(), sessions.end(), [] (const Session& s) { return false; } ); When I compile it (in Visual Studio 2013 Update 1) I get the following error: algorithm(1759): error C2280: 'Session &Session::operator =(const Session &)' : attempting to reference a deleted function Session.h(78) : see declaration of 'Session::operator =' Indeed, I have deleted operator= in the Session class like this: Session&

How to overload less operator in a template class for sort algorithm use?

我与影子孤独终老i 提交于 2020-01-15 05:21:34
问题 I have a self-define class using template, like this: template<class T> class foo { public: T a; bool operator<(const foo<T> &f); //other functions... } template<class T> bool foo<T>::operator<(const foo<T> &f) {return a - f.a;} Now, I new some foos and give them value, then I want to sort this array: foo<int>* fp = new foo<int>[3]; //give each element value sort(fp, fp+3); //run-time error When I use sort function, I got a run-time error. Did I do something wrong? Please help me. 回答1:

STL之map容器和multimap容器

时光总嘲笑我的痴心妄想 提交于 2020-01-15 05:18:20
摘要:本文主要介绍了map容器和multimap容器的相关内容。 1、基本概念 1.1 Map的特性 所有元素都会根据元素的键值自动排序。Map所有的元素都是pair,同时拥有实值和键值,pair的第一元素被视为键值,第二元素被视为实值,map不允许两个元素有相同的键值。 1.1 map容器的键值无法修改 map的键值关系到map元素的排列规则,任意改变map键值将会严重破坏map组织。如果想要修改元素的实值,那么是可以的。 Map和list拥有相同的某些性质,当对它的容器元素进行新增操作或者删除操作时,操作之前的所有迭代器,在操作完成之后依然有效,当然被删除的那个元素的迭代器必然是个例外。 1.2 Multimap容器 Multimap和map的操作类似,唯一区别multimap键值可重复。 Map和multimap都是以红黑树为底层实现机制。 2、常用的API API 意义 构造函数 map<T1, T2> mapTT map默认构造函数 map(const map &mp) 拷贝构造函数 赋值操作 map& operator=(const map &mp) 重载等号操作符 swap(mp) 交换两个集合容器 大小操作 size() 返回容器中元素的数目 empty() 判断容器是否为空 插入数据元素操作 map.insert(...); //往容器插入元素,返回pair