在 C++ 中使用对
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13383395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using pair in C++
提问by user1543957
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;
}
回答by Mark B
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).
distance
由标准库定义,您已经using
将其添加到全局命名空间中,因此它试图使用该名称空间而不是您编写的(和预期的)。
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
.
同样就像样式注释一样,在 C++ 代码#define
中通常可以用 const 或内联函数替换以提供更大的类型安全性,我喜欢为using std::cout
我需要的每个项目编写, etc 而不是using namespace std
.
回答by John Dibling
There are at least 3 problems with your code that make it not legal C++. The one in question is this:
您的代码至少有 3 个问题使其不合法 C++。问题是这样的:
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.
distance
标准库中有一个,编译器正在解析调用这个。这是因为你这样做了using namespace std
。最好不要这样做using namespace std
,尤其是在标题中,但正如您所看到的那样,它也在这里引起了问题。
There are a few potential solutions.
有一些潜在的解决方案。
- Remove the
using namespace std
and qualify all your uses of Standard Library objects (likestd::cin
) - Replace the
using namespace std
with specific using declarations, bringing in only those pieces from the StdLib that you actually use (likeusing std::cin
) - Change the distance call to refer explicitly to the global namespace:
::distance(...);
- 删除
using namespace std
并限定您对标准库对象的所有使用(如std::cin
) - 将 替换为
using namespace std
特定的 using 声明,只引入您实际使用的 StdLib 中的那些部分(如using std::cin
) - 更改 distance 调用以显式引用全局命名空间:
::distance(...);
EditIn addition, this is not legal C++:
编辑此外,这不是合法的 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.
标准 C++ 仅允许在编译时已知大小时声明 C 样式数组。您可能正在使用 gcc 工具来构建它,这就是它不会导致错误的原因。但是,您使用的是 GCC 特定的语言扩展、动态大小的数组,并且您编写的代码不是 C++ 语言本身的一部分。
回答by emartel
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.
John Dibling 对这个问题给出了很好的解释,但即使删除using namespace std;
也不够,因为距离在与 pair 相同的标头中定义,并且它作为计算两个迭代器之间距离的函数得到解决。
I think this is a minimal rewrite in C++ of what you're trying to achieve
我认为这是在 C++ 中对您要实现的目标的最小重写
#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;
}
回答by Ashish Kumar
The usage of pair in C++
C++中pair的用法
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 :
现在假设您要创建一个包含“n”对的列表(或数组或向量),为此我们可以使用如下的向量数据结构:
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.
上面的一段代码将创建一个对的列表,这些对将存储在一个名为 v 的向量中。然后将 n 个元素与它们各自的对一起打印出来。
回答by rashedcs
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;
}