I was looking for help on how to initialize the following data structure inside of my constructor for backtracking:
stack<tuple<vector<set<int> >, int, int> > record; //none of the structures have been initialized yet
Thank you all for your help.
When you have a complex type like that, it is helpful to divide the type into fundamental types before figuring out how to initialize it.
Divide your type into fundamental types, it looks like:
stack<tuple<vector<set<int> >, int, int> > record;
^ ^
| |
tuple<vector<set<int> >, int, int>
^ ^ ^ ^ ^ ^
| | | | | |
vector<set<int> >
^ ^
| |
set<int>
^ ^
| |
To initialize an object of such a type, you'll have to figure out how to build up from the constituent fundamental types.
Initialize an int.
int a{0};
Initialize a set<int>.
set<int> b{1, 2};
Initialize a vector<set<int>>.
vector<set<int>> c{ {1, 2}, {2, 3, 4}, {4, 5, 6, 8} };
Initialize a tuple<vector<set<int>>, int, int>.
tuple<vector<set<int>>, int, int> d{ { {1, 2}, {2, 3, 4}, {4, 5, 6, 8} }, 10, 20};
However, you cannot use the same strategy to initialize stack since std::stack does not have a constructor that you can use like:
stack<int> e{1, 3, 5};
That means, you can't initialize a stack<tuple<vector<set<int>>, int, int>> as:
stack<tuple<vector<set<int> >, int, int> > record
{
{{ {1, 2}, {2, 3, 4}, {4, 5, 6, 8} }, 10, 20},
{{ {1, 2}, {2, 3, 4}, {4, 5, 6, 8} }, 10, 20}
};
Your only choice is to default construct record and add items to it.
stack<tuple<vector<set<int> >, int, int> > record;
using item_type = decltype(record)::value_type;
record.push(item_type{{ {1, 2}, {2, 3, 4}, {4, 5, 6, 8} }, 10, 20});
record.push(item_type{{ {1, 2}, {2, 3, 4}, {4, 5, 6, 8} }, 10, 20});
Related
I'm relatively new to hash maps. I have the following code in my program:
std::unordered_map<int, int> XY ({
{0, 0}, {0, 3}, {0, 6},
{3, 0}, {3, 3}, {3, 6},
{6, 0}, {6, 3}, {6, 6}
});
For some reason, the map only contains the first three pairs ({0, 0}, {0, 3}, and {0, 6}). Even when I cout the bucket count, it outputs 11. Yet, there's still only three in my map.
How do I fix this? It seems unreasonable to do a bunch of .insert()'s.
I am trying to use the function adjacent_difference from the numeric library on the second element of each entry of a vector of pairs (vector<pair<double,double>>). How can I do it?
Update: here's my code so far (obviously wrong xD):
vector <pair<double,double>> initvalues; //receives pairs with the structure (174.386, 10)
for(int i = 0; i < 10; ++i)
{
initvalues.push_back(make_pair(i, 2+i));
}
vector <pair<double,double>> result(initvalues.size()-1);
adjacent_difference((initvalues.second).begin(),(initvalues.second).end(), (result.second).begin());
initvalues is my main vector that allocates pairs of valus with the structure (174.386, 10) as example. result is the output I want and it will store the first entry of the initvalues vector in the first pair entry and the adjacent_difference in the second entry of the pair.
However, I obtain the following output in the terminal compiling the code I've pasted in here:
stack.C: In function ‘int main()’:
stack.C:16:35: error: ‘class std::vector<std::pair<double, double> >’ has no member named ‘second’
16 | adjacent_difference((initvalues.second).begin(), (initvalues.second).end(), (result.second).begin());
| ^~~~~~
stack.C:16:64: error: ‘class std::vector<std::pair<double, double> >’ has no member named ‘second’
16 | adjacent_difference((initvalues.second).begin(), (initvalues.second).end(), (result.second).begin());
| ^~~~~~
stack.C:16:87: error: ‘class std::vector<std::pair<double, double> >’ has no member named ‘second’
16 | nitvalues.second).begin(), (initvalues.second).end(), (result.second).begin());
| ^~~~~~
If I understand you, this is what you want:
vector<pair<double, double>> initvalues;
for (int i = 0; i < 10; ++i) {
initvalues.push_back(make_pair(i, 2 + i));
}
vector<pair<double, double>> result;
result.reserve(initvalues.size());
adjacent_difference(initvalues.begin(), initvalues.end(),
std::back_inserter(result),
[](const auto& l, const auto& r) {
return std::pair<double, double>{l.first, l.second - r.second};
});
This gives you:
initValues = {0, 2} {1, 3} {2, 4} {3, 5} {4, 6} {5, 7} {6, 8} {7, 9} {8, 10} {9, 11}
result = {0, 2} {1, 1} {2, 1} {3, 1} {4, 1} {5, 1} {6, 1} {7, 1} {8, 1} {9, 1}
I have:
struct X {
int i, j;
};
struct XArray {
X xs[3];
};
X xs1[3] { {1, 2}, {3, 4}, {5, 6} };
XArray xs2 { {1, 2}, {3, 4}, {5, 6} };
The xs1 initializes fine, initializing xs2 gives compiler error:
error: too many initializers for 'XArray'
XArray xs2 { {1, 2}, {3, 4}, {5, 6} };
^
What is wrong? Why can't I initialize?
You need another level of curly-braces:
XArray xs2 { { {1, 2}, {3, 4}, {5, 6} } };
// ^ ^ ^
// | | |
// For XArray structure | |
// | |
// For xs array |
// |
// For the X structure
The compileŕ assumes that xs is one field, the array will only be resolved when you add another brace like:
XArray xs2 { {{1, 2}, {3, 4}, {5, 6}} };
When you would add another element, e.g.
struct YArray {
X a;
X xs[3];
}
then it becomes clear that a and xs both need to be put into braces:
YArray y{
{1,2}, // a
{ {1, 2}, {3, 4}, {5, 6} } // xs
};
X xs[3] = { {1,2}, {3,4}, {5,6}};
Then you plug the whole part to the right into your code:
XArray x2 = { { {1,2}, ... {5,6} }};
This is because XArray has only one member, and it needs one pair of brackets enclosing it:
XArray t = { /* value */ };
And value happens to be an array, which also needs brackets:
X xs[3] = { /* value2 */ };
Since this array holds three values, each of them has to be initialized with a pair of numbers.
You have to use:
XArray xs2 { { {1, 2}, {3, 4}, {5, 6} } };
This way the first element of XArray is initialized with: { {1, 2}, {3, 4}, {5, 6} }.
Online here
Imagine a data set like this:
{{{1,2},{3,4}},{{8,8},{3,7},{5,2}}}.
Note that at the top level this list has {{1,2},{3,4}} as the first element and {{8,8},{3,7},{5,2}} as the second.
Using that fact, the desired output would be:
{{1,2,1},{3,4,1},{8,8,2},{3,7,2},{5,2,2}}
I have already tried using Map[].
This arose because I was using cluster analysis which gave me a list, rather than an indexing of various clusters. I did not find an option in Cluster[] to do this directly.
In[1]:= v = {{{1, 2}, {3, 4}}, {{8, 8}, {3, 7}, {5, 2}}};
Flatten[Table[Map[Join[#, {i}] &, v[[i]]], {i, 1, Length[v]}], 1]
Out[1]= {{1, 2, 1}, {3, 4, 1}, {8, 8, 2}, {3, 7, 2}, {5, 2, 2}}
This is how I would go about the conversion, using the steps as they naturally come to mind.
v = {{{1, 2}, {3, 4}}, {{8, 8}, {3, 7}, {5, 2}}};
Note the result obtained using MapIndexed :-
MapIndexed[{#1, First[#2]} &, v]
{{{{1, 2}, {3, 4}}, 1}, {{{8, 8}, {3, 7}, {5, 2}}, 2}}
To append the part specs (1 & 2) to the subelements I would use MapThread. This requires multiple part specs, e.g. {2, 2, 2} for element 2 :-
MapThread[Append, {{{8, 8}, {3, 7}, {5, 2}}, {2, 2, 2}}]
{{8, 8, 2}, {3, 7, 2}, {5, 2, 2}}
So the MapIndexed expression is modified to produce the necessary part specs :-
MapIndexed[{#1, ConstantArray[First[#2], Length[#1]]} &, v]
{{{{1, 2}, {3, 4}}, {1, 1}}, {{{8, 8}, {3, 7}, {5, 2}}, {2, 2, 2}}}
Now MapThread can be used in the MapIndexed expression :-
MapIndexed[MapThread[Append, {#1, ConstantArray[First[#2], Length[#1]]}] &, v]
{{1, 2, 1}, {3, 4, 1}}, {{8, 8, 2}, {3, 7, 2}, {5, 2, 2}}}
Finally, the first list level is flattened :-
Flatten[MapIndexed[MapThread[Append,
{#1, ConstantArray[First[#2], Length[#1]]}] &, v], 1]
{{1, 2, 1}, {3, 4, 1}, {8, 8, 2}, {3, 7, 2}, {5, 2, 2}}
So I want to initialize an int 2d array very quickly, but I can't figure out how to do it. I've done a few searches and none of them say how to initialize a 2D array, except to do:
int [SOME_CONSTANT][ANOTHER_CONSTANT] = {{0}};
Basically, I've got 8 vertices, and I'm listing the 4 vertices of each face of a cube in an array. I've tried this:
int[6][4] sides = {{0, 1, 2, 3}, {4, 5, 6, 7}, {0, 4, 7, 3}, {7, 6, 2, 3}, {5, 1, 2, 6}, {0, 1, 5, 4}};
But that tells me that there's an error with 'sides', and that it expected a semi-colon. Is there any way to initialize an array quickly like this?
Thanks!
You have the [][] on the wrong side. Try this:
int sides[6][4] = {{0, 1, 2, 3}, {4, 5, 6, 7}, {0, 4, 7, 3}, {7, 6, 2, 3}, {5, 1, 2, 6}, {0, 1, 5, 4}};
Keep in mind that what you really have is:
int **sides
(A pointer to a pointer of ints). It's sides that has the dimensions, not the int. Therefore, you could also do:
int x, y[2], z[3][4], ...;
I think You meant to say
int sides[6][4] = {{0, 1, 2, 3}, {4, 5, 6, 7}, {0, 4, 7, 3}, {7, 6, 2, 3}, {5, 1, 2, 6}, {0, 1, 5, 4}};
int array[n][m] behaves just like int array[n * m].
In fact, array[i][j] = array[m * i + j] for all i, j.
So int array[2][3] = {1, 2, 3, 4, 5, 6}; is a valid declaration and, for example,
array[1][1] = array[3 * 1 + 1] = array[4] = 5.
int sides[6][4] = {{0, 1, 2, 3}, {4, 5, 6, 7}, {0, 4, 7, 3}, {7, 6, 2, 3}, {5, 1, 2, 6}, {0, 1, 5, 4}};
I'm not a regular c++ programmer but I looks like int sides[6][4] seems to compile while int[6][4] sides fails. Languages like C# lets you have the [][] on either sides but apparently c++ doesn't.
int sides[6][4] = ... should do the trick. This sounds like you may be coming from a Java (or other language) background so I do recommend a C++ book The Definitive C++ Book Guide and List for more details.
Yes, the intended type of sides is int[6][4], but C++ has confusing syntax sometimes. The way to declare said array is:
int sides[6][4] = {/*stuff*/};
You run into this with function pointers too, but even worse:
int (*myfuncptr)(int); //creates a function pointer called myfuncptr
With function pointers though, you can do this:
typedef int (*func_ptr_type)(int);
func_ptr_type myfuncptr;
Unfortunately, there's no corresponding magic trick for arrays.
i would make a array outside of function and just assign it it to your local. this will very likely invoke memcpy or just inline memory copying loop
this is the fastest you can get