std-pair

How to chain delete pairs from a vector in C++?

穿精又带淫゛_ 提交于 2020-01-04 14:03:55
问题 I have this text file where I am reading each line into a std::vector<std::pair> , handgun bullets bullets ore bombs ore turret bullets The first item depends on the second item. And I am writing a delete function where, when the user inputs an item name, it deletes the pair containing the item as second item. Since there is a dependency relationship, the item depending on the deleted item should also be deleted since it is no longer usable. For example, if I delete ore , bullets and bombs

Initializer list inside std::pair

£可爱£侵袭症+ 提交于 2020-01-01 01:13:11
问题 This code: #include <iostream> #include <string> std::pair<std::initializer_list<std::string>, int> groups{ { "A", "B" }, 0 }; int main() { for (const auto& i : groups.first) { std::cout << i << '\n'; } return 0; } compiles but returns segfault. Why? Tested on gcc 8.3.0 and on online compilers. 回答1: std::initializer_list is not meant to be stored, it is just meant for ... well initialization. Internally it just stores a pointer to the first element and the size. In your code the std::string

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

六月ゝ 毕业季﹏ 提交于 2019-12-31 01:45:28
问题 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

What is C# analog of C++ std::pair?

和自甴很熟 提交于 2019-12-28 01:41:33
问题 I'm interested: What is C#'s analog of std::pair in C++? I found System.Web.UI.Pair class, but I'd prefer something template-based. Thank you! 回答1: Tuples are available since .NET4.0 and support generics: Tuple<string, int> t = new Tuple<string, int>("Hello", 4); In previous versions you can use System.Collections.Generic.KeyValuePair<K, V> or a solution like the following: public class Pair<T, U> { public Pair() { } public Pair(T first, U second) { this.First = first; this.Second = second; }

C++ display map that has vector

馋奶兔 提交于 2019-12-25 07:59:00
问题 std::map<std::string, std::vector<string>> data; In order to print out this by using copy , how should my std::ostream_iterator be? Apparently std::ostream_iterator<std::pair<std::string, std::vector<std::string>>> out_it(std::cout, "\n"); did not make it. My operator<< overload is the following std::ostream& operator<<(std::ostream& out, const std::pair<std::string, std::vector<std::string>>& p) and it writes out the p.first and p.second and returns it. 回答1: So here is a operator<< that will

How to decrease key for a particular edge in a priority_queue<PI, vector<PI> ,greater<PI> >,trying to implement prim's algorithm?

送分小仙女□ 提交于 2019-12-25 07:18:58
问题 #include <bits/stdc++.h> using namespace std; #define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define LL long long int #define pb push_back #define mp make_pair #define PI pair<int,int> #define PL pair<LL,LL> #define PIS pair< int,string> #define test int t;cin>>t;while(t--) #define ff first #define ss second #define INF 1000000000 #define input(a,n) for(i=1;i<=n;i++)cin>>a[i]; #define output(a,n) for(i=1;i<=n;i++)cout<<a[i]<<" "; vector< vector<LL> >v(3002, vector<LL>(3002,

C++ Erasing from list of pairs

放肆的年华 提交于 2019-12-25 06:58:46
问题 Very simple: I have the following code and the method erase is not working. I do not see any problem there because if I go to http://www.cplusplus.com/reference/list/list/erase/ , syntax is: iterator erase (iterator position); list<pair<string,int>> l0 { { "name1", 20 }, { "name2", 30 }, { "name3", 40 } }; for( auto &it : l0 ) l0 . erase( it ); May there be a problem that there is a list of pair<string,int> and not a list of a basic data types? EDIT: The problem is that the code is not

Priority queue changes its content

爷,独闯天下 提交于 2019-12-24 07:59:24
问题 I have been trying to create a priority queue in which every element is a pair that stores a pointer to an unsigned int and an unsigned int . The problem is that whenever I add a pair to the priority queue, the element that the previously added pair was pointing to, switches its value to 0. Here is the code #include <iostream> #include <vector> #include <utility> #include <queue> typedef unsigned int ui; typedef std::pair<ui*, ui> Ppuiui; typedef std::priority_queue<Ppuiui> Queue; void showPQ

std::make_pair with float array (float2, unsigned int)

醉酒当歌 提交于 2019-12-24 00:46:08
问题 I have a vector templated with a 'float2, unsigned int'-pair like: std::vector<std::pair<float2, unsigned int>> myVec; And then I'm trying to add such a pair to the vector: unsigned int j = 0; float2 ab = {1.0, 2.0}; myVec.push_back(std::make_pair(ab, j)); This is how I expect it should work, though when I try to compile it I get the error: 1>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\utility(163): error C2536: 'std::_Pair_base<_Ty1,_Ty2>::std::_Pair_base<_Ty1,_Ty2>::first

C++ Error Handling - downside of using std::pair or std::tuple for returning error codes and function returns [closed]

和自甴很熟 提交于 2019-12-23 09:17:42
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . Without getting into the general exceptions vs error codes discussion, what do you think are the downsides of using std::pair or std