stl

Does std::sort check if a vector is already sorted?

廉价感情. 提交于 2019-12-29 11:47:22
问题 I believe that the C++ standard for std::sort does not guarantee O(n) performance on a list that's already sorted. But still, I'm wondering whether to your knowledge any implementations of the STL (GCC, MSVC, etc) make the std::is_sorted check before executing the sort algorithm? Asked another way, what performance can one expect (without guarantees, of course) from running std::sort on a sorted container? Side note: I posted some benchmarks for GCC 4.5 with C++0x enabled on my blog. Here's

Passing a parameter to a comparison function?

倾然丶 夕夏残阳落幕 提交于 2019-12-29 11:35:12
问题 When using the STL sort algorithm on a vector, I want to pass in my own comparison function which also takes a parameter. For example, ideally I want to do a local function declaration like: int main() { vector<int> v(100); // initialize v with some random values int paramA = 4; bool comp(int i, int j) { // logic uses paramA in some way... } sort(v.begin(), v.end(), comp); } However, the compiler complains about that. When I try something like: int main() { vector<int> v(100); // initialize v

ifstream:: What is the maximum file size that a ifstream can read

一个人想着一个人 提交于 2019-12-29 07:43:15
问题 I tried to read a 3GB data file using ifstream and it gives me wrong file size, whereas when I read a 600MB file, it gives me the correct result. In addition to wrong file size, I am also unable to read the entire file using ifstream. Here is the code that I used std::wstring name; name.assign(fileName.begin(), fileName.end()); __stat64 buf; if (_wstat64(name.c_str(), &buf) != 0) std::cout << -1; // error, could use errno to find out more std::cout << " Windows file size : " << buf.st_size <<

Compilation problems with vector<auto_ptr<> >

你说的曾经没有我的故事 提交于 2019-12-29 07:40:07
问题 Consider the following code: #include <iostream> #include <memory> #include <vector> using namespace std; struct A { int a; A(int a_):a(a_) {} }; int main() { vector<auto_ptr<A> > as; for (int i = 0; i < 10; i++) { auto_ptr<A> a(new A(i)); as.push_back(a); } for (vector<auto_ptr<A> >::iterator it = as.begin(); it != as.end(); ++it) cout << (*it)->a << endl; } When trying to compile it, I get the following obscure compiler error from g++: g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF

what is the preferred way to expose custom STL-style iteration?

倾然丶 夕夏残阳落幕 提交于 2019-12-29 07:35:22
问题 (also see Is there a good way not to hand-write all twelve required Container functions for a custom type in C++? ) For a class such as namespace JDanielSmith { class C { const size_t _size; const std::unique_ptr<int[]> _data; public: C(size_t size) : _size(size), _data(new int[size]) {} inline const int* get() const noexcept { return _data.get(); } inline int* get() noexcept { return _data.get(); } size_t size() const noexcept { return _size; } }; } what is the preferred way to expose

map::lower_bound() equivalent for python's dict class?

喜你入骨 提交于 2019-12-29 07:35:15
问题 I am writing some code that requires me to fetch the lower bound of a key (for simplicity, ignore keys that lie below the smallest key in the collection). In C++, using std::map (as the most comparable data type) I would simply use the lower_bound() to return the iterator. My Pythonfoo is not that great, but I am guessing that (in case Python does not already have a way of doing this), this would be a good use of a lambda function ... What is the Pythonic way of retrieving the lower bound key

creating a map from two vectors

只愿长相守 提交于 2019-12-29 06:56:13
问题 If I have two stl vectors vect1, vect2 and I want to produce from them a map, so first element from vect1 will correspond to first element in vect2 and so on. How can I do that in the most simple way? 回答1: std::vector<int> a, b; // fill vectors here... std::map<int, int> m; assert(a.size() == b.size()); for (size_t i = 0; i < a.size(); ++i) m[a[i]] = b[i]; 回答2: Here is a solution that uses standard library functions (and C++0x lambdas). const int data1[] = { 0, 2, 4, 6, 8 }; const int data2[]

creating a map from two vectors

允我心安 提交于 2019-12-29 06:56:08
问题 If I have two stl vectors vect1, vect2 and I want to produce from them a map, so first element from vect1 will correspond to first element in vect2 and so on. How can I do that in the most simple way? 回答1: std::vector<int> a, b; // fill vectors here... std::map<int, int> m; assert(a.size() == b.size()); for (size_t i = 0; i < a.size(); ++i) m[a[i]] = b[i]; 回答2: Here is a solution that uses standard library functions (and C++0x lambdas). const int data1[] = { 0, 2, 4, 6, 8 }; const int data2[]

Is there a difference between using .begin() vs .end() for std::inserter for std::set?

半腔热情 提交于 2019-12-29 06:34:07
问题 If there is any difference between it1 and it2? std::set<sometype> s; auto it1 = std::inserter(s, s.begin()); auto it2 = std::inserter(s, s.end()); 回答1: In practice, not much. If you're inserting a large number of already in order elements into an empty set , the second will be somewhat faster, but that's about it. std::insert_iterator calls insert with the iterator; std::set interprets it as a hint, and inserts in constant time (rather than lg n) if the insertion is immediately before the

Does std::multiset guarantee insertion order?

…衆ロ難τιáo~ 提交于 2019-12-29 05:57:30
问题 I have a std::multiset which stores elements of class A . I have provided my own implementation of operator< for this class. My question is if I insert two equivalent objects into this multiset is their order guaranteed? For example, first I insert a object a1 into the set and then I insert an equivalent object a2 into this set. Can I expect the a1 to come before a2 when I iterate through the set? If no, is there any way to achieve this using multiset? 回答1: In C++03 you are not guaranteed