Questions tagged [std-pair]

A std::pair is an ordered, heterogeneous sequence of exactly two objects (it is a special case of std::tuple).

std::pair is a part of the C++ Standard Library, defined in the header <utility>.

Reference: https://en.cppreference.com/w/cpp/utility/pair

776 questions
-2
votes
2 answers

Comparator for vector>

vector > v; for(i=0;i<5;i++){ cin>>b>>c; v.push_back(make_pair(b,c)); } sort(v.begin(),v.end()); Is it possible to write a comparator for the sort function such that v[i].first is sorted in increasing order and for similar…
Shivam
  • 31
  • 1
  • 1
  • 5
-2
votes
1 answer

Function with map parameter needs additional arguments?

I have this piece of code: std::map< int, std::pair > m; for ( std::vector::const_iterator passIt = it->GetPasses().begin(); passIt != it->GetPasses().end(); ++passIt ) { m.insert( std::make_pair((*passIt)->GetType(),…
Innkeeper
  • 663
  • 1
  • 10
  • 22
-2
votes
1 answer

std::pair iterator without default constructor

I have an std::map mapping to objects which don't have default constructors. I need an iterator to iterate over the map, but the iterator needs a default constructor, how do I solve this? std::map MyMap; //No default constructor…
cynric4sure
  • 189
  • 1
  • 12
-3
votes
2 answers

How do I compare an element of array in c/c++?

int arr[] = {10, 20, 20, 10, 10, 30, 50, 10, 20}; I want to compare each element in an array so that I can make pairs of similar numbers using C/C++. In above example there are three pairs (10-10, 20-20, 10-10). I want to find number of pairs in…
user13183713
  • 25
  • 1
  • 2
-3
votes
1 answer

C++ dynamic vector of pairs

I need to dynamically allocate an array of 5 vectors of pairs. This code snippet is supposed to add first elements to all 5 vectors: std::vector> * arr = new std::vector>[5]; for (int i = 0; i < 5; i++) { …
user2376997
  • 501
  • 6
  • 22
-3
votes
1 answer

C++ count function in maps with std::pair

I'm trying to code a function which has an unsigned int as output, and two unsigned ints as inputs. Now since this function has been defined recursively, i was trying to implement memoization using std::map to increase the time…
-3
votes
1 answer

Returning pair from a function in C++?

I am a beginner in C++ and I faced the following problem: In my program I have a function to which I pass an array with fixed size as a parameter. I iterate through it and perform some operations. As a result I have 2 variables - br and a, which I…
A.Petrov
  • 33
  • 1
  • 6
-3
votes
1 answer

Evaluating arguments in order when constructing std::pair

I'm trying to construct a pair, from two values read from a stream. These values need to be read in the correct order (T1, then T2), but I believe the order of argument evaluation is undefined in something like the following: std::pair
user673679
  • 1,327
  • 1
  • 16
  • 35
-3
votes
1 answer

vector of pair initialization

Can someone please help me understand how this piece of code works? Pair a[] = {{5, 29}, {39, 40}, {15, 28}, {27, 40}, {50, 90}}; int n = sizeof(a)/sizeof(a[0]); vector arr(a, a + n); (Pair is a structure that has two integers a and b) From…
-3
votes
2 answers

'struct std::pair' has no member named 'push_back'

I made a vector of pairs and want to initialize values to those pairs using make pair but I get this error: 'struct std::pair' has no member named 'push_back' Here is my code: const int maxm=100005;//10^5 vector >…
kolaveri
  • 59
  • 1
  • 2
  • 15
-3
votes
2 answers

Sort a vector of pairs by first element then by second element of the pair in C++?

If I have a vector > datatype, what is the accepted way to sort it by the first element of the pair and then by second if the firsts are equal? For instance maybe (1,10), (3,3), (7,13), (7,16), (8,1), (8,2), (15,2) etc.
MyNameIsKhan
  • 2,594
  • 8
  • 36
  • 56
-4
votes
2 answers

Difference in erasing an element from a vector and a vector of pairs

#include using namespace std; int main() { vectorp; p.push_back(30); p.push_back(60); p.push_back(20); p.erase(p.end()); for(int i = 0; i < p.size(); ++i) cout<
-4
votes
1 answer

Adding pairs to a vector C++

So, I'm attempting to add pairs to a vector, but they must successfully pass 2 criteria: Their weight or 2nd value is greater than or equal to zero. and throws a string error if it does. The vector must not already contain the key or the first…
Granzo
  • 1
  • 3
-4
votes
1 answer

'make_pair' cannot be used as a function

This is my complete code: #include #include #include using namespace std; #define ff first #define mp make_pair #define ss second int main(void) { int m; vector grid; cin >> m; …
MrRobot9
  • 2,402
  • 4
  • 31
  • 68
-4
votes
2 answers

Why we cannot declare vector< int, pair< int, int > >

In a certain competitive coding, i need to use a data-structure mentioned above but it gave me an error, why it threw an error and which is the simplest data-structure i can use for this
user3111412
  • 177
  • 1
  • 3
  • 11
1 2 3
51
52