How to create a new custom type with given vector

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
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
Dear takeshi404/Trevion

Please explain the logic of:
1
2
3
4
5
6
7
8
9
vector<Single> x;
// fill x with data

cout << "[VECTOR BEGIN] : " << &x << endl;
for ( auto item = x.begin() ; !(x.scorched) ; item++ )
{
  x.scorched = true;
  cout << item;
}

What is the type of 'item'?
Does a std::vector have public member 'scorched'?
When does the loop end?
it doesn't work when I use my own custom defined types


Has your custom defined type got appropriate constructor, copy constructor etc etc?? If the used type is properly defined, then it will work.

If it's not then post the custom type definition and how it's being used.
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
I got errors about conversion

What conversion? What error? We are not psychics.
Last edited on
error: could not convert ‘arr’ from ‘vector<vector<int>>’ to ‘vector<vector<simpleType>>’
int result = hourglassSum(arr);
You have function int hourglassSum(vector<vector<simpleType>>);

You have probably code that does use the function:
1
2
3
vector<vector<int>> snafu; // declaration

int result = hourglassSum( snafu ); // error 

How should one convert int to simpleType?
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
Post a complete sample program. There's not sufficient info/code to advise.
Topic archived. No new replies allowed.