Retrieving an array from a multi-dimensional array - c++

I'm working on some mathematics research, and I'm having difficulty working my way through the c++ language. Specifically, (and i'm not sure if this is even possible) I'd like to retrieve an array from a multi-dimensional array. Let me clarify. As I understand it, a multi-dimensional array is an array of arrays. I am working with a 3-D array, and what I want to do is retrieve an entire 2-D array from that 3-D array. Here is a snippet from my code. The first declarations are how I had it before (with 15 2-D arrays), the next declaration is how I want things to go (so that I can use a while loop to move through all my 2-D arrays checking for things as it goes).
int round1[3][2] = { { 1, 2 }, { 3, 4 }, { 5, 6 } };
int round2[3][2] = { { 1, 2 }, { 3, 5 }, { 4, 6 } };
int round3[3][2] = { { 1, 2 }, { 3, 6 }, { 4, 5 } };
int round4[3][2] = { { 1, 3 }, { 2, 4 }, { 5, 6 } };
int round5[3][2] = { { 1, 3 }, { 2, 5 }, { 4, 6 } };
int round6[3][2] = { { 1, 3 }, { 2, 6 }, { 4, 5 } };
int round7[3][2] = { { 1, 4 }, { 2, 3 }, { 5, 6 } };
int round8[3][2] = { { 1, 4 }, { 2, 5 }, { 3, 6 } };
int round9[3][2] = { { 1, 4 }, { 2, 6 }, { 3, 5 } };
int round10[3][2] = { { 1, 5 }, { 2, 3 }, { 4, 6 } };
int round11[3][2] = { { 1, 5 }, { 2, 4 }, { 3, 6 } };
int round12[3][2] = { { 1, 5 }, { 2, 6 }, { 3, 4 } };
int round13[3][2] = { { 1, 6 }, { 2, 3 }, { 4, 5 } };
int round14[3][2] = { { 1, 6 }, { 2, 4 }, { 3, 5 } };
int round15[3][2] = { { 1, 6 }, { 2, 5 }, { 3, 4 } };
int rounds[15][3][2] = { { { 1, 2 }, { 3, 4 }, { 5, 6 } }, { { 1, 2 }, { 3, 5 }, { 4, 6 } }, { { 1, 2 }, { 3, 6 }, { 4, 5 } }, { { 1, 3 }, { 2, 4 }, { 5, 6 } }, { { 1, 3 }, { 2, 5 }, { 4, 6 } }, { { 1, 3 }, { 2, 6 }, { 4, 5 } }, { { 1, 4 }, { 2, 3 }, { 5, 6 } }, { { 1, 4 }, { 2, 5 }, { 3, 6 } }, { { 1, 4 }, { 2, 6 }, { 3, 5 } }, { { 1, 5 }, { 2, 3 }, { 4, 6 } }, { { 1, 5 }, { 2, 4 }, { 3, 6 } }, { { 1, 5 }, { 2, 6 }, { 3, 4 } }, { { 1, 6 }, { 2, 3 }, { 4, 5 } }, { { 1, 6 }, { 2, 4 }, { 3, 5 } }, { { 1, 6 }, { 2, 5 }, { 3, 4 } } };
int tourney_count = 0;
string tourney[5] = { "", "", "", "", "" };
int roundcount_a = 0;
int roundcount_b = 0;
int roundcount_c = 0;
int roundcount_d = 0;
int roundcount_e = 0;
if (is_compatible(rounds[0][][], rounds[3][][]))
//do stuff
return 0;
is_compatible is a function I've defined above that basically checks to see if any two 2-D arrays contain like pairs. My plan is to do some work with while loops (haven't quite figured it all out yet) to go through my 3-D array and move from one to the next checking to see which ones are compatible, and then moving on to a third array. That's about it.
If anyone has any better ideas on how to go about something like this (not using arrays in arrays in arrays), then I would be happy to hear you out. However, I'd really just like to know if I can do what i'm trying to do and return one of these 2-D arrays from a 3-D array.
Thanks.

Related

loopback 3 string not matched with and condition

i am trying to filter with multiple value in loopback 3. 'AND condition' is not working. Numeric value filter working good but string value not matched.
this is my condition is now :
{ "and": [{ "id": { "neq": "5d146f1f7a62651b8162fcf3" } },
{ "gender": "male" }, { "age": { "between": [16, 60] } },
{ "interestId": { "inq": ["5d11c0ce59e2b64e8dc12956", "5d11c0e259e2b64e8dc12958", "5d11c08659e2b64e8dc12953", "5d11c01759e2b64e8dc1294b", "5d11c03759e2b64e8dc1294d"] } },
{ "location": { "near": { "lat": 26.8574194, "lng": 75.7977288 }, "maxDistance": 6, "unit": "kilometers" } },
{ "maritalStatus": { "like": {} } }, { "college": { "like": {} } },
{ "career": { "like": {} } }, { "currentJob": { "like": {} } },
{ "height": { "between": [50, 89] } }, { "weight": { "between": [72, 192] } }, { "bodyBuild": "Slim" }] }
String value is not push in array.
if (interestId.highSchool && interestId.highSchool !="") {
var highschool = new RegExp('.*' + interestId.highSchool + '.*', "i");
query.and.push({
highSchool: { like: highschool }
})
}
where is issue i don't understand. if i did't pass string value it's working fine.
Try following way:-
if (interestId.highSchool && interestId.highSchool !="") {
query.and.push({
highSchool: {regexp: new RegExp('.*' + interestId.highSchool + '.*', "i")}
})
}

MongoDB: Create an Exact Match field

I'm trying order some data, and want to have the exact match as first results and then the rest.
I tried with this $eq in $project, but it seems that something won't work.
Is it possible to do something similar?
db.getCollection('words').aggregate([
{ $match: { "kanji.text": /^彼/ } },
{ $project: {
"kanji": 1,
"kana": 1,
"sense":1,
"exact": {
$eq : [ "$kanji.0.text", "彼" ]
}
}
},
{ $sort: {"exact": 1} }
])
Document Structure:
EDIT: I have found a bad workaround
db.getCollection('words').aggregate([
{ $match: { "kanji.text": /^彼/ } },
{ $limit : 10 },
{ $addFields: {
exact: {
$filter: {
input: '$kanji.text',
cond: {
$eq: ['$$this', "彼"]
}
}
}
}},
{ $project: {
"kanji": 1,
"kana": 1,
"sense":1,
"exact": 1
}
},
{ $sort: {"exact.0": -1} }
])

how can I serialize 2 sets using nlohmann/json.hpp

I have two unordered sets of pairs (X,Y) implemented using boost hash and I would like to convert them to a Json file that has a special format.
unordered_set<pair<int,int>> visited, cleaned
. I want them to be represented this way in Json format using nlohmann/json.hpp C++:
{
"visited": [
{
"X": 2,
"Y": 2
},
{
"X": 3,
"Y": 0
},
{
"X": 3,
"Y": 1
},
{
"X": 3,
"Y": 2
}
],
"cleaned": [
{
"X": 2,
"Y": 2
},
{
"X": 3,
"Y": 0
},
{
"X": 3,
"Y": 2
}
],
}
can anyone help me with the c++ code for this part?
my code is
for (auto it = visited.begin(); it != visited.end(); ++it)
{
j2["visited"]["X"]=it->second;
j2["visited"]["Y"] = it->first;
}
for (auto it = cleaned.begin(); it != cleaned.end(); ++it)
{
j2["cleaned"]["X"] = it->second;
j2["cleaned"]["Y"] = it->first;
}
and it produces:
{
"cleaned": {
"X": 3,
"Y": 2
},
"visited": {
"X": 3,
"Y": 2
}
}
The JSON format you want contains arrays. Use something like this to explicitly create
them:
nlohmann::json arr;
for (auto it = visited.begin(); it != visited.end(); ++it) {
nlohmann::json o;
o["X"] = it->second;
o["Y"] = it->first;
arr.push_back(o);
}
j2["visited"] = arr;
And similarly for the second part.

Initialization of a struct array [duplicate]

This question already has answers here:
How to initialize an array of struct in C++?
(3 answers)
Closed 5 years ago.
struct Sensors
{
int pin;
int angle;
bool state;
};
Sensors sensor[6];
How can I initialize every field of sensor like a normal array?
This method
mydata data[] = { { 1, 2, 3, 4, 5, 6 },
{ 0, 60, 120, 180, -120, -60 },
{ false, false, false, false, false, false} };
doesn't work, it returns me too many initialization.
You can use aggregate initialization.
struct Sensors
{
int pin;
int angle;
bool state;
};
Sensors sensor[6] = {
{ 0, 0, true },
{ 1, 0, true },
{ 2, 0, false },
{ 3, 0, false },
{ 4, 0, false },
{ 5, 0, false }
};
struct Sensors
{
int pin;
int angle;
bool state;
};
int main()
{
Sensors sensor[6] = {
{ 1, 2, false },
{ 1, 2, true },
{ 1, 3, false },
{ 2, 2, false },
{ -1, -2, true },
{ 1, 2, false }
};
return 0;
}

Problems initializing arrays in structs in arrays in structs

I've been tasked to remove some compiler warning. I've been able to boil the problem down to the following example, which I am scratching my head why it won't work. I guess I don't know how to initialize stuff in C++. Any help would be appreciated.
I use g++ like so:
g++ init_arr.cpp
Here's the code. I want to initialize all the people at all the tables in Aisle pizza:
// init_arr.cpp
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
struct Person {
int id;
string name;
double money;
};
struct Table {
Person tab[4];
};
struct Aisle {
Table ais[3];
};
int main() {
cout << "main function()" << endl;
Aisle pizza =
{
{ // Table 0
{ 0, "Tom", 100.0 },
{ 1, "Mary", 101.0 },
{ 2, "Jane", 103.0 },
{ 3, "Joe", 104.0 }
},
{ // Table 1
{ 0, "Tom", 100.0 },
{ 1, "Mary", 101.0 },
{ 2, "Jane", 103.0 },
{ 3, "Joe", 104.0 }
},
{ // Table 2
{ 0, "Tom", 100.0 },
{ 1, "Mary", 101.0 },
{ 2, "Jane", 103.0 },
{ 3, "Joe", 104.0 }
}
};
return 0;
}
I thought the above would work, but I get the following error:
g++ init_arr.cpp -std=gnu++0x
init_arr.cpp: In function ‘int main()’:
init_arr.cpp:49: error: too many initializers for ‘Table [3]’
init_arr.cpp:49: error: too many initializers for ‘Aisle’
While #us2012 showed what works and provides a good explanation (+1 for him), I find it not very readable. This is an alternative:
Aisle pizza =
{
Table { // Table 0
Person { 0, "Tom", 100.0 },
Person { 1, "Mary", 101.0 },
Person { 2, "Jane", 103.0 },
Person { 3, "Joe", 104.0 }
},
Table { // Table 1
Person { 0, "Tom", 100.0 },
Person { 1, "Mary", 101.0 },
Person { 2, "Jane", 103.0 },
Person { 3, "Joe", 104.0 }
},
Table { // Table 2
Person { 0, "Tom", 100.0 },
Person { 1, "Mary", 101.0 },
Person { 2, "Jane", 103.0 },
Person { 3, "Joe", 104.0 }
}
};
You're missing lots of pairs of parentheses. I have added comments to make it clearer which bit starts where.
To put it into one sentence, your problem is that an array with three elements can be initialized with {1,2,3} while a struct that contains an array as its single member is an extra layer and therefore has to be initalized with { {1,2,3} } - the outer layer is the struct, the inner layer is the array.
Aisle pizza =
{ // Aisle init
{ // Table ais[3] init
{ // ais[0] init
{ // Person tab[4] init
{ 0, "Tom", 100.0 },
{ 1, "Mary", 101.0 },
{ 2, "Jane", 103.0 },
{ 3, "Joe", 104.0 }
}
},
{ // ais[1] init
{ // Person tab[4] init
{ 0, "Tom", 100.0 },
{ 1, "Mary", 101.0 },
{ 2, "Jane", 103.0 },
{ 3, "Joe", 104.0 }
}
},
{ // ais[2] init
{ // Person tab[4] init
{ 0, "Tom", 100.0 },
{ 1, "Mary", 101.0 },
{ 2, "Jane", 103.0 },
{ 3, "Joe", 104.0 }
}
}
}
};
Each block needs to represent an object. Aisle struct contains an array object (ais). Each element of the ais array contains a Table struct. Each Table struct contains an array object (tab). and so on...
Try this:
Aisle pizza =
{ // Aisle
{ // .ais
{ // .ais[0]
{ // .ais[0].tab
{ 0, "Tom", 100.0 }, // tab[0]
{ 1, "Mary", 101.0 }, // tab[1]
{ 2, "Jane", 103.0 }, // tab[2]
{ 3, "Joe", 104.0 } // tab[3]
}
},
{ // .ais[1]
{ // .ais[1].tab
{ 0, "Tom", 100.0 }, // tab[0]
{ 1, "Mary", 101.0 }, // tab[1]
{ 2, "Jane", 103.0 }, // tab[2]
{ 3, "Joe", 104.0 } // tab[3]
}
},
{ // .ais[2]
{ // .ais[2].tab
{ 0, "Tom", 100.0 }, // tab[0]
{ 1, "Mary", 101.0 }, // tab[1]
{ 2, "Jane", 103.0 }, // tab[2]
{ 3, "Joe", 104.0 } // tab[3]
}
}
}
};