std-pair

Best way to erase vector of ranges from std::vector

◇◆丶佛笑我妖孽 提交于 2019-12-23 01:07:05
问题 In one of my projects it is necessary to remove certain elements from a std::vector<double> values . The indices I have to remove are given as a vector of intervals. For example {1,3} means, that I have to remove indices from 1 to 3 inclusive from values . I can assume that the intervals given are mutually exclusive. The code shown below illustrates, what the desired behavior should look like. #include <iostream> #include <vector> int main(int argc, char** args) { // Intervals of indices I

How do I create a set with std::pair thats sorted based on the ::second pair member using bind

北城余情 提交于 2019-12-22 08:28:55
问题 I know I could use the following: template <typename Pair> struct ComparePairThroughSecond : public std::unary_function<Pair, bool> { bool operator ()(const Pair& p1, const Pair& p2) const { return p1.second < p2.second; } }; std::set<std::pair<int, long>, ComparePairThroughSecond> somevar; but wondered if it could be done with boost::bind 回答1: How about the following one. I'm using boost::function to 'erase' the actual type of the comparator. The comparator is created using boost:bind itself

use of emplace(args&& …) in associative containers

风格不统一 提交于 2019-12-21 18:02:15
问题 I am trying to forward some arguments to do inplace construction of objects . I don't quite get the rationale behind the usage of emplace in associative containers or may be I am just using/thinking in a wrong way. It would be great if someone can share code snippets for usage. Associative container like map always store an object of kind pair() , and the emplace function says that it will call the constructor of the object stored (which for is always pair in case of maps) by forwarding the

How to std::hash an unordered std::pair

巧了我就是萌 提交于 2019-12-21 11:55:50
问题 I want to be able to use a std::pair as a key in an unordered_container. I know that I could do this the following way: template<typename T> void hash_combine(std::size_t &seed, T const &key) { std::hash<T> hasher; seed ^= hasher(key) + 0x9e3779b9 + (seed << 6) + (seed >> 2); } namespace std { template<typename T1, typename T2> struct hash<std::pair<T1, T2>> { std::size_t operator()(std::pair<T1, T2> const &p) const { std::size_t seed(0); ::hash_combine(seed, p.first); ::hash_combine(seed, p

How to std::hash an unordered std::pair

隐身守侯 提交于 2019-12-21 11:55:10
问题 I want to be able to use a std::pair as a key in an unordered_container. I know that I could do this the following way: template<typename T> void hash_combine(std::size_t &seed, T const &key) { std::hash<T> hasher; seed ^= hasher(key) + 0x9e3779b9 + (seed << 6) + (seed >> 2); } namespace std { template<typename T1, typename T2> struct hash<std::pair<T1, T2>> { std::size_t operator()(std::pair<T1, T2> const &p) const { std::size_t seed(0); ::hash_combine(seed, p.first); ::hash_combine(seed, p

STL map insertion efficiency: [] vs. insert

試著忘記壹切 提交于 2019-12-21 03:26:08
问题 There are two ways of map insertion: m[key] = val; Or m.insert(make_pair(key, val)); My question is, which operation is faster? People usually say the first one is slower, because the STL Standard at first 'insert' a default element if 'key' is not existing in map and then assign 'val' to the default element. But I don't see the second way is better because of 'make_pair'. make_pair actually is a convenient way to make 'pair' compared to pair<T1, T2>(key, val) . Anyway, both of them do two

struct with 2 cells vs std::pair? [duplicate]

点点圈 提交于 2019-12-20 16:53:26
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: What is the difference between using a struct with two fields and a pair? Dear all, I have a little question about pairs and struct. Is there any advantage to use a std::pair instead of a struct with two cells ? I have used pairs for a while but the main problem is readability : If you want to represent for example a duple (int "label", double "value") you can use either a : typedef std::pair<int,double> myElem;

Comparing two map::iterators: why does it need the copy constructor of std::pair?

这一生的挚爱 提交于 2019-12-20 10:28:42
问题 The very simple code below compiles and links without a warning in C++98 but gives an incomprehensible compile error in C++11 mode. #include <map> struct A { A(A& ); // <-- const missing }; int main() { std::map<int, A> m; return m.begin() == m.end(); // line 9 } The error with -std=c++11 is, gcc version 4.9.0 20140302 (experimental) (GCC): ali@X230:~/tmp$ ~/gcc/install/bin/g++ -std=c++11 cctor.cpp In file included from /home/ali/gcc/install/include/c++/4.9.0/bits/stl_algobase.h:64:0, from

Forward declaration of objects with STL containers

为君一笑 提交于 2019-12-19 17:48:07
问题 Consider the following code snippet, where the first line serves only as forward declaration class A; followed by defining new class class B { vector<A> Av; //line 1 map<int, A> Am; //line 2 pair<int, A> Ap; //line 3 }; line 1 and line 2 seems to be fine with the forward declaration (which may tell me that those container use pointer type of implementation) where as line 3 does not seem to compile on VS2012. My question is that behavior dictated by the standard or specific to the compiler I

Why can't you assign a pair from a tuple, but tuple can be assigned from a pair?

♀尐吖头ヾ 提交于 2019-12-19 16:47:41
问题 I'm not clear why it is legal to assign tuple<X,Y>=pair<X,Y> But it is illegal to assign pair<X,Y>=tuple<X,Y> std::pair<int, double> x { 1 , 5.5}; std::tuple<int, double> y { 1 , 5.5}; int a; double b; std::tie(a,b) = x; std::tie(a,b) = y; x = y; // THIS LINE (line 12) y = x; // but this is fine ??? Shouldn't this be symmetrical? Using g++ 4.8.1 gives the following errors: tp.cpp:12:4: error: no match for operator= (operand types are std::pair<int, double> and std::tuple<int, double>) x = y;