stl

what is a domain error

我的梦境 提交于 2020-01-01 02:17:33
问题 in c++, <stdexcept> has a base class for 'domain errors', std::domain_error. i don't understand under what circumstances i should throw a domain error in my code. all of the other exception base classes are pretty self explanatory. i'm pretty sure that std::domain_error has nothing to do with internet domain names, per se, so please explain what class of error a domain error is and provide some examples. 回答1: Domain and range errors are both used when dealing with mathematical functions. On

How to use find algorithm with a vector of pointers to objects in c++?

冷暖自知 提交于 2020-01-01 02:11:05
问题 I want to find in a vector of Object pointers for a matching object. Here's a sample code to illustrate my problem: class A { public: A(string a):_a(a) {} bool operator==(const A& p) { return p._a == _a; } private: string _a; }; vector<A*> va; va.push_back(new A("one")); va.push_back(new A("two")); va.push_back(new A("three")); find(va.begin(), va.end(), new A("two")); I want to find the second item pushed into the vector. But since vector is defined as a pointers collection, C++ does not use

resize versus push_back in std::vector : does it avoid an unnecessary copy assignment?

旧巷老猫 提交于 2020-01-01 01:15:31
问题 When invoking the method push_back from std::vector , its size is incremented by one, implying in the creation of a new instance, and then the parameter you pass will be copied into this recently created element, right? Example: myVector.push_back(MyVectorElement()); Well then, if I want to increase the size of the vector with an element simply using its default values, wouldn't it be better to use the resize method instead? I mean like this: myVector.resize(myVector.size() + 1); As far as I

C++ STL map I don't want it to sort!

拟墨画扇 提交于 2020-01-01 01:13:19
问题 This is my code map<string,int> persons; persons["B"] = 123; persons["A"] = 321; for(map<string,int>::iterator i = persons.begin(); i!=persons.end(); ++i) { cout<< (*i).first << ":"<<(*i).second<<endl; } Expected output: B:123 A:321 But output it gives is: A:321 B:123 I want it to maintain the order in which keys and values were inserted in the map<string,int> . Is it possible? Or should I use some other STL data structure? Which one? 回答1: There is no standard container that does directly

Comparators in STL

て烟熏妆下的殇ゞ 提交于 2019-12-31 22:47:43
问题 I am using struct minHeap to generate a min heap using priority_queue .And function comp to print numbers in reverse order using sort function given in STL . Now my doubt is that I can not use struct minHeap in function sort and can not use function comp in priorityQueue . I feel that function of both struct minHeap and comp is similar. Please explain me when to use structs for comaprator and when to use normal functions to behave as comparators in STL ? #include<iostream> #include <queue>

Chaining of ordering predicates (e.g. for std::sort)

南笙酒味 提交于 2019-12-31 22:37:10
问题 You can pass a function pointer, function object (or boost lambda) to std::sort to define a strict weak ordering of the elements of the container you want sorted. However, sometimes (enough that I've hit this several times), you want to be able to chain "primitive" comparisons. A trivial example would be if you were sorting a collection of objects that represent contact data. Sometimes you will want to sort by last name, first name, area code . Other times first name, last name - yet other

using of std::accumulate

╄→гoц情女王★ 提交于 2019-12-31 16:11:28
问题 Need prettier solution of below example but with std::accumulate. #include <algorithm> #include <vector> #include <iostream> class Object { public: Object( double a, double b ): a_( a ), b_( b ) {} double GetA() const { return a_; } double GetB() const { return b_; } // other methods private: double a_; double b_; }; class Calculator { public: Calculator( double& result ): result_( result ) {} void operator() ( const Object& object ) { // some formula result_ += object.GetA() * object.GetB();

黑马程序员C++教程从0到1入门--STL标准库

丶灬走出姿态 提交于 2019-12-31 10:59:13
黑马程序员C++教程从0到1入门--STL标准库 STL 组件 STL基本结构 STL 组件 STL 是 C++ 标准程序库的核心。STL 内的所有组件都由模板构成,其元素可以是任意型别。程序员通过选用恰当的群集类别调用其成员函数和算法中的数据即可。 STL 组件主要包括容器,迭代器、算法和仿函数。 容器 容器即用来存储并管理某类对象的集合。例如鱼缸是用来盛放金鱼的容器。 在 STL 中,容器又分为序列式容器和关联式容器两大类,而迭代器的功能主要是遍历容器内全部或部分元素的对象。迭代器可划分为 5 种类属,这 5 种类属归属两种类型:双向迭代器和随机存取迭代器。 每一种容器都有其优点和缺点。为满足程序的各种需求,STL 准备了多种容器类型,容器可以是 arrays 或是 linked lists,或者每个元素有特别的键值。 迭代器 迭代器用于在一个对象群集的元素上进行遍历动作。对象群集可能是容器,也可能是容器的一部分。 迭代器的主要用途是为容器提供一组很小的公共接口。利用这个接口,某项操作可以行进至群集内的下一个元素。 每种容器都提供了各自的迭代器。迭代器了解该容器的内部结构,所以能够正确行进。迭代器的接口和一般指针类似。 算法 算法用来处理群集内的元素,可以出于不同目的搜寻、排序、修改、使用那些元素。所有容器的迭代器都提供一致的接口,通过迭代器的协助,算法程序可以用于任意容器。

How is nth_element Implemented?

送分小仙女□ 提交于 2019-12-31 10:52:46
问题 There are a lot of claims on StackOverflow and elsewhere that nth_element is O(n) and that it is typically implemented with Introselect: http://en.cppreference.com/w/cpp/algorithm/nth_element I want to know how this can be achieved. I looked at Wikipedia's explanation of Introselect and that just left me more confused. How can an algorithm switch between QSort and Median-of-Medians? I found the Introsort paper here: http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.14.5196&rep=rep1

How is nth_element Implemented?

寵の児 提交于 2019-12-31 10:49:17
问题 There are a lot of claims on StackOverflow and elsewhere that nth_element is O(n) and that it is typically implemented with Introselect: http://en.cppreference.com/w/cpp/algorithm/nth_element I want to know how this can be achieved. I looked at Wikipedia's explanation of Introselect and that just left me more confused. How can an algorithm switch between QSort and Median-of-Medians? I found the Introsort paper here: http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.14.5196&rep=rep1