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?
#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
You failed to include <utility>
, which defines std::pair
, and you're using vector
and pair
, instead of 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>
.
You need to have provide full namespace for vector, pair, make_par, they are from std namespace:
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:
void someFunc(int x, int y, const std::vector<std::pair<int, int> >& hello);
^^^ ^^
Have you included <vector>
and <utility>
? You should use the std::
namespace on both vector
and pair
.
eg. 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)
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
left | center | right |
---|---|---|
One | Two | Three |
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.