Problems initializing arrays in structs in arrays in structs - c++

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

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

how to show lines separately without overlapping when values are same

Working on google charts API (Line chart).I have same set of values which are used to draw lines for server and shipping, as the values are same two lines are overlapping as shown in the demo https://plnkr.co/edit/PNA1XQapRzvxGfm7behd?p=preview
Is there a way where we can see two lines instead of overlapping when they have the same values?
angular.module('myApp', ['googlechart'])
.controller('myController', function($scope) {
var chart1 = {};
chart1.type = "LineChart";
chart1.displayed = false;
chart1.data = {
"cols": [{
id: "month",
label: "Month",
type: "string"
}, {
id: "laptop-id",
label: "Laptop",
type: "number"
}, {
id: "desktop-id",
label: "Desktop",
type: "number"
}, {
id: "server-id",
label: "Server",
type: "number"
}, {
id: "cost-id",
label: "Shipping",
type: "number"
}],
"rows": [{
c: [{
v: "January"
}, {
v: 19,
}, {
v: 12,
}, {
v: 7, //server
}, {
v: 7 //shipping
}]
}, {
c: [{
v: "February"
}, {
v: 13
}, {
v: 1,
}, {
v: 12 //server
}, {
v: 12 //shipping
}]
}, {
c: [{
v: "March"
}, {
v: 24
}, {
v: 5
}, {
v: 11 //server
}, {
v: 11 //shipping
}
]
}]
};
chart1.options = {
"title": "Sales per month",
"colors": ['#0000FF', '#009900', '#CC0000', '#DD9900'],
"defaultColors": ['#0000FF', '#009900', '#CC0000', '#DD9900'],
"isStacked": "percent",
focusTarget: 'category',
"fill": 20,
"displayExactValues": true,
"vAxis": {
"title": "Sales unit",
"gridlines": {
"count": 10
}
},
"hAxis": {
"title": "Date"
}
};
chart1.view = {
columns: [0, 1, 2, 3, 4]
};
$scope.myChart = chart1;
});
use the series chart option to set pointSize on a specific series
the following will change the server series in the example provided...
series: {
2: {
pointSize: 8
}
}
see forked plnker...

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

performing priority query in mongo

sample document :
{"name":"John", "age":35, "address":".....",.....}
Employees whose join_month=3 is priority 1
Employees whose address contains the string "Avenue" is priority 2
Employees whose address contains the string "Street" is priority 3
Employees whose address contains the string "Road" is priority 4
As of now, I'm at this stage:
db.collection.aggregate([
{ "$match": {
"$or": [
{ "join_month": 3 },
{ "address": /.*Avenue.*/i },
{ "address": /.*Street.*/i },
{ "address": /.*Road.*/i }
]
}},
{ "$project": {
"name": 1,
"age": 1,
"_id": 0,
"priority": { ?????????? }
}},
{ "$sort": { "priority": 1 } }
])
I'm stuck at priority field. What should I put there?
Using the aggregation framework you would "ideally" want to use the $regex filter within the $cond logical operator in the $project pipeline step but unfortunately MongoDB is yet to support this.
There is a JIRA ticket for this currently open $project filter using $regex
However, a workaround (though not the best solution performant-wise) would be to use map-reduce. Consider populating a test collection:
db.test.insert([
{ _id: 0, "join_month": 12, "address": "33 Point Avenue", "name": "John", "age":35 },
{ _id: 1, "join_month": 10, "address": "2A Broad Street, Surbub", "name": "Jane", "age":21 },
{ _id: 2, "join_month": 3, "address": "127 Umpstreeten Road, Surbub", "name": "Alan", "age":63 },
{ _id: 3, "join_month": 3, "address": "127 Umpstreeten Road, Surbub", "name": "Louise", "age":30 }
])
Define your map function as:
var mapper = function() {
var priority;
if (this.join_month==3){
priority = 1;
}
else if (this.address.match(/Avenue/i)){
priority = 2;
}
else if (this.address.match(/Street/i)){
priority = 3;
}
else if (this.address.match(/Road/i)){
priority = 4;
}
else {
priority = 99;
}
var value = {
"name": this.name,
"age": this.age,
"priority": priority
};
emit( this._id, value );
};
The reduce function follows:
var reducer = function() { };
Then run the mapduce operation on the test collection and store the result in the collection mr_result
db.test.mapReduce(mapper, reducer, {
"out": 'mr_result'
"query": {
"$or": [
{ "join_month": 3 },
{ "address": /.*Avenue.*/i },
{ "address": /.*Street.*/i },
{ "address": /.*Road.*/i }
]
}
})
Query the result collection:
db.mr_result.find().sort({ "priority": 1})
Sample Output
{ "_id" : 2, "value" : { "name" : "Alan", "age" : 63, "priority" : 1 } }
{ "_id" : 3, "value" : { "name" : "Louise", "age" : 30, "priority" : 1 } }
{ "_id" : 0, "value" : { "name" : "John", "age" : 35, "priority" : 2 } }
{ "_id" : 1, "value" : { "name" : "Jane", "age" : 21, "priority" : 3 } }

Retrieving an array from a multi-dimensional array

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.