user1543957
user1543957

Reputation: 1828

Using pair in C++

Can someone please tell why I am unable to compile the following program:

#include<iostream>
#include<string>
#include<cmath>
#include<iostream>
#include<cfloat>
#define MOD 10000009

using namespace std;

double distance(pair<int,int> p1,pair<int,int> p2)
{
    double dist;
    dist = sqrt( (p1.first-p2.first)*(p1.first-p2.first) + (p1.second-p2.second)*(p1.second-p2.second) );
    return(dist);
}

int main()
{
        int N,i,j;
        cin >> N;
        pair<int,int> pi[N];
        for(i=0;i<N;i++)
        {
            cin >> pi[i].first >> pi[i].second;
        }

        for(i=0;i<N;i++)
        {
            cout << pi[i].first << " "<< pi[i].second << endl;
        }

        distance(pi[0],pi[1]); // This line is giving error

    return 0;
}

enter image description here

Upvotes: 2

Views: 27629

Answers (5)

rashedcs
rashedcs

Reputation: 3725

You have to use reference variable as argument of distance function. The correct code are given below :

#include<bits/stdc++.h>
#define MOD 10000009

using namespace std;

double distance(const pair<int,int> &p1, const pair<int,int> &p2)
{
   double dist;
   dist = sqrt((p1.first-p2.first)*(p1.first-p2.first) + (p1.second-p2.second)*(p1.second-p2.second));
   return dist;
}

int main()
{
        int N,i,j;
        cin >> N;
        pair<int,int> pi[N];
        for(i=0;i<N;i++)
        {
            cin >> pi[i].first >> pi[i].second;
        }

        for(i=0;i<N;i++)
        {
            cout << pi[i].first << " "<< pi[i].second << endl;
        }

        cout<<distance(pi[0],pi[1]); 

      return 0;
}

Upvotes: 0

Ashish Kumar
Ashish Kumar

Reputation: 1

The usage of pair in C++

pair<some_datatype, some_other_datatype> my_pairs;

The above line will make pairs of the first element with respect to the second element, now you can use this pair like this :

pair<int,int> my_pairs;
my_pairs  = make_pair(5,6);
cout<<my_pairs.first<<" "<<my_pairs.second<<endl;

Now say you want to create a list (or array or vector) of “n" pairs, to do that we can use the vector data structure like this :

typedef pair<some_datatype,some_other_datatype> my_pairs;
vector<my_pairs> v;

for(int i=0; i<n; i++)
{
    int j,k;
    cin>>j>>k;
    v.push_back(make_pair(j,k));
}

for(int i=0; i<n; i++)
    cout<<v[i].first<<“ “<<v[i].second<<endl;

The above piece of code will make a list of pairs that would be stored in a vector called v. Then the n elements are printed out with their respective pairs.

Upvotes: 0

emartel
emartel

Reputation: 7773

John Dibling gave a good explanation of the problem, but even removing using namespace std; will not be sufficient as distance is defined in the same header as pair and it gets resolved as the function that calculates the distance between two iterators.

I think this is a minimal rewrite in C++ of what you're trying to achieve

#include <iostream>
#include <utility>
#include <vector>

typedef std::pair<int,int> intPair;
typedef std::vector<intPair> vectorPairs;

double Distance2D(const intPair& p1, const intPair& p2)
{
    double dist;
    dist = sqrt( (p1.first-p2.first)*(p1.first-p2.first) + (p1.second-p2.second)*(p1.second-p2.second) );
    return(dist);
}

int main()
{
    int numEntries;
    std::cin >> numEntries;
    vectorPairs pairs(numEntries);
    for(vectorPairs::iterator itor = pairs.begin(); itor != pairs.end(); ++itor)
    {
        std::cin >> itor->first >> itor->second;
    }
    for(vectorPairs::iterator itor = pairs.begin(); itor != pairs.end(); ++itor)
    {
        std::cout << itor->first << " " << itor->second << std::endl;
    }

    if(pairs.size() >= 2) // need to validate that 0 and 1 and within the range of pairs
    {
        double dist = Distance2D(pairs.at(0), pairs.at(1)); 
        std::cout << dist;
    }
    return 0;
}

Upvotes: 2

John Dibling
John Dibling

Reputation: 101456

There are at least 3 problems with your code that make it not legal C++. The one in question is this:

distance(pi[0],pi[1]); // This line is giving error

There is a distance in the Standard Library, and the compiler is resolving to call this one. This is due to the fact that you do using namespace std. It is preferable to not do using namespace std at all, especially in headers but as you can see it's causing a problem here too.

There are a few potential solutions.

  1. Remove the using namespace std and qualify all your uses of Standard Library objects (like std::cin)
  2. Replace the using namespace std with specific using declarations, bringing in only those pieces from the StdLib that you actually use (like using std::cin)
  3. Change the distance call to refer explicitly to the global namespace: ::distance(...);

Edit In addition, this is not legal C++:

int N,i,j;
cin >> N;
pair<int,int> pi[N];

Standard C++ only allows C-style arrays to be declared when the size is known at compile-time. You are probably using a gcc tool to build this, which is why it isn't resulting in an error. However, you are using a GCC-specific language extension, dynamic-size arrays, and the code you've written isn't part of the C++ language itself.

Upvotes: 6

Mark B
Mark B

Reputation: 96241

distance is defined by the standard library, which you already using'ed into the global namespace, so it's trying to use that one rather than the one you wrote (and expected).

Call your function something else to avoid the name clash.

Also just as style notes, in C++ code #define can usually be replaced with const or inline functions to provide much greater type safety, and I like to write using std::cout, etc for each item I need rather than using namespace std.

Upvotes: 6

Related Questions