Reputation: 133
I am trying to solve this challenge from testdome. I think my approach to the solution is not optimal (or even correct). But before changing my approach I was wondering is it possible to return a vector of pairs? if yes, how?.
Below you will see the challenge, and afterwards my code.
Thank you.
#include <stdexcept>
#include <iostream>
#include <vector>
#include <utility>
std::pair<int, int> findTwoSum(const std::vector<int>& list, int sum)
{
int tracker = list.size();
std::pair <int, int> result;
std::vector <std::pair <int,int>> list1(100);
for (int i = 0; i < (int)list.size(); i++){
for (int j = ((int)list.size()-tracker); j < (int)list.size(); j++){
if (list[i]+list[j] == sum)
list1.push_back(std::make_pair(list[i], list[j]) );
}
tracker--;
}
return list1; // I am stuck here
//throw std::logic_error("Waiting to be implemented");
}
#ifndef RunTests
int main()
{
std::vector<int> list = {3, 1, 5, 7, 5, 9};
std::pair<int, int> indices = findTwoSum(list, 10);
std::cout << indices.first << '\n' << indices.second;
}
#endif
EDIT
Initially, this was my solution. But once I posted it on testdome, I am getting zero correct answers out of 4 categories. It is test number 5. That is why I thought, returning only one answer was wrong.
#include <stdexcept>
#include <iostream>
#include <vector>
#include <utility>
std::pair<int, int> findTwoSum(const std::vector<int>& list, int sum)
{
int tracker = list.size();
std::pair <int, int> result;
std::vector <std::pair <int,int>> list1(100);
for (int i = 0; i < (int)list.size(); i++){
for (int j = ((int)list.size()-tracker); j < (int)list.size(); j++){
if (list[i]+list[j] == sum)
return result=std::make_pair(list[i], list[j]);
}
tracker--;
}
return std::make_pair(-1,-1);
//throw std::logic_error("Waiting to be implemented");
}
#ifndef RunTests
int main()
{
std::vector<int> list = {3, 1, 5, 7, 5, 9};
std::pair<int, int> indices = findTwoSum(list, 10);
std::cout << indices.first << '\n' << indices.second;
}
#endif
Upvotes: 2
Views: 6361
Reputation: 12263
If you are using C++17, you have two options:
auto
return type deductionauto create_vector_of_pairs() {
std::vector<std::pair<int, int>> v;
v.emplace_back(1, 2);
v.emplace_back(3, 4);
v.emplace_back(5, 6);
return v;
}
std::vector<std::pair<...>>
as return typestd::vector<std::pair<int, int>> create_vector_of_pairs() {
std::vector<std::pair<int, int>> v;
v.emplace_back(1, 2);
v.emplace_back(3, 4);
v.emplace_back(5, 6);
return v;
}
Then, whatever option from above you choose, in your main function you could do something like:
std::vector<std::pair<int, int>> v = create_vector_of_pairs();
or
auto v = create_vector_of_pairs();
Upvotes: 0
Reputation: 655
Use can do this:
std::vector<std::pair<int, int> >findTwoSum(const
std::vector<int>& list, int sum)
{
int tracker = list.size();
std::pair <int, int> result;
std::vector <std::pair <int,int>> list1(100);
/* ...*/
return list1;
}
int main()
{
std::vector<int> list = {3, 1, 5, 7, 5, 9};
auto indices = findTwoSum(list, 10);
std::cout << indices[0].first << '\n' << indices[0].second;
}
Upvotes: 2
Reputation: 31465
If you want to return a vector of pairs, your function signature should look like
std::vector<std::pair<...>> f();
Your current function is just returning a single std::pair
.
Upvotes: 0
Reputation: 12939
you just need to switch the function signature to
std::vector<std::pair<int, int>> findTwoSum(const std::vector<int>& list, int sum)
But in the main you are assigning it to a pair, so you need also to change this:
std::pair<int, int> indices = findTwoSum(list, 10); // from
std::vector<std::pair<int, int>> indices = findTwoSum(list, 10); // to
std::vector<std::pair<int, int>> indices { findTwoSum(list, 10) }; // but this is better because you call just one constructor instead of a default constructor and a copy constructor
Upvotes: 0
Reputation: 330
You can simply use std::vector<std::pair<int, int>>
as the return value of your function. (and not only std::pair<int, int>
as you've done in your example)
So your function becomes:
std::vector<std::pair<int, int>> findTwoSum(const std::vector<int>& list, int sum) { ... }
Upvotes: 0