c++-standard-library

Does C++ standard library provide more compact and generalized version of the erase–remove idiom?

你说的曾经没有我的故事 提交于 2019-11-29 17:16:25
问题 We can erase one element/ entry from a container by the popular erase–remove idiom. However, many of us would have encountered some problems while applying this idiom: one can easily get into the pitfall of typos like c.erase(std::remove_if(c.begin(), c.end(), pred)); // , c.end() //---> missing here or c.erase((std::remove_if(c.begin(), c.end(), pred), c.end())) // ^^ ^^ // extra () makes it pass only c.end() to the c.erase It even follows the wrong semantics for containers like std::list by

`std::pair` `second` has incomplete type with `unordered_map` tree

不羁岁月 提交于 2019-11-29 15:07:34
I was reviewing some older code of mine and I saw the code using pointers to implement a tree of Variant objects. It is a tree because each Variant can contain an unordered_map of Variant* . I looked at the code and wondered why isn't it just using values, a std::vector<Variant> , and std::unordered_map<std::string, Variant> , instead of Variant* . So I went ahead and changed it. It seemed okay except one thing, I got errors : /usr/local/include/c++/6.1.0/bits/stl_pair.h:153:11: error: 'std::pair<_T1, _T2>::second' has incomplete type _T2 second; /// @c second is a copy of the second object ^~

Get index by type in std::variant

北慕城南 提交于 2019-11-29 09:48:07
Is there a utility in the standard library to get the index of a given type in std::variant ? Or should I make one for myself? That is, I want to get the index of B in std::variant<A, B, C> and have that return 1 . There is std::variant_alternative for the opposite operation. Of course, there could be many same types on std::variant 's list, so this operation is not a bijection, but it isn't a problem for me (I can have first occurrence of type on list, or unique types on std::variant list). We could take advantage of the fact that index() almost already does the right thing. We can't

Implementing a “string pool” that is guaranteed not to move

落爺英雄遲暮 提交于 2019-11-29 06:41:17
I need a "string pool" object into which I can repeatedly insert a "sequence of chars" (I use this phrase to mean "string" without confusing it with std::string or a C string), obtain a pointer to the sequence, and be guaranteed that the pointer will not become invalidated if/when the pool needs to grow. Using a simple std::string as the pool won't work, because of the possibility for the string to be reallocated when it outgrows its initial capacity, thus invalidating all previous pointers into it. The pool will not grow without bound -- there are well-defined points at which I will call a

What's the difference between input iterators and read-only forward iterators?

可紊 提交于 2019-11-29 06:06:32
What's the difference between input iterators and read-only forward iterators? Because the latter are read-only, they obviously don't satisfy requirements of output iterators. And, because of that, they're effectively input iterators with additional guarantees (if any). The problem is, what additional guarantees? My guess would be that forward iterators are multi-pass and input iterators are not, am I right? Yes, input iterators are one-pass iterators. You can only iterate over them once, while forward iterators are multi-pass. From §24.2.3 [input.iterators] p2 (the table) , pre-/postcondition

Is using std::vector< std::shared_ptr<const T> > an antipattern?

你。 提交于 2019-11-29 05:36:18
问题 For a long time I was using std::vector and std::shared_ptr hand in hand. Recently I started using std::shared_ptr<const T> whenever a pointer to a const object was needed. This is all OK, since std::shared_ptr<T> can be cast to std::shared_ptr<const T> and then they share the same reference counter and everything feels natural. But when I try to use constructs such as std::vector< std::shared_ptr<const T> > I run into troubles. To simplify I will denote the two structures: template <class T>

Are there any STL headers which are not part of the C++ Standard Library?

眉间皱痕 提交于 2019-11-29 02:14:39
问题 I know that some C++ Standard Library headers are originated from the STL, such as vector . But I'm failing to find an up-do-date list of STL headers which are still not incorporated by the Standard Library. Do they exist? PS: I would like to have them listed, and also to know if all major implementations include them or where to get them, if possible. 回答1: Note, this is a function by function break down, rather than a by header breakdown, because it seems to be more useful. If we examine SGI

std::stoi missing in g++ 4.7.2?

对着背影说爱祢 提交于 2019-11-29 01:56:56
I get the error message "stoi is not a member of std" when I try to use std::stoi and try to compile it. I'm using g++ 4.7.2 from the command line so it can't be IDE error, I have all my includes in order, and g++4.7.2 defaults to using c++11. If it helps, my OS is Ubuntu 12.10. Is there something I haven't configured? #include <iostream> #include <string> using namespace std; int main(){ string theAnswer = "42"; int ans = std::stoi(theAnswer, 0, 10); cout << "The answer to everything is " << ans << endl; } Will not compile. But there's nothing wrong with it. std::stoi() is new in C++11 so you

Why is std::hash a struct instead of a function?

一曲冷凌霜 提交于 2019-11-28 22:45:52
问题 Standard library implements std::hash as a template struct that is specialized for different types. It is used like this: #include <iostream> #include <functional> int main() { std::hash<int> hasher; std::cout << hasher(1337) << std::endl; return 0; } My question is what is the reasoning behind this design choice. Why it isn't implemented as a template function and used like this: #include <iostream> #include <functional> int main() { std::cout << std::hash<int>(1337) << std::endl; return 0;

What should I use instead of sscanf?

旧巷老猫 提交于 2019-11-28 21:11:04
I have a problem that sscanf solves (extracting things from a string). I don't like sscanf though since it's not type-safe and is old and horrible. I want to be clever and use some more modern parts of the C++ standard library. What should I use instead? Try std::stringstream : #include <sstream> ... std::stringstream s("123 456 789"); int a, b, c; s >> a >> b >> c; For most jobs standard streams do the job perfectly, std::string data = "AraK 22 4.0"; std::stringstream convertor(data); std::string name; int age; double gpa; convertor >> name >> age >> gpa; if(convertor.fail() == true) { // if