Dec 13, 2020 at 10:22am UTC
Let say I have a vector of ints, and I want to specify an additional value for each of them like a string or bool.
I was thinking about something like that but it has multiple issues and it won't work:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
struct Single
{
int num;
bool scorched = false ;
};
int hourglassSum(vector<vector<Single>> arr)
{
for (auto x : arr )
{
cout << "[VECTOR BEGIN] : " << &x << endl;
for (auto item = x.begin() ; !(x.scorched) ; item++ )
{
x.scorched = true ;
cout << item;
}
}
return 0;
}
Last edited on Dec 13, 2020 at 10:23am UTC
Dec 13, 2020 at 10:41am UTC
You just need a vector<Single>. For C++17 consider:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#include <vector>
#include <iostream>
using namespace std;
struct Single
{
int num {};
bool scorched {};
};
int hourglassSum(vector<Single>& arr)
{
for (auto & [num, scor] : arr) {
if (!scor) {
scor = true ;
cout << num << " " << scor << '\n' ;
}
}
return 0;
}
Last edited on Dec 13, 2020 at 10:42am UTC
Dec 15, 2020 at 3:13pm UTC
Almost got it, just need a vector of vectors unfortunately.
Also, I already have the integers in arr so I want my function to just use this values without overwriting, with adding variable to the type but I got errors about conversion:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
struct simpleType
{
int val; bool scor = false ;
};
int hourglassSum(vector<vector<simpleType>> arr)
{
for (const auto &X : arr)
{ cout << "[VECTOR] @ " << &X << endl;
for (const auto &item : X)
{
// rest of the code
}
}
return 0;
}
Last edited on Dec 15, 2020 at 3:14pm UTC
Dec 15, 2020 at 3:16pm UTC
I got errors about conversion
What conversion? What error? We are not psychics.
Last edited on Dec 15, 2020 at 3:16pm UTC
Dec 15, 2020 at 3:53pm UTC
error: could not convert ‘arr’ from ‘vector<vector<int>>’ to ‘vector<vector<simpleType>>’
int result = hourglassSum(arr);
Dec 15, 2020 at 4:46pm UTC
Why are you asking me with my own question? I don't focking know, im too stupid if you failed to realize that yet
Last edited on Dec 15, 2020 at 4:50pm UTC
Dec 15, 2020 at 5:02pm UTC
Post a complete sample program. There's not sufficient info/code to advise.