C++ Unordered map initializer map not initializing properly - c++

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.

Related

Boost.Geometry doesn't find second point of polygon-line intersecion

I'm trying to use Boost.Geometry library to find the intersection of square and line,
model::ring<model::d2::point_xy<double>> ring { {0, 0}, {2, 0}, {2, 2}, {0, 2} };
model::polygon<model::d2::point_xy<double>> pol;
pol.inners().push_back (ring);
model::linestring<model::d2::point_xy<double>> line { {1, 3}, {-1, -1} };
model::multi_point<model::d2::point_xy<double>> out;
intersection (pol, line, out); //out returns only {0.5, 2}, but not {0, 1}
but it returns only one point, althougt actually there is two points of intersection
How can I find all the points of intersection?
Close your ring and put it in the expected order (by default clockwise, see default template parameters):
model::ring<model::d2::point_xy<double>> ring {
{0, 0}, {0, 2}, {2, 2}, {2, 0}, {0, 0}
};
Your ring was not valid, i.e. not fullfilling the requirements of the specified template arguments.
As per documentation (see under rules) using an invalid geometry as input may give wrong results and validity is neither checked not corrected by the algorithm.
The ring is also not automatically closed on construction or before first use (how should it know you are not going to append more points?). Here is an example construction with repeated closing point.
There are however is_valid and correct to remedy this.
You also probably want to push the points to the exterior ring, i.e. pol.outer(). Your polygon needs to have an exterior ring, interior rings determine holes. You can directly construct the polygon without interior rings:
model::polygon<model::d2::point_xy<double>> pol {
{ {0, 0}, {0, 2}, {2, 2}, {2, 0}, {0, 0} }
};

Having some trouble with initializing a 2D array and then sending it off to a function

I am making a TicTacToe program and I'm trying to use OOP techniques. Within my 'Board' class I am wanting the program to store each way a set of moves can be won.
I hope this can be demonstrated here:
Board.h
#pragma once
class Board
{
private:
int winningRows[8][3]; //Variable in question
public:
static const char X = 'X'; //Game piece 'X'
static const char O = 'O'; //Game piece 'O'
static const char EMPTY = ' '; //Empty game piece
static const char TIE = 'T'; //Game is tie
static const char NOONE = 'N'; //Nobody has won game yet
static const int numbOfSquares = 9; //Number of squares on the board
int InitializeWinningCombinations();
void FindWinner();
};
Board.cpp
#include "stdafx.h"
#include "Board.h"
int Board::InitializeWinningCombinations()
{
/*
The playing board
0, 1, 2
3, 4, 5
6, 7, 8
*/
//All possible ways player can win game
winningRows[8][3] = {
//Horizontal
{0, 1, 2},
{3, 4, 5},
{6, 7, 8},
//Vertical
{0, 3, 6},
{1, 4, 7},
{2, 5, 8},
//Diagonal
{2, 4, 6},
{0, 4, 8}
};
//return winnigRows[8][3];
}
void Board::FindWinner()
{
//I am wanting to get the variable here so I can play around with it later.
int winningRows = InitializeWinningCombinations();
}
I could just have the 'winningRows' variable inside the 'FindWinnner' function but from my understanding it is best to abstract as much as possible and have it as a member of the 'Board' class
Thank you for your time.
winningRows[8][3] = {
//Horizontal
{0, 1, 2},
{3, 4, 5},
{6, 7, 8},
//Vertical
{0, 3, 6},
{1, 4, 7},
{2, 5, 8},
//Diagonal
{2, 4, 6},
{0, 4, 8}
};
Is an attempted array assignment not an initialization and it cannot be done. You can initialize the array in a constructor like
Board() : winningRows{
//Horizontal
{0, 1, 2},
{3, 4, 5},
{6, 7, 8},
//Vertical
{0, 3, 6},
{1, 4, 7},
{2, 5, 8},
//Diagonal
{2, 4, 6},
{0, 4, 8}
} {}
Live Example
You'd have to change the signature to
int** InitializeWinningCombinations();
Then you could call it as
int** winningRows = InitializeWinningCombinations();

Initializing multiple data structures inside of each other c++11

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});

Mathematica: converting output from Cluster[]

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}}

2D int array in C++

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