stl

STL iterator: “dereferencing” iterator to a temporary. Is it possible?

情到浓时终转凉″ 提交于 2019-12-22 07:48:33
问题 I'm writing a 3D grid for my scientific software and I need to iterate through the nodes of the grid to get their coordinates. Instead of holding each node object in the container I'd rather like to just calculate the coordinates on the fly while iterating. The problem is that stl::iterator requires to return reference to a value as a result of operator*() , or pointer for operator->() . Some of the code below: class spGridIterator { public: typedef forward_iterator_tag iterator_category;

How do I sort efficiently a quadruple structs in C++?

时间秒杀一切 提交于 2019-12-22 07:06:14
问题 I have a struct with members x,y,z and w. How do I sort efficiently first by x, then by y, by z and finally by w in C++? 回答1: If you want to implement a lexicographical ordering, then the simplest way is to use std::tie to implement a less-than or greater-than comparison operator or functor, and then use std::sort on a collection of your structs. struct Foo { T x, y, z, w; }; .... #include <tuple> // for std::tie bool operator<(const Foo& lhs, const Foo& rhs) { // assumes there is a bool

trying to insert std::pair into std::set

倾然丶 夕夏残阳落幕 提交于 2019-12-22 06:56:07
问题 i can't understand what the error is in this code: #include <set> #include <utility> #include <iostream> using namespace std; class A { public: A(unsigned int a) : _a(a) { } A() : _a(0) { } unsigned int a() const { return _a; } private: unsigned int _a; }; class B { public: B(unsigned int b) : _b(b) { } B() : _b(0) { } unsigned int b() const { return _b; } private: unsigned int _b; }; void display(const Point& point) { //cout << "A: " << point.first.a() << ", B: " << point.second.b() << endl;

Does any stl::set implementation not use a red-black tree?

坚强是说给别人听的谎言 提交于 2019-12-22 06:45:13
问题 Has anyone seen an implementation of the STL where stl::set is not implemented as a red-black tree? The reason I ask is that, in my experiments, B-2B trees outperform stl::set (and other red-black tree implementations) by a factor of 2 to 4 depending on the value of B. I'm curious if there is a compelling reason to use red-black trees when there appear to be faster data structures available. 回答1: Some folks over at Google actually built a B-tree based implementation of the C++ standard

C++ Linked list behavior

江枫思渺然 提交于 2019-12-22 05:53:25
问题 I have some C code, where in there are two linked lists(say A and B) and A is inserted at a particular position into B and A still has elements. How do I simulate the same behavior effectively using the C++ STL? If I try splice, it makes the second one empty. Thanks, Gokul. 回答1: You need to copy the elements. Consider something like this: std::copy(a.begin(), a.end(), std::inserter(b, b_iterator)); If you want the same nodes shared by two lists, this is simply not supported by std::list (STL

C++ concisely checking if item in STL container (e.g. vector)

≡放荡痞女 提交于 2019-12-22 05:49:16
问题 bool xInItems = std::find(items.begin(), items.end(), x) != items.end(); Is there a more concise way of checking if x is in items? This seems unnecessarily verbose (repeating items three times), which makes the intent of the code a little harder to read. For example, is there something like the following: bool xInItems = boost::contains(items, x); If there doesn't exist any more concise boost/stl algorithm to check if a collection contains an item, is it considered good or bad practice to use

Why vector.push_back(auto_ptr) wouldn't compile?

混江龙づ霸主 提交于 2019-12-22 05:43:15
问题 I learned that STL can forbid programmer putting an auto_ptr into a container. For example following code wouldn't compile: auto_ptr<int> a(new int(10)); vector<auto_ptr<int> > v; v.push_back(a); auto_ptr has the copy constructor, why this code can even compile? 回答1: Looking at the definition of std::auto_ptr: namespace std { template <class Y> struct auto_ptr_ref {}; template <class X> class auto_ptr { public: typedef X element_type; // 20.4.5.1 construct/copy/destroy: explicit auto_ptr(X* p

Reading large strings in C++ — is there a safe fast way?

时光怂恿深爱的人放手 提交于 2019-12-22 05:27:09
问题 http://insanecoding.blogspot.co.uk/2011/11/how-to-read-in-file-in-c.html reviews a number of ways of reading an entire file into a string in C++. The key code for the fastest option looks like this: std::string contents; in.seekg(0, std::ios::end); contents.resize(in.tellg()); in.seekg(0, std::ios::beg); in.read(&contents[0], contents.size()); Unfortunately, this is not safe as it relies on the string being implemented in a particular way. If, for example, the implementation was sharing

computing column sums of matrix vector<vector<double> > with iterators?

﹥>﹥吖頭↗ 提交于 2019-12-22 05:25:23
问题 In a previous post column vector with row means -- with std::accumulate? I asked if it was possible, using STL functionality, to compute row means of a matrix vector< vector<double> > data ( rows, vector<double> ( columns ) ); The top answer by @benjaminlindley is not only just what I was looking for, it is a thing of beauty. Forever hopeful I thought it would be as easy to compute column means, so an STL equivalent of vector<double> colmeans( data[0].size() ); for ( int i=0; i<data.size(); i

C++ STL - How does the third argument of the STL sort() work?

随声附和 提交于 2019-12-22 05:23:23
问题 I wish to sort an array of objects of class Person on the basis of its data member ' age '. I store the objects in a vector<Person> v . As far I have understood, there can be at least 4 ways to perform this action and I have the following questions based on the methods written below. How does operator() defined inside a class work? Should I not overload the '<' operator here as well? Why '()' ? I sent an object as the 3rd parameter in method 1. But, in method 2, I sent the name of a function.