C++ 如何将 vector<pair<int, int> > 传递给函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14618065/
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
How can you pass a vector<pair<int, int> > into a function?
提问by Bob John
If I try to declare the function as void some_function(vector<pair<int, int> > theVector)
, I get an error (presumably from the comma after "pair<int
." Any ideas on how I can pass this vector with pairs into a function?
如果我尝试将函数声明为void some_function(vector<pair<int, int> > theVector)
,我会收到一个错误(大概是来自“ pair<int
.”之后的逗号。关于如何将这个带有成对的向量传递给函数的任何想法?
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
void someFunc(int x, int y, vector<pair<int, int> > hello);
int main()
{
int x = 0;
int y = 5;
vector<pair<int, int> > helloWorld;
helloWorld.push_back(make_pair(1,2));
someFunc(x,y,helloWorld);
}
void someFunc(int x, int y, vector<pair<int, int> > hello)
{
cout << "I made it." << endl;
}
Error: 'vector' has not been declared
错误:“向量”尚未声明
回答by
You failed to include <utility>
, which defines std::pair
, andyou're using vector
and pair
, instead of std::vector
and std::pair
.
您未能包含<utility>
,它定义了std::pair
,并且您使用的是vector
and pair
,而不是std::vector
and std::pair
。
All of the standard template library is inside the namespace std
, so you must prefix types from the STL with std
, like std::vector
. An alternative would be to add using std::vector;
after you include <vector>
.
所有标准模板库都在命名空间内std
,因此您必须在 STL 中为类型添加前缀std
,例如std::vector
. 另一种方法是using std::vector;
在包含之后添加<vector>
。
回答by billz
You need to have provide full namespace for vector, pair, make_par, they are from std namespace:
您需要为 vector、pair、make_par 提供完整的命名空间,它们来自 std 命名空间:
void someFunc(int x, int y, std::vector<std::pair<int, int> > hello);
int main()
{
int x = 0;
int y = 5;
std::vector<std::pair<int, int> > helloWorld;
helloWorld.push_back(std::make_pair(1,2));
someFunc(x,y,helloWorld);
return 0;
}
void someFunc(int x, int y, std::vector<std::pair<int, int> > hello)
{
std::cout << "I made it." << std::endl;
}
Side note: you could pass vector to someFunc by reference, this will elide unnecessary copy:
旁注:您可以通过引用将 vector 传递给 someFunc,这将消除不必要的副本:
void someFunc(int x, int y, const std::vector<std::pair<int, int> >& hello);
^^^ ^^
回答by MahlerFive
Have you included <vector>
and <utility>
?
You should use the std::
namespace on both vector
and pair
.
你包括<vector>
和<utility>
吗?您应该std::
在vector
和上都使用命名空间pair
。
eg.
void some_function(std::vector< std::pair<int, int> > theVector)
例如。
void some_function(std::vector< std::pair<int, int> > theVector)
edit: Of course you generally shouldn't pass in a vector by value, but by reference.
编辑:当然,您通常不应该通过值传递向量,而是通过引用传递。
eg.
void some_function(std::vector< std::pair<int, int> >& theVector)
例如。
void some_function(std::vector< std::pair<int, int> >& theVector)
回答by Soroush khoubyarian
I chekced your code you just have to add the std
namespace right below your #include
. And you don't need to add #include <utility>
it can work without it.using namespace std
我检查了您的代码,您只需std
在#include
. 而且你不需要添加#include <utility>
它可以在没有它的情况下工作。using namespace std