std-pair

Pair inside priority queue

牧云@^-^@ 提交于 2019-12-03 02:25:44
问题 I am trying to store pairs in priority queue and I am using a compare function that compares second value of each pair. #include<iostream> #include<queue> #include<utility> using namespace std; class CompareDist { public: bool operator()(pair<int,int> n1,pair<int,int> n2) { return n1.second>n2.second; } }; int main() { priority_queue<pair<int,int>,CompareDist> pq; } When I compile this I get an error error: no type named ‘value_type’ in ‘class CompareDist’ What could be the reason.I am new to

Adding to a vector of pair

可紊 提交于 2019-12-03 01:36:06
问题 I have a vector of pair like such: vector<pair<string,double>> revenue; I want to add a string and a double from a map like this: revenue[i].first = "string"; revenue[i].second = map[i].second; But since revenue isn't initialized, it comes up with an out of bounds error. So I tried using vector::push_back like this: revenue.push_back("string",map[i].second); But that says cannot take two arguments. So how can I add to this vector of pair ? 回答1: Use std::make_pair: revenue.push_back(std::make

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

China☆狼群 提交于 2019-12-02 22:43:26
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 /home/ali/gcc/install/include/c++/4.9.0/bits/stl_tree.h:61, from /home/ali/gcc/install/include/c++/4.9.0

Using pair as key in a map (C++ / STL)

谁说胖子不能爱 提交于 2019-12-02 21:55:21
I want to use a pair from STL as a key of a map. #include <iostream> #include <map> using namespace std; int main() { typedef pair<char*, int> Key; typedef map< Key , char*> Mapa; Key p1 ("Apple", 45); Key p2 ("Berry", 20); Mapa mapa; mapa.insert(p1, "Manzana"); mapa.insert(p2, "Arandano"); return 0; } But the compiler throw a bunch of unreadable information and I'm very new to C and C++. How can I use a pair as a key in a map? And in general How can I use any kind of structure (objects, structs, etc) as a key in a map? Thanks! std::map::insert takes a single argument: the key-value pair, so

Use of for_each on map elements

拥有回忆 提交于 2019-12-02 17:16:35
I have a map where I'd like to perform a call on every data type object member function. I yet know how to do this on any sequence but, is it possible to do it on an associative container? The closest answer I could find was this: Boost.Bind to access std::map elements in std::for_each . But I cannot use boost in my project so, is there an STL alternative that I'm missing to boost::bind? If not possible, I thought on creating a temporary sequence for pointers to the data objects and then, call for_each on it, something like this: class MyClass { public: void Method() const; } std::map<int,

Pair inside priority queue

被刻印的时光 ゝ 提交于 2019-12-02 15:56:00
I am trying to store pairs in priority queue and I am using a compare function that compares second value of each pair. #include<iostream> #include<queue> #include<utility> using namespace std; class CompareDist { public: bool operator()(pair<int,int> n1,pair<int,int> n2) { return n1.second>n2.second; } }; int main() { priority_queue<pair<int,int>,CompareDist> pq; } When I compile this I get an error error: no type named ‘value_type’ in ‘class CompareDist’ What could be the reason.I am new to STL. This is what priority_queue looks like: template< class T, class Container = std::vector<T>,

Is it possible to “constify” a field of `std::pair` without hacks?

核能气质少年 提交于 2019-12-01 18:39:48
In C++, the compiling the following code: std::pair <int, int> x; static_cast <std::pair <const int, int>*> (&x); gives an error: error: invalid static_cast from type ‘std::pair<int, int>*’ to type ‘std::pair<const int, int>*’ I more or less understand why it happens, as cv-qualifying a type in a template parameter list can, in principle, give an "incompatible" result. And even if in this case it doesn't, compiler has no way to know it. Anyway, is there a non-hackish way to perform this conversion ? I'm wary of using reinterpret_cast for anything as I've been by type-punning problems before.

Forward declaration of objects with STL containers

旧巷老猫 提交于 2019-12-01 17:13:16
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 am using? Thanks The relevant rules for the standard library types are in [res.on.functions]: In

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

走远了吗. 提交于 2019-12-01 16:21:19
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; ^ tp.cpp:12:4: note: candidates are: In file included from /opt/gcc-4.8.1/include/c++/4.8.1/utility:70

Copy std::map into std::vector of pairs

你说的曾经没有我的故事 提交于 2019-12-01 15:46:07
I'm trying to copy a map into a vector of pair, so I can then sort the vector by the second data member of the pairs. I have resolved this doing like this: void mappedWordsListSorter(){ for (auto itr = mappedWordsList.begin(); itr != mappedWordsList.end(); ++itr){ vectorWordsList.push_back(*itr); } sort(vectorWordsList.begin(), vectorWordsList.end(), [=](pair<string, int>& a, pair<string, int>& b){return a.second > b.second;}); } I need to find a way to do this without using a raw loop, using the standard library instead. I have come across a lot of examples doing this by only transferring